public static interface Stage.Session extends AutoCloseable
org.hibernate.Session
interface, allowing a reactive style of interaction with the database. The semantics of operations on this interface are identical to the semantics of the similarly-named operations of Session
, except that the operations are performed asynchronously, returning a CompletionStage
without blocking the calling thread.
Entities associated with an Session
do not support transparent lazy association fetching. Instead, fetch(Object)
should be used to explicitly request asynchronous fetching of an association, or the association should be fetched eagerly when the entity is first retrieved, for example, by:
enabling a fetch profile
, EntityGraph
, or join fetch
clause in a HQL query. org.hibernate.Session
Modifier and Type | Method and Description |
---|---|
Stage.Session | clear() Completely clear the session. |
void | close() Close the reactive session and release the underlying database connection. |
boolean | contains(Object entity) Determine if the given instance belongs to this persistence context. |
<T> <any> | createEntityGraph(Class<T> rootType) Create a new mutable EntityGraph |
<T> <any> | createEntityGraph(Class<T> rootType, String graphName) Create a new mutable copy of a named EntityGraph |
<R> Stage.Query<R> | createNamedQuery(String queryName) Create an instance of Stage.Query for the named query. |
<R> Stage.Query<R> | createNamedQuery(String queryName, Class<R> resultType) Create an instance of Stage.Query for the named query. |
<R> Stage.Query<R> | createNativeQuery(String queryString) Create an instance of Stage.Query for the given SQL query string, or SQL update, insert, or delete statement. |
<R> Stage.Query<R> | createNativeQuery(String queryString, Class<R> resultType) Create an instance of Stage.Query for the given SQL query string, using the given resultType to interpret the results. |
<R> Stage.Query<R> | createNativeQuery(String queryString, ResultSetMapping<R> resultSetMapping) Create an instance of Stage.Query for the given SQL query string, using the given ResultSetMapping to interpret the result set. |
<R> Stage.Query<R> | createQuery(<any> criteriaQuery) Create an instance of Stage.Query for the given criteria query. |
<R> Stage.Query<R> | createQuery(<any> criteriaUpdate) Create an instance of Stage.Query for the given criteria update. |
<R> Stage.Query<R> | createQuery(<any> criteriaDelete) Create an instance of Stage.Query for the given criteria delete. |
<R> Stage.Query<R> | createQuery(String queryString) Create an instance of Stage.Query for the given HQL/JPQL query string or HQL/JPQL update or delete statement. |
<R> Stage.Query<R> | createQuery(String queryString, Class<R> resultType) Create an instance of Stage.Query for the given HQL/JPQL query string. |
Stage.Session | detach(Object entity) Remove this instance from the session cache. |
Stage.Session | disableFetchProfile(String name) Disable a particular fetch profile on this session, or do nothing if the requested fetch profile is not enabled. |
void | disableFilter(String filterName) Disable the named filter for this session. |
Stage.Session | enableFetchProfile(String name) Enable a particular fetch profile on this session, or do nothing if requested fetch profile is already enabled. |
Filter | enableFilter(String filterName) Enable the named filter for this session. |
<E,T> CompletionStage<T> | fetch(E entity, <any> field) Fetch a lazy property of the given entity, identified by a JPA attribute metamodel . |
<T> CompletionStage<T> | fetch(T association) Asynchronously fetch an association that's configured for lazy loading. |
<T> CompletionStage<T> | find(<any> entityGraph, Object id) Asynchronously return the persistent instance with the given identifier of an entity class, using the given EntityGraph as a fetch plan. |
<T> CompletionStage<List<T>> | find(Class<T> entityClass, Object... ids) Asynchronously return the persistent instances of the given entity class with the given identifiers, or null if there is no such persistent instance. |
<T> CompletionStage<T> | find(Class<T> entityClass, Object id) Asynchronously return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. |
<T> CompletionStage<T> | find(Class<T> entityClass, Object id, LockMode lockMode) Asynchronously return the persistent instance of the given entity class with the given identifier, requesting the given LockMode . |
CompletionStage<Void> | flush() Force this session to flush asynchronously. |
Integer | getBatchSize() The session-level JDBC batch size, or null if it has not been overridden. |
CacheMode | getCacheMode() Get the current cache mode. |
Filter | getEnabledFilter(String filterName) Retrieve a currently enabled filter by name. |
<T> <any> | getEntityGraph(Class<T> rootType, String graphName) Obtain a named EntityGraph |
FlushMode | getFlushMode() Get the current flush mode for this session. |
LockMode | getLockMode(Object entity) Determine the current lock mode of the given entity. |
<T> T | getReference(Class<T> entityClass, Object id) Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. |
<T> T | getReference(T entity) Return the persistent instance of with the same identity as the given instance, which might be detached, assuming that the instance is still persistent in the database. |
<T> ResultSetMapping<T> | getResultSetMapping(Class<T> resultType, String mappingName) Obtain a native SQL result set mapping defined via the annotation javax.persistence.SqlResultSetMapping . |
boolean | isDefaultReadOnly() |
boolean | isFetchProfileEnabled(String name) Determine if the fetch profile with the given name is enabled for this session. |
boolean | isOpen() |
boolean | isReadOnly(Object entityOrProxy) Is the specified entity or proxy read-only? |
CompletionStage<Void> | lock(Object entity, LockMode lockMode) Obtain the specified lock level upon the given object. |
<T> CompletionStage<Void> | merge(T... entities) Merge multiple entity instances at once. |
<T> CompletionStage<T> | merge(T entity) Copy the state of the given object onto the persistent instance with the same identifier. |
CompletionStage<Void> | persist(Object... entities) Persist multiple transient entity instances at once. |
CompletionStage<Void> | persist(Object entity) Asynchronously persist the given transient instance, first assigning a generated identifier. |
CompletionStage<Void> | refresh(Object... entities) Refresh multiple entity instances at once. |
CompletionStage<Void> | refresh(Object entity) Re-read the state of the given instance from the underlying database. |
CompletionStage<Void> | refresh(Object entity, LockMode lockMode) Re-read the state of the given instance from the underlying database, requesting the given LockMode . |
CompletionStage<Void> | remove(Object... entities) Remove multiple entity instances at once. |
CompletionStage<Void> | remove(Object entity) Asynchronously remove a persistent instance from the datastore. |
Stage.Session | setBatchSize(Integer batchSize) Set the session-level JDBC batch size, overriding the batch size set by the configuration property hibernate.jdbc.batch_size . |
Stage.Session | setCacheMode(CacheMode cacheMode) Set the cache mode for this session. |
Stage.Session | setDefaultReadOnly(boolean readOnly) Change the default for entities and proxies loaded into this session from modifiable to read-only mode, or from modifiable to read-only mode. |
Stage.Session | setFlushMode(FlushMode flushMode) Set the flush mode for this session. |
Stage.Session | setReadOnly(Object entityOrProxy, boolean readOnly) Set an unmodified persistent object to read-only mode, or a read-only object to modifiable mode. |
<T> CompletionStage<T> | unproxy(T association) Asynchronously fetch an association that's configured for lazy loading, and unwrap the underlying entity implementation from any proxy. |
<T> CompletionStage<T> | withTransaction(Function<Stage.Transaction,CompletionStage<T>> work) Performs the given work within the scope of a database transaction, automatically flushing the session. |
<T> CompletionStage<T> find(Class<T> entityClass, Object id)
null
if there is no such persistent instance. If the instance is already associated with the session, return the associated instance. This method never returns an uninitialized instance.
session.find(Book.class, id).thenAccept(book -> print(book.getTitle()));
entityClass
- The entity typeid
- an identifierCompletionStage
javax.persistence.EntityManager#find(Class, Object)
<T> CompletionStage<T> find(Class<T> entityClass, Object id, LockMode lockMode)
LockMode
.entityClass
- The entity typeid
- an identifierlockMode
- the requested LockMode
CompletionStage
find(Class,Object)
, this discussion of lock modes
<T> CompletionStage<T> find(<any> entityGraph, Object id)
EntityGraph
as a fetch plan.entityGraph
- an EntityGraph
specifying the entity and associations to be fetchedid
- an identifierfind(Class,Object)
<T> CompletionStage<List<T>> find(Class<T> entityClass, Object... ids)
entityClass
- The entity typeids
- the identifiersCompletionStage
<T> T getReference(Class<T> entityClass, Object id)
fetch(Object)
. You should not use this method to determine if an instance exists (use find(java.lang.Class<T>, java.lang.Object)
instead). Use this only to retrieve an instance which you safely assume exists, where non-existence would be an actual error.
entityClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classjavax.persistence.EntityManager#getReference(Class, Object)
<T> T getReference(T entity)
fetch(Object)
.entity
- a detached persistent instanceCompletionStage<Void> persist(Object entity)
This operation cascades to associated instances if the association is mapped with javax.persistence.CascadeType#PERSIST
.
session.persist(newBook).thenAccept(v -> session.flush());
entity
- a transient instance of a persistent classjavax.persistence.EntityManager#persist(Object)
CompletionStage<Void> persist(Object... entities)
persist(Object)
CompletionStage<Void> remove(Object entity)
This operation cascades to associated instances if the association is mapped with javax.persistence.CascadeType#REMOVE
.
session.delete(book).thenAccept(v -> session.flush());
entity
- the managed persistent instance to be removedIllegalArgumentException
- if the given instance is not managedjavax.persistence.EntityManager#remove(Object)
CompletionStage<Void> remove(Object... entities)
remove(Object)
<T> CompletionStage<T> merge(T entity)
This operation cascades to associated instances if the association is mapped with javax.persistence.CascadeType#MERGE
.
entity
- a detached instance with state to be copiedjavax.persistence.EntityManager#merge(Object)
<T> CompletionStage<Void> merge(T... entities)
merge(Object)
CompletionStage<Void> refresh(Object entity)
entity
- a managed persistent instanceIllegalArgumentException
- if the given instance is not managedjavax.persistence.EntityManager#refresh(Object)
CompletionStage<Void> refresh(Object entity, LockMode lockMode)
LockMode
.entity
- a managed persistent entity instancelockMode
- the requested lock moderefresh(Object)
CompletionStage<Void> refresh(Object... entities)
refresh(Object)
CompletionStage<Void> lock(Object entity, LockMode lockMode)
LockMode#PESSIMISTIC_READ
, LockMode#PESSIMISTIC_WRITE
, LockMode#PESSIMISTIC_FORCE_INCREMENT
, LockMode#OPTIMISTIC
, or LockMode#OPTIMISTIC_FORCE_INCREMENT
. org.hibernate.annotations.CascadeType#LOCK
.entity
- a managed persistent instancelockMode
- the lock levelIllegalArgumentException
- if the given instance is not managedCompletionStage<Void> flush()
session.flush().thenAccept(v -> print("done saving changes"));
javax.persistence.EntityManager#flush()
<T> CompletionStage<T> fetch(T association)
session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))
association
- a lazy-loaded associationCompletionStage
Stage.fetch(Object)
, org.hibernate.Hibernate#initialize(Object)
<E,T> CompletionStage<T> fetch(E entity, <any> field)
attribute metamodel
. Note that this feature is only supported in conjunction with the Hibernate bytecode enhancer.
session.fetch(book, Book_.isbn).thenAccept(isbn -> print(isbn))
<T> CompletionStage<T> unproxy(T association)
session.unproxy(author.getBook()).thenAccept(book -> print(book.getTitle()));
association
- a lazy-loaded associationCompletionStage
org.hibernate.Hibernate#unproxy(Object)
LockMode getLockMode(Object entity)
boolean contains(Object entity)
<R> Stage.Query<R> createQuery(String queryString)
Stage.Query
for the given HQL/JPQL query string or HQL/JPQL update or delete statement. In the case of an update or delete, the returned Stage.Query
must be executed using Stage.Query.executeUpdate()
which returns an affected row count.queryString
- The HQL/JPQL query, update or delete statementStage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String)
<R> Stage.Query<R> createQuery(String queryString, Class<R> resultType)
Stage.Query
for the given HQL/JPQL query string.queryString
- The HQL/JPQL queryresultType
- the Java type returned in each row of query resultsStage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String, Class)
<R> Stage.Query<R> createNamedQuery(String queryName)
Stage.Query
for the named query.queryName
- The name of the queryStage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String)
<R> Stage.Query<R> createNamedQuery(String queryName, Class<R> resultType)
Stage.Query
for the named query.queryName
- The name of the queryresultType
- the Java type returned in each row of query resultsStage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String, Class)
<R> Stage.Query<R> createNativeQuery(String queryString, Class<R> resultType)
Stage.Query
for the given SQL query string, using the given resultType
to interpret the results. Object
, or a built-in type such as String
or Integer
, the result set must have a single column, which will be returned as a scalar.Object[]
, then the result set must have multiple columns, which will be returned as elements of arrays of type Object[]
.queryString
- The SQL queryresultType
- the Java type returned in each row of query resultsStage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createNativeQuery(String, Class)
<R> Stage.Query<R> createNativeQuery(String queryString, ResultSetMapping<R> resultSetMapping)
Stage.Query
for the given SQL query string, using the given ResultSetMapping
to interpret the result set.queryString
- The SQL queryresultSetMapping
- the result set mappingStage.Query
instance for manipulation and executiongetResultSetMapping(Class, String)
, javax.persistence.EntityManager#createNativeQuery(String, String)
<R> Stage.Query<R> createNativeQuery(String queryString)
Stage.Query
for the given SQL query string, or SQL update, insert, or delete statement. In the case of an update, insert or delete, the returned Stage.Query
must be executed using Stage.Query.executeUpdate()
which returns an affected row count. In the case of a query: Object[]
.queryString
- The SQL select, update, insert, or delete statement<R> Stage.Query<R> createQuery(<any> criteriaQuery)
Stage.Query
for the given criteria query.criteriaQuery
- The CriteriaQuery
Stage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String)
<R> Stage.Query<R> createQuery(<any> criteriaUpdate)
Stage.Query
for the given criteria update.criteriaUpdate
- The CriteriaUpdate
Stage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String)
<R> Stage.Query<R> createQuery(<any> criteriaDelete)
Stage.Query
for the given criteria delete.criteriaDelete
- The CriteriaDelete
Stage.Query
instance for manipulation and executionjavax.persistence.EntityManager#createQuery(String)
Stage.Session setFlushMode(FlushMode flushMode)
flush mode
for this session. The flush mode determines the points at which the session is flushed. Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
For a logically "read only" session, it is reasonable to set the session's flush mode to FlushMode#MANUAL
at the start of the session (in order to achieve some extra performance).
flushMode
- the new flush modeFlushMode getFlushMode()
Stage.Session detach(Object entity)
This operation cascades to associated instances if the association is mapped with javax.persistence.CascadeType#DETACH
.
entity
- The entity to evictNullPointerException
- if the passed object is null
IllegalArgumentException
- if the passed object is not defined as an entityjavax.persistence.EntityManager#detach(Object)
Stage.Session clear()
javax.persistence.EntityManager#clear()
Stage.Session enableFetchProfile(String name)
name
- The name of the fetch profile to be enabled.org.hibernate.UnknownProfileException
- Indicates that the given name does not match any known profile namesfor discussion of this feature
<T> ResultSetMapping<T> getResultSetMapping(Class<T> resultType, String mappingName)
javax.persistence.SqlResultSetMapping
.<T> <any> getEntityGraph(Class<T> rootType, String graphName)
EntityGraph
<T> <any> createEntityGraph(Class<T> rootType)
EntityGraph
<T> <any> createEntityGraph(Class<T> rootType, String graphName)
EntityGraph
Stage.Session disableFetchProfile(String name)
name
- The name of the fetch profile to be disabled.org.hibernate.UnknownProfileException
- Indicates that the given name does not match any known profile namesfor discussion of this feature
boolean isFetchProfileEnabled(String name)
name
- The name of the profile to be checked.org.hibernate.UnknownProfileException
- Indicates that the given name does not match any known profile namesfor discussion of this feature
Stage.Session setDefaultReadOnly(boolean readOnly)
Read-only entities are not dirty-checked and snapshots of persistent state are not maintained. Read-only entities can be modified, but changes are not persisted.
org.hibernate.Session#setDefaultReadOnly(boolean)
boolean isDefaultReadOnly()
Stage.Session setReadOnly(Object entityOrProxy, boolean readOnly)
org.hibernate.Session#setReadOnly(Object, boolean)
boolean isReadOnly(Object entityOrProxy)
org.hibernate.Session#isReadOnly(Object)
Stage.Session setCacheMode(CacheMode cacheMode)
cache mode
for this session. The cache mode determines the manner in which this session interacts with the second level cache.
cacheMode
- The new cache mode.CacheMode getCacheMode()
Stage.Session setBatchSize(Integer batchSize)
hibernate.jdbc.batch_size
.Integer getBatchSize()
null
if it has not been overridden.Filter enableFilter(String filterName)
filterName
- The name of the filter to be enabled.void disableFilter(String filterName)
filterName
- The name of the filter to be disabled.Filter getEnabledFilter(String filterName)
filterName
- The name of the filter to be retrieved.<T> CompletionStage<T> withTransaction(Function<Stage.Transaction,CompletionStage<T>> work)
Stage.Transaction.markForRollback()
is called.work
- a function which accepts Stage.Transaction
and returns the result of the work as a CompletionStage
.void close()
close
in interface AutoCloseable
boolean isOpen()
close()
has been called