public static interface Stage.StatelessSession extends Stage.Closeable
StatelessSession
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()
or Stage.Session.detach(Object)
to perform
first-level cache management, and
Stage.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.
StatelessSession
Modifier and Type | Method and Description |
---|---|
CompletionStage<Void> |
close()
Close the reactive session and release the underlying database
connection.
|
<T> EntityGraph<T> |
createEntityGraph(Class<T> rootType)
Create a new mutable
EntityGraph |
<T> EntityGraph<T> |
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(CriteriaDelete<R> criteriaDelete)
Create an instance of
Stage.Query for the given criteria delete. |
<R> Stage.Query<R> |
createQuery(CriteriaQuery<R> criteriaQuery)
Create an instance of
Stage.Query for the given criteria query. |
<R> Stage.Query<R> |
createQuery(CriteriaUpdate<R> criteriaUpdate)
Create an instance of
Stage.Query for the given criteria update. |
<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.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... entities)
Delete multiple rows.
|
CompletionStage<Void> |
delete(Object entity)
Delete a row.
|
<T> CompletionStage<T> |
fetch(T association)
Asynchronously fetch an association that's configured for lazy loading.
|
<T> CompletionStage<T> |
get(Class<T> entityClass,
Object id)
Retrieve a row.
|
<T> CompletionStage<T> |
get(Class<T> entityClass,
Object id,
org.hibernate.LockMode lockMode)
Retrieve a row, obtaining the specified lock mode.
|
<T> CompletionStage<T> |
get(EntityGraph<T> entityGraph,
Object id)
Retrieve a row, using the given
EntityGraph as a fetch plan. |
<T> EntityGraph<T> |
getEntityGraph(Class<T> rootType,
String graphName)
Obtain a named
EntityGraph |
<T> ResultSetMapping<T> |
getResultSetMapping(Class<T> resultType,
String mappingName)
Obtain a native SQL result set mapping defined via the annotation
SqlResultSetMapping . |
CompletionStage<Void> |
insert(int batchSize,
Object... entities)
Insert multiple rows.
|
CompletionStage<Void> |
insert(Object... entities)
Insert multiple rows.
|
CompletionStage<Void> |
insert(Object entity)
Insert a row.
|
boolean |
isOpen() |
CompletionStage<Void> |
refresh(int batchSize,
Object... entities)
Refresh the entity instance state from the database.
|
CompletionStage<Void> |
refresh(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 entity,
org.hibernate.LockMode lockMode)
Refresh the entity instance state from the database.
|
CompletionStage<Void> |
update(int batchSize,
Object... entities)
Update multiple rows.
|
CompletionStage<Void> |
update(Object... entities)
Update multiple rows.
|
CompletionStage<Void> |
update(Object entity)
Update a row.
|
<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> get(Class<T> entityClass, Object id)
entityClass
- The class of the entity to retrieveid
- The id of the entity to retrieveCompletionStage
StatelessSession.get(Class, Serializable)
<T> CompletionStage<T> get(Class<T> entityClass, Object id, org.hibernate.LockMode lockMode)
entityClass
- The class of the entity to retrieveid
- The id of the entity to retrievelockMode
- The lock mode to apply to the entityCompletionStage
StatelessSession.get(Class, Serializable, LockMode)
<T> CompletionStage<T> get(EntityGraph<T> entityGraph, Object id)
EntityGraph
as a fetch plan.entityGraph
- an EntityGraph
specifying the entity
and associations to be fetchedid
- The id of the entity to retrieveCompletionStage
<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 executionStage.Session.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 executionStage.Session.createQuery(String, Class)
<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.queryString
- The SQL select, update, insert, or delete statementStage.Session.createNativeQuery(String)
<R> Stage.Query<R> createNamedQuery(String queryName)
Stage.Query
for the named query.queryName
- The name of the queryStage.Query
instance for manipulation and executionEntityManager.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 executionEntityManager.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.queryString
- The SQL queryresultType
- the Java type returned in each row of query resultsStage.Query
instance for manipulation and executionStage.Session.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 executionStage.Session.createNativeQuery(String, ResultSetMapping)
<R> Stage.Query<R> createQuery(CriteriaQuery<R> criteriaQuery)
Stage.Query
for the given criteria query.criteriaQuery
- The CriteriaQuery
Stage.Query
instance for manipulation and executionEntityManager.createQuery(String)
<R> Stage.Query<R> createQuery(CriteriaUpdate<R> criteriaUpdate)
Stage.Query
for the given criteria update.criteriaUpdate
- The CriteriaUpdate
Stage.Query
instance for manipulation and executionEntityManager.createQuery(String)
<R> Stage.Query<R> createQuery(CriteriaDelete<R> criteriaDelete)
Stage.Query
for the given criteria delete.criteriaDelete
- The CriteriaDelete
Stage.Query
instance for manipulation and executionEntityManager.createQuery(String)
CompletionStage<Void> insert(Object entity)
entity
- a new transient instanceStatelessSession.insert(Object)
CompletionStage<Void> insert(Object... entities)
entities
- new transient instancesStatelessSession.insert(Object)
CompletionStage<Void> insert(int batchSize, Object... entities)
batchSize
- the batch sizeentities
- new transient instancesStatelessSession.insert(Object)
CompletionStage<Void> delete(Object entity)
entity
- a detached entity instanceStatelessSession.delete(Object)
CompletionStage<Void> delete(Object... entities)
entities
- detached entity instancesStatelessSession.delete(Object)
CompletionStage<Void> delete(int batchSize, Object... entities)
batchSize
- the batch sizeentities
- detached entity instancesStatelessSession.delete(Object)
CompletionStage<Void> update(Object entity)
entity
- a detached entity instanceStatelessSession.update(Object)
CompletionStage<Void> update(Object... entities)
entities
- a detached entity instanceStatelessSession.update(Object)
CompletionStage<Void> update(int batchSize, Object... entities)
batchSize
- the batch sizeentities
- a detached entity instanceStatelessSession.update(Object)
CompletionStage<Void> refresh(Object entity)
entity
- The entity to be refreshed.StatelessSession.refresh(Object)
CompletionStage<Void> refresh(Object... entities)
entities
- The entities to be refreshed.StatelessSession.refresh(Object)
CompletionStage<Void> refresh(int batchSize, Object... entities)
batchSize
- the batch sizeentities
- The entities to be refreshed.StatelessSession.refresh(Object)
CompletionStage<Void> refresh(Object entity, org.hibernate.LockMode lockMode)
entity
- The entity to be refreshed.lockMode
- The LockMode to be applied.StatelessSession.refresh(Object, LockMode)
<T> CompletionStage<T> fetch(T association)
session.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()))
Warning: this operation in a stateless session is quite sensitive to
data aliasing effects and should be used with great care.association
- a lazy-loaded associationCompletionStage
Hibernate.initialize(Object)
<T> ResultSetMapping<T> getResultSetMapping(Class<T> resultType, String mappingName)
SqlResultSetMapping
.<T> EntityGraph<T> getEntityGraph(Class<T> rootType, String graphName)
EntityGraph
<T> EntityGraph<T> createEntityGraph(Class<T> rootType)
EntityGraph
<T> EntityGraph<T> createEntityGraph(Class<T> rootType, String graphName)
EntityGraph
<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
.Stage.SessionFactory.withTransaction(BiFunction)
Stage.Transaction currentTransaction()
Stage.Transaction
, or null if no transaction
was started using withTransaction(Function)
.withTransaction(Function)
,
Stage.SessionFactory.withTransaction(BiFunction)
boolean isOpen()
close()
has been calledCompletionStage<Void> close()
close
in interface Stage.Closeable
Copyright © 2020-2022 Red Hat, Inc. All Rights Reserved.