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
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
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 a mapped table or column is incompatible or inaccessible
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