Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public interface Session
A persistence context obtained from an entity manager’s openSession() method.
Tracks changes to managed entities and maintains one instance per entity identity.
Sessions and their queries are not thread-safe; use and complete transactions on
one thread. Call close() when finished; it never commits pending work.
Writes require an explicit transaction. Queries flush pending changes when a
transaction is active. A failed flush marks that transaction rollback-only;
call rollbackTransaction() before continuing. Rollback detaches all entities
but does not restore the Java objects’ previous field values.
Lazy relationships require an open session that still manages their owner. Load relationships before detaching or serializing if they are needed later. Implementations are supplied by the ORM; applications should not implement this interface.
Methods
public abstract <T> JpqlQuery<T> createQuery(String statement, Class<T> resultType) | Creates a typed query using the supported JPQL subset. |
public abstract JpqlQuery<Object> createQuery(String statement) | Creates an untyped JPQL query, including bulk update or delete statements. |
public abstract <T> JpqlQuery<T> createNativeQuery(String statement, Class<T> resultType) | Creates a native SQL query with named parameters. |
public abstract void beginTransaction() | Starts a transaction for this session. |
public abstract void commitTransaction() | Flushes pending changes and commits the active transaction. |
public abstract void rollbackTransaction() | Rolls back the active transaction and detaches all managed entities. |
public abstract boolean isTransactionActive() | Reports whether this session has an active transaction. |
public abstract boolean isRollbackOnly() | Reports whether a transaction failure prevents committing. |
public abstract boolean contains(Object entity) | Tests whether this session manages an entity that is not scheduled for removal. |
public abstract void detach(Object entity) | Stops tracking an instance, discarding its unflushed changes. |
public abstract void clear() | Detaches every entity and discards all unflushed changes. |
public abstract void close() | Rolls back any active transaction, detaches entities, and releases session resources. |
public abstract <T> T find(Class<T> type, Object id) | Finds an entity, reusing its managed instance when present. |
public abstract <T> T find(Class<T> type, Object id, LockMode mode) | Finds an entity and optionally locks its database row. |
public abstract void lock(Object entity, LockMode mode) | Applies a lock mode to a managed entity’s row. |
public abstract <T> void persist(T entity) | Makes a new entity managed and schedules its insertion at flush. |
public abstract <T> T merge(T entity) | Copies state into a managed instance, cascading through MERGE associations. |
public abstract void remove(Object entity) | Schedules a managed entity for deletion, cascading REMOVE associations. |
public abstract void refresh(Object entity) | Reloads a persisted managed instance, discarding its local changes. |
public abstract void flush() | Writes pending inserts, updates, relationship changes, and removals. |
public abstract <T> boolean increment(Class<T> type, Object id, String field, long amount) | Atomically adds to an integral counter in SQL after flushing pending changes. |
public abstract <T> Query<T> query(Class<T> type) | Creates a fluent entity query. |
public abstract void createTables() | Creates missing tables, indexes, and constraints for registered mappings. |
public abstract void validateSchema() | Checks mapped columns, type families, nullability, and primary keys. |
public abstract SchemaReport schemaReport() | Compares every registered mapping, its collection tables included, with the tables that already exist, and reports each difference as either a required migration or an unsupported mapping. |
public abstract long count(Object entity, String field) | Counts stored related rows without loading the relationship’s entities. |
public abstract boolean isLoaded(Object entity, String field) | Checks relationship initialization without fetching its contents. |
public abstract void initialize(Object entity, String field) | Loads a relationship if it is still uninitialized. |
Method details
createQuery
public abstract <T> JpqlQuery<T> createQuery(String statement, Class<T> resultType)Parameters
statementString- query using entity and Java attribute names
resultTypeClass<T>- expected entity or scalar type; use
Object[].classfor tuples <T>- result type
Returns
Throws
IllegalArgumentException- if syntax or a mapped name is unsupported
createQuery
public abstract JpqlQuery<Object> createQuery(String statement)Parameters
statementString- query using entity and Java attribute names
Returns
Object[] tuplesThrows
IllegalArgumentException- if syntax or a mapped name is unsupported
createNativeQuery
public abstract <T> JpqlQuery<T> createNativeQuery(String statement, Class<T> resultType)Parameters
statementString- one SELECT, UPDATE or DELETE statement
resultTypeClass<T>- scalar class, or Object.class for mutations
<T>- scalar result type
Returns
Throws
IllegalArgumentException- for unsupported query syntax or result types
beginTransaction
public abstract void beginTransaction()Throws
PersistenceException- if the session is closed, a transaction is already active, or the database cannot begin the transaction
commitTransaction
public abstract void commitTransaction()Throws
PersistenceException- if no usable transaction is active or commit fails
rollbackTransaction
public abstract void rollbackTransaction()Throws
PersistenceException- if no transaction is active or rollback fails
isTransactionActive
public abstract boolean isTransactionActive()Returns
isRollbackOnly
public abstract boolean isRollbackOnly()Returns
contains
public abstract boolean contains(Object entity)Parameters
entityObject- instance to test; null is allowed
Returns
detach
public abstract void detach(Object entity)Parameters
entityObject- instance to detach
clear
public abstract void clear()close
public abstract void close()Throws
PersistenceException- if rollback or resource release fails
find
public abstract <T> T find(Class<T> type, Object id)Parameters
typeClass<T>- mapped entity class
idObject- non-null scalar key, embedded key, or composite
Identifier <T>- entity type
Returns
Throws
IllegalArgumentException- if the identifier shape is invalid
PersistenceException- if no generated mapping exists for the entity type
find
public abstract <T> T find(Class<T> type, Object id, LockMode mode)Parameters
typeClass<T>- mapped entity class
idObject- entity identifier
modeLockMode- requested lock mode
<T>- entity type
Returns
Throws
UnsupportedOperationException- if the database does not support row locks
OptimisticLockException- if a managed version is stale
PersistenceException- if the required transaction is not active
lock
public abstract void lock(Object entity, LockMode mode)Parameters
entityObject- managed instance
modeLockMode- requested lock mode; pessimistic modes require an active transaction
Throws
UnsupportedOperationException- if the database does not support row locks
OptimisticLockException- if the row is missing or its version is stale
PersistenceException- if the entity is not managed or a transaction is required
persist
public abstract <T> void persist(T entity)Parameters
entityT- new mapped instance
<T>- entity type
Throws
PersistenceException- if no transaction is active or the instance cannot be persisted
merge
public abstract <T> T merge(T entity)Parameters
entityT- new or detached mapped instance
<T>- entity type
Returns
Throws
PersistenceException- if no transaction is active or merging fails
remove
public abstract void remove(Object entity)Parameters
entityObject- managed instance to remove
Throws
PersistenceException- if no transaction is active or the instance is not managed
refresh
public abstract void refresh(Object entity)Parameters
entityObject- persisted instance managed by this session
Throws
PersistenceException- if the instance cannot be refreshed
flush
public abstract void flush()Throws
OptimisticLockException- if a versioned row was changed or removed elsewhere
PersistenceException- if no usable transaction is active or a write fails
increment
public abstract <T> boolean increment(Class<T> type, Object id, String field, long amount)Parameters
typeClass<T>- mapped entity class
idObject- entity identifier
fieldString- Java name of an int or long field that is neither key nor version
amountlong- signed amount to add
<T>- entity type
Returns
Throws
IllegalArgumentException- if the field is not a supported counter
PersistenceException- if no usable transaction is active or the update fails
query
public abstract <T> Query<T> query(Class<T> type)Parameters
typeClass<T>- mapped entity class
<T>- entity type
Returns
Throws
PersistenceException- if no generated mapping exists for the entity type
createTables
public abstract void createTables()Throws
PersistenceException- if a transaction is active or schema creation fails
validateSchema
public abstract void validateSchema()Throws
PersistenceException- if
schemaReport()finds any difference, with that report as the message, or a mapped table is inaccessible
schemaReport
public abstract SchemaReport schemaReport()Returns
SchemaReport.isCompatible() when there are noneThrows
PersistenceException- if the catalog cannot be read
count
public abstract long count(Object entity, String field)Parameters
entityObject- owner with a persisted identifier
fieldString- Java name of the relationship or element collection
Returns
isLoaded
public abstract boolean isLoaded(Object entity, String field)Parameters
entityObject- mapped instance
fieldString- Java relationship name
Returns
initialize
public abstract void initialize(Object entity, String field)Parameters
entityObject- relationship owner
fieldString- Java relationship name
Throws
LazyInitializationException- if unloaded state belongs to a detached entity
PersistenceException- if the session is closed or fetching fails