Class TransactionSession
- All Implemented Interfaces:
Session
The Session the build injects: the managed session of whatever
transaction the calling thread is in.
One object is injected into a singleton and used by every request, so it
cannot BE a session -- a session is a persistence context, one per unit of
work, and not thread-safe. Each call is forwarded to the session of the
calling thread's @Transactional method instead, which is opened on the
transaction's connection when first used, flushed before the transaction
commits and closed when it ends. That is the same contract as a Spring-managed
EntityManager.
Outside a transaction there is no unit of work to belong to, and every call refuses with a message saying so. The transaction's boundaries belong to the annotation, so beginning, committing, rolling back and closing through this object are refused too.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidStarts a transaction for this session.voidclear()Detaches every entity and discards all unflushed changes.voidclose()Rolls back any active transaction, detaches entities, and releases session resources.voidFlushes pending changes and commits the active transaction.booleanTests whether this session manages an entity that is not scheduled for removal.longCounts stored related rows without loading the relationship's entities.createQuery(String statement) Creates an untyped JPQL query, including bulk update or delete statements.<T> JpqlQuery<T> createQuery(String statement, Class<T> resultType) Creates a typed query using the supported JPQL subset.voidCreates missing tables, indexes, and constraints for registered mappings.voidStops tracking an instance, discarding its unflushed changes.<T> TFinds an entity, reusing its managed instance when present.<T> TFinds an entity and optionally locks its database row.voidflush()Writes pending inserts, updates, relationship changes, and removals.<T> booleanAtomically adds to an integral counter in SQL after flushing pending changes.voidinitialize(Object entity, String field) Loads a relationship if it is still uninitialized.booleanChecks relationship initialization without fetching its contents.booleanReports whether a transaction failure prevents committing.booleanReports whether this session has an active transaction.voidApplies a lock mode to a managed entity's row.<T> Tmerge(T entity) Copies state into a managed instance, cascading through MERGE associations.<T> voidpersist(T entity) Makes a new entity managed and schedules its insertion at flush.<T> Query<T> Creates a fluent entity query.voidReloads a persisted managed instance, discarding its local changes.voidSchedules a managed entity for deletion, cascading REMOVE associations.voidRolls back the active transaction and detaches all managed entities.voidChecks mapped columns, type families, nullability, and primary keys.
-
Constructor Details
-
TransactionSession
-
-
Method Details
-
createQuery
Description copied from interface:SessionCreates a typed query using the supported JPQL subset. Parsing and mapping validation occur immediately, before SQL execution.- Specified by:
createQueryin interfaceSession- Type Parameters:
T- result type- Parameters:
statement- query using entity and Java attribute namesresultType- expected entity or scalar type; useObject[].classfor tuples- Returns:
- a query belonging to this session
-
createQuery
Description copied from interface:SessionCreates an untyped JPQL query, including bulk update or delete statements.- Specified by:
createQueryin interfaceSession- Parameters:
statement- query using entity and Java attribute names- Returns:
- a query yielding entities, scalars, or
Object[]tuples
-
beginTransaction
public void beginTransaction()Description copied from interface:SessionStarts a transaction for this session.- Specified by:
beginTransactionin interfaceSession
-
commitTransaction
public void commitTransaction()Description copied from interface:SessionFlushes pending changes and commits the active transaction. A failure leaves a still-active database transaction requiring rollback. If the database already ended the failed transaction, the session detaches its entities and becomes inactive; a new transaction can then be started.- Specified by:
commitTransactionin interfaceSession
-
rollbackTransaction
public void rollbackTransaction()Description copied from interface:SessionRolls back the active transaction and detaches all managed entities. Pending changes are discarded; Java field values are not reverted.- Specified by:
rollbackTransactionin interfaceSession
-
isTransactionActive
public boolean isTransactionActive()Description copied from interface:SessionReports whether this session has an active transaction.- Specified by:
isTransactionActivein interfaceSession- Returns:
- true between a successful begin and commit or rollback
-
isRollbackOnly
public boolean isRollbackOnly()Description copied from interface:SessionReports whether a transaction failure prevents committing.- Specified by:
isRollbackOnlyin interfaceSession- Returns:
- true when the active transaction must be rolled back
-
contains
Description copied from interface:SessionTests whether this session manages an entity that is not scheduled for removal. -
detach
Description copied from interface:SessionStops tracking an instance, discarding its unflushed changes. Cascades DETACH only through already loaded relationships. Does nothing for an instance that is not managed by this session. -
clear
-
close
-
find
Description copied from interface:SessionFinds an entity, reusing its managed instance when present. An uncached lookup flushes pending changes in an active transaction.- Specified by:
findin interfaceSession- Type Parameters:
T- entity type- Parameters:
type- mapped entity classid- non-null scalar key, embedded key, or compositeIdentifier- Returns:
- the managed entity, or null if no matching row exists
-
find
Description copied from interface:SessionFinds an entity and optionally locks its database row. A pessimistic lock requires an active transaction and a supporting backend. -
lock
-
persist
public <T> void persist(T entity) Description copied from interface:SessionMakes a new entity managed and schedules its insertion at flush. Traverses associations with PERSIST cascade. A to-one reference to an unsaved entity without that cascade is rejected at flush. -
merge
public <T> T merge(T entity) Description copied from interface:SessionCopies state into a managed instance, cascading through MERGE associations. Continue working with the returned instance; the supplied detached instance does not become managed merely because it was passed to this method. -
remove
-
refresh
Description copied from interface:SessionReloads a persisted managed instance, discarding its local changes. Traverses REFRESH cascades. Rejects new, unflushed instances before changing session state. A failure after reload begins clears the context and marks an active transaction rollback-only. -
flush
-
increment
Description copied from interface:SessionAtomically adds to an integral counter in SQL after flushing pending changes. Also increments an optimistic version when present and refreshes a managed instance of the affected row. Guards against counter and version overflow.- Specified by:
incrementin interfaceSession- Type Parameters:
T- entity type- Parameters:
type- mapped entity classid- entity identifierfield- Java name of an int or long field that is neither key nor versionamount- signed amount to add- Returns:
- true if a row changed; false for a missing row, null counter, or overflow
-
query
-
createTables
public void createTables()Description copied from interface:SessionCreates missing tables, indexes, and constraints for registered mappings. Run outside application transactions. Does not migrate existing tables.- Specified by:
createTablesin interfaceSession
-
validateSchema
public void validateSchema()Description copied from interface:SessionChecks mapped columns, type families, nullability, and primary keys. Does not migrate the schema or exhaustively validate indexes and foreign keys.- Specified by:
validateSchemain interfaceSession
-
count
Description copied from interface:SessionCounts stored related rows without loading the relationship's entities. Flushes pending changes when a transaction is active. Detached owners are identified by their persisted key; local detached relationship edits are ignored. -
isLoaded
Description copied from interface:SessionChecks relationship initialization without fetching its contents. -
initialize
Description copied from interface:SessionLoads a relationship if it is still uninitialized.- Specified by:
initializein interfaceSession- Parameters:
entity- relationship ownerfield- Java relationship name
-