Interface Stage.StatelessSession
-
- All Superinterfaces:
Stage.Closeable
- Enclosing interface:
- Stage
public static interface Stage.StatelessSession extends Stage.Closeable
A non-blocking counterpart to the HibernateStatelessSession
interface, which provides a command-oriented API for performing bulk operations against a database.A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Changes to many to many associations and element collections may not be made persistent in a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors.
For certain kinds of work, a stateless session may perform slightly better than a stateful session.
In particular, for a session which loads many entities, use of a
StatelessSession
alleviates the need to call:Stage.Session.clear()
orStage.Session.detach(Object)
to perform first-level cache management, andStage.Session.setCacheMode(CacheMode)
to bypass interaction with the second-level cache.
Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.
- See Also:
StatelessSession
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description CompletionStage<Void>
close()
Close the reactive session and release the underlying database connection.<T> jakarta.persistence.EntityGraph<T>
createEntityGraph(Class<T> rootType)
Create a new mutableEntityGraph
<T> jakarta.persistence.EntityGraph<T>
createEntityGraph(Class<T> rootType, String graphName)
Create a new mutable copy of a namedEntityGraph
Stage.MutationQuery
createMutationQuery(String queryString)
Create an instance ofStage.MutationQuery
for the given HQL/JPQL update or delete statement.<R> Stage.Query<R>
createNamedQuery(String queryName)
Create an instance ofStage.Query
for the named query.<R> Stage.SelectionQuery<R>
createNamedQuery(String queryName, Class<R> resultType)
Create an instance ofStage.Query
for the named query.<R> Stage.Query<R>
createNativeQuery(String queryString)
Create an instance ofStage.Query
for the given SQL query string, or SQL update, insert, or delete statement.<R> Stage.SelectionQuery<R>
createNativeQuery(String queryString, Class<R> resultType)
Create an instance ofStage.Query
for the given SQL query string, using the givenresultType
to interpret the results.<R> Stage.MutationQuery
createQuery(jakarta.persistence.criteria.CriteriaDelete<R> criteriaDelete)
Create an instance ofStage.MutationQuery
for the given criteria delete.<R> Stage.SelectionQuery<R>
createQuery(jakarta.persistence.criteria.CriteriaQuery<R> criteriaQuery)
Create an instance ofStage.SelectionQuery
for the given criteria query.<R> Stage.MutationQuery
createQuery(jakarta.persistence.criteria.CriteriaUpdate<R> criteriaUpdate)
Create an instance ofStage.MutationQuery
for the given criteria update.<R> Stage.Query<R>
createQuery(String queryString)
Deprecated.See explanation inQueryProducer.createSelectionQuery(String)
<R> Stage.SelectionQuery<R>
createQuery(String queryString, Class<R> resultType)
Create an instance ofStage.SelectionQuery
for the given HQL/JPQL query string and query result type.<R> Stage.SelectionQuery<R>
createSelectionQuery(String queryString, Class<R> resultType)
Create an instance ofStage.SelectionQuery
for the given HQL/JPQL query string.Stage.Transaction
currentTransaction()
Obtain the transaction currently associated with this session, if any.CompletionStage<Void>
delete(int batchSize, Object... entities)
Delete multiple rows.CompletionStage<Void>
delete(Object entity)
Delete a row.CompletionStage<Void>
delete(Object... entities)
Delete multiple rows.<T> CompletionStage<T>
fetch(T association)
Asynchronously fetch an association that's configured for lazy loading.<T> CompletionStage<T>
get(jakarta.persistence.EntityGraph<T> entityGraph, Object id)
Retrieve a row, using the givenEntityGraph
as a fetch plan.<T> CompletionStage<T>
get(Class<T> entityClass, Object id)
Retrieve a row.default <T> CompletionStage<T>
get(Class<T> entityClass, Object id, jakarta.persistence.LockModeType lockModeType)
Retrieve a row, obtaining the specified lock mode.<T> CompletionStage<T>
get(Class<T> entityClass, Object id, LockMode lockMode)
Retrieve a row, obtaining the specified lock mode.<T> jakarta.persistence.EntityGraph<T>
getEntityGraph(Class<T> rootType, String graphName)
Obtain a namedEntityGraph
Stage.SessionFactory
getFactory()
TheStage.SessionFactory
which created this session.<T> ResultSetMapping<T>
getResultSetMapping(Class<T> resultType, String mappingName)
Obtain a native SQL result set mapping defined via the annotationSqlResultSetMapping
.CompletionStage<Void>
insert(int batchSize, Object... entities)
Insert multiple rows.CompletionStage<Void>
insert(Object entity)
Insert a row.CompletionStage<Void>
insert(Object... entities)
Insert multiple rows.boolean
isOpen()
CompletionStage<Void>
refresh(int batchSize, Object... entities)
Refresh the entity instance state from the database.CompletionStage<Void>
refresh(Object entity)
Refresh the entity instance state from the database.CompletionStage<Void>
refresh(Object... entities)
Refresh the entity instance state from the database.default CompletionStage<Void>
refresh(Object entity, jakarta.persistence.LockModeType lockModeType)
Refresh the entity instance state from the database.CompletionStage<Void>
refresh(Object entity, LockMode lockMode)
Refresh the entity instance state from the database.CompletionStage<Void>
update(int batchSize, Object... entities)
Update multiple rows.CompletionStage<Void>
update(Object entity)
Update a row.CompletionStage<Void>
update(Object... entities)
Update multiple rows.CompletionStage<Void>
upsert(Object entity)
CompletionStage<Void>
upsert(String entityName, Object entity)
<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.
-
-
-
Method Detail
-
get
<T> CompletionStage<T> get(Class<T> entityClass, Object id)
Retrieve a row.- Parameters:
entityClass
- The class of the entity to retrieveid
- The id of the entity to retrieve- Returns:
- a detached entity instance, via a
CompletionStage
- See Also:
StatelessSession.get(Class, Object)
-
get
<T> CompletionStage<T> get(Class<T> entityClass, Object id, LockMode lockMode)
Retrieve a row, obtaining the specified lock mode.- Parameters:
entityClass
- The class of the entity to retrieveid
- The id of the entity to retrievelockMode
- The lock mode to apply to the entity- Returns:
- a detached entity instance, via a
CompletionStage
- See Also:
StatelessSession.get(Class, Object, LockMode)
-
get
default <T> CompletionStage<T> get(Class<T> entityClass, Object id, jakarta.persistence.LockModeType lockModeType)
Retrieve a row, obtaining the specified lock mode.- Parameters:
entityClass
- The class of the entity to retrieveid
- The id of the entity to retrievelockModeType
- The lock mode to apply to the entity- Returns:
- a detached entity instance, via a
CompletionStage
- See Also:
StatelessSession.get(Class, Object, LockMode)
-
get
<T> CompletionStage<T> get(jakarta.persistence.EntityGraph<T> entityGraph, Object id)
Retrieve a row, using the givenEntityGraph
as a fetch plan.- Parameters:
entityGraph
- anEntityGraph
specifying the entity and associations to be fetchedid
- The id of the entity to retrieve- Returns:
- a detached entity instance, via a
CompletionStage
-
createQuery
@Deprecated <R> Stage.Query<R> createQuery(String queryString)
Deprecated.See explanation inQueryProducer.createSelectionQuery(String)
Create an instance ofStage.Query
for the given HQL/JPQL query string or HQL/JPQL update or delete statement. In the case of an update or delete, the returnedStage.Query
must be executed usingStage.MutationQuery.executeUpdate()
which returns an affected row count.- Parameters:
queryString
- The HQL/JPQL query, update or delete statement- Returns:
- The
Stage.Query
instance for manipulation and execution - See Also:
Session.createQuery(String)
-
createQuery
<R> Stage.SelectionQuery<R> createQuery(String queryString, Class<R> resultType)
Create an instance ofStage.SelectionQuery
for the given HQL/JPQL query string and query result type.- Parameters:
queryString
- The HQL/JPQL queryresultType
- the Java type returned in each row of query results- Returns:
- The
Stage.Query
instance for manipulation and execution - See Also:
Session.createQuery(String, Class)
-
createSelectionQuery
<R> Stage.SelectionQuery<R> createSelectionQuery(String queryString, Class<R> resultType)
Create an instance ofStage.SelectionQuery
for the given HQL/JPQL query string.- Parameters:
queryString
- The HQL/JPQL query- Returns:
- The
Stage.SelectionQuery
instance for manipulation and execution - See Also:
EntityManager.createQuery(String, Class)
-
createMutationQuery
Stage.MutationQuery createMutationQuery(String queryString)
Create an instance ofStage.MutationQuery
for the given HQL/JPQL update or delete statement.- Parameters:
queryString
- The HQL/JPQL query, update or delete statement- Returns:
- The
Stage.MutationQuery
instance for manipulation and execution - See Also:
EntityManager.createQuery(String)
-
createNativeQuery
<R> Stage.Query<R> createNativeQuery(String queryString)
Create an instance ofStage.Query
for the given SQL query string, or SQL update, insert, or delete statement. In the case of an update, insert, or delete, the returnedStage.Query
must be executed usingStage.MutationQuery.executeUpdate()
which returns an affected row count.- Parameters:
queryString
- The SQL select, update, insert, or delete statement- See Also:
QueryProducer.createNativeQuery(String)
-
createNamedQuery
<R> Stage.Query<R> createNamedQuery(String queryName)
Create an instance ofStage.Query
for the named query.- Parameters:
queryName
- The name of the query- Returns:
- The
Stage.Query
instance for manipulation and execution - See Also:
EntityManager.createQuery(String)
-
createNamedQuery
<R> Stage.SelectionQuery<R> createNamedQuery(String queryName, Class<R> resultType)
Create an instance ofStage.Query
for the named query.- Parameters:
queryName
- The name of the queryresultType
- the Java type returned in each row of query results- Returns:
- The
Stage.Query
instance for manipulation and execution - See Also:
EntityManager.createQuery(String, Class)
-
createNativeQuery
<R> Stage.SelectionQuery<R> createNativeQuery(String queryString, Class<R> resultType)
Create an instance ofStage.Query
for the given SQL query string, using the givenresultType
to interpret the results.- Parameters:
queryString
- The SQL queryresultType
- the Java type returned in each row of query results- Returns:
- The
Stage.Query
instance for manipulation and execution - See Also:
QueryProducer.createNativeQuery(String, Class)
-
createQuery
<R> Stage.SelectionQuery<R> createQuery(jakarta.persistence.criteria.CriteriaQuery<R> criteriaQuery)
Create an instance ofStage.SelectionQuery
for the given criteria query.- Parameters:
criteriaQuery
- TheCriteriaQuery
- Returns:
- The
Stage.SelectionQuery
instance for manipulation and execution - See Also:
EntityManager.createQuery(String)
-
createQuery
<R> Stage.MutationQuery createQuery(jakarta.persistence.criteria.CriteriaUpdate<R> criteriaUpdate)
Create an instance ofStage.MutationQuery
for the given criteria update.- Parameters:
criteriaUpdate
- TheCriteriaUpdate
- Returns:
- The
Stage.MutationQuery
instance for manipulation and execution - See Also:
EntityManager.createQuery(String)
-
createQuery
<R> Stage.MutationQuery createQuery(jakarta.persistence.criteria.CriteriaDelete<R> criteriaDelete)
Create an instance ofStage.MutationQuery
for the given criteria delete.- Parameters:
criteriaDelete
- TheCriteriaDelete
- Returns:
- The
Stage.MutationQuery
instance for manipulation and execution - See Also:
EntityManager.createQuery(String)
-
insert
CompletionStage<Void> insert(Object entity)
Insert a row.- Parameters:
entity
- a new transient instance- See Also:
StatelessSession.insert(Object)
-
insert
CompletionStage<Void> insert(Object... entities)
Insert multiple rows.- Parameters:
entities
- new transient instances- See Also:
StatelessSession.insert(Object)
-
insert
CompletionStage<Void> insert(int batchSize, Object... entities)
Insert multiple rows.- Parameters:
batchSize
- the batch sizeentities
- new transient instances- See Also:
StatelessSession.insert(Object)
-
delete
CompletionStage<Void> delete(Object entity)
Delete a row.- Parameters:
entity
- a detached entity instance- See Also:
StatelessSession.delete(Object)
-
delete
CompletionStage<Void> delete(Object... entities)
Delete multiple rows.- Parameters:
entities
- detached entity instances- See Also:
StatelessSession.delete(Object)
-
delete
CompletionStage<Void> delete(int batchSize, Object... entities)
Delete multiple rows.- Parameters:
batchSize
- the batch sizeentities
- detached entity instances- See Also:
StatelessSession.delete(Object)
-
update
CompletionStage<Void> update(Object entity)
Update a row.- Parameters:
entity
- a detached entity instance- See Also:
StatelessSession.update(Object)
-
update
CompletionStage<Void> update(Object... entities)
Update multiple rows.- Parameters:
entities
- a detached entity instance- See Also:
StatelessSession.update(Object)
-
update
CompletionStage<Void> update(int batchSize, Object... entities)
Update multiple rows.- Parameters:
batchSize
- the batch sizeentities
- a detached entity instance- See Also:
StatelessSession.update(Object)
-
refresh
CompletionStage<Void> refresh(Object entity)
Refresh the entity instance state from the database.- Parameters:
entity
- The entity to be refreshed.- See Also:
StatelessSession.refresh(Object)
-
refresh
CompletionStage<Void> refresh(Object... entities)
Refresh the entity instance state from the database.- Parameters:
entities
- The entities to be refreshed.- See Also:
StatelessSession.refresh(Object)
-
refresh
CompletionStage<Void> refresh(int batchSize, Object... entities)
Refresh the entity instance state from the database.- Parameters:
batchSize
- the batch sizeentities
- The entities to be refreshed.- See Also:
StatelessSession.refresh(Object)
-
refresh
CompletionStage<Void> refresh(Object entity, LockMode lockMode)
Refresh the entity instance state from the database.- Parameters:
entity
- The entity to be refreshed.lockMode
- The LockMode to be applied.- See Also:
StatelessSession.refresh(Object, LockMode)
-
refresh
default CompletionStage<Void> refresh(Object entity, jakarta.persistence.LockModeType lockModeType)
Refresh the entity instance state from the database.- Parameters:
entity
- The entity to be refreshed.lockModeType
- The LockMode to be applied.- See Also:
StatelessSession.refresh(Object, LockMode)
-
upsert
CompletionStage<Void> upsert(Object entity)
- Parameters:
entity
- a detached entity instance- See Also:
StatelessSession.upsert(Object)
-
upsert
CompletionStage<Void> upsert(String entityName, Object entity)
- Parameters:
entityName
- The entityName for the entity to be mergedentity
- a detached entity instance- See Also:
StatelessSession.upsert(String, Object)
-
fetch
<T> CompletionStage<T> fetch(T association)
Asynchronously fetch an association that's configured for lazy loading.session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))
- Parameters:
association
- a lazy-loaded association- Returns:
- the fetched association, via a
CompletionStage
- See Also:
Hibernate.initialize(Object)
-
getResultSetMapping
<T> ResultSetMapping<T> getResultSetMapping(Class<T> resultType, String mappingName)
Obtain a native SQL result set mapping defined via the annotationSqlResultSetMapping
.
-
getEntityGraph
<T> jakarta.persistence.EntityGraph<T> getEntityGraph(Class<T> rootType, String graphName)
Obtain a namedEntityGraph
-
createEntityGraph
<T> jakarta.persistence.EntityGraph<T> createEntityGraph(Class<T> rootType)
Create a new mutableEntityGraph
-
createEntityGraph
<T> jakarta.persistence.EntityGraph<T> createEntityGraph(Class<T> rootType, String graphName)
Create a new mutable copy of a namedEntityGraph
-
withTransaction
<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. The transaction will be rolled back if the work completes with an uncaught exception, or ifStage.Transaction.markForRollback()
is called.- If there is already a transaction associated with this session, the work is executed in the context of the existing transaction, and no new transaction is initiated.
- If there is no transaction associated with this session, a new transaction is started, and the work is executed in the context of the new transaction.
- Parameters:
work
- a function which acceptsStage.Transaction
and returns the result of the work as aCompletionStage
.- See Also:
Stage.SessionFactory.withTransaction(BiFunction)
-
currentTransaction
Stage.Transaction currentTransaction()
Obtain the transaction currently associated with this session, if any.- Returns:
- the
Stage.Transaction
, or null if no transaction was started usingwithTransaction(Function)
. - See Also:
withTransaction(Function)
,Stage.SessionFactory.withTransaction(BiFunction)
-
isOpen
boolean isOpen()
- Returns:
- false if
close()
has been called
-
close
CompletionStage<Void> close()
Close the reactive session and release the underlying database connection.- Specified by:
close
in interfaceStage.Closeable
-
getFactory
Stage.SessionFactory getFactory()
TheStage.SessionFactory
which created this session.
-
-