Top Description Methods
org.hibernate

public Interface StatelessSession

extends SharedSessionContract
Known Direct Implementers
org.hibernate.internal.StatelessSessionImpl
Imports
jakarta.persistence.EntityGraph, org.hibernate.graph.GraphSemantic

A command-oriented API often used for performing bulk operations against the database. A stateless session has no persistence context, and always works directly with detached entity instances. When a method of this interface is called, any necessary interaction with the database happens immediately and synchronously.

Viewed in opposition to Session, the StatelessSession is a whole competing programming model, one preferred by some developers for its simplicity and somewhat lower level of abstraction. But the two kinds of session are not enemies, and may comfortably coexist in a single program.

A stateless session comes some with designed-in limitations:

Furthermore:

Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.

On the other hand, for certain kinds of transactions, a stateless session may perform slightly faster than a stateful session.

Author
Gavin King

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares org.hibernate.SharedSessionContract.close.

Close the stateless session and release the JDBC connection.
public void
delete(Object
a detached entity instance
entity
)

Delete a row.

public void
delete(String
The entityName for the entity to be deleted
entityName
,
Object
a detached entity instance
entity
)

Delete a row.

public void
fetch(Object
a lazy-loaded association
association
)

Fetch an association that's configured for lazy loading.

public Object

Returns:

a detached entity instance
get
(String
The name of the entity to retrieve
entityName
,
Object
The id of the entity to retrieve
id
)

Retrieve a row.

public <T> T

Returns:

a detached entity instance
get
(Class<T>
The class of the entity to retrieve
entityClass
,
Object
The id of the entity to retrieve
id
)

Retrieve a row.

public Object

Returns:

a detached entity instance
get
(String
The name of the entity to retrieve
entityName
,
Object
The id of the entity to retrieve
id
,
LockMode
The lock mode to apply to the entity
lockMode
)

Retrieve a row, obtaining the specified lock mode.

public <T> T

Returns:

a detached entity instance
get
(Class<T>
The class of the entity to retrieve
entityClass
,
Object
The id of the entity to retrieve
id
,
LockMode
The lock mode to apply to the entity
lockMode
)

Retrieve a row, obtaining the specified lock mode.

public <T> T

Returns:

a detached entity instance
get
(EntityGraph<T> graph, GraphSemantic
a GraphSemantic specifying how the graph should be interpreted
graphSemantic
,
Object
The id of the entity to retrieve
id
)

Retrieve a row, fetching associations specified by the given EntityGraph.

public <T> T

Returns:

a detached entity instance
get
(EntityGraph<T> graph, GraphSemantic
a GraphSemantic specifying how the graph should be interpreted
graphSemantic
,
Object
The id of the entity to retrieve
id
,
LockMode
The lock mode to apply to the entity
lockMode
)

Retrieve a row, fetching associations specified by the given EntityGraph, and obtaining the specified lock mode.

public Object

Returns:

The identifier of the inserted entity
insert
(Object
a new transient instance
entity
)

Insert a row.

public Object

Returns:

the identifier of the instance
insert
(String
The entityName for the entity to be inserted
entityName
,
Object
a new transient instance
entity
)

Insert a row.

public void
refresh(Object
The entity to be refreshed.
entity
)

Refresh the entity instance state from the database.

public void
refresh(String
The entityName for the entity to be refreshed.
entityName
,
Object
The entity to be refreshed.
entity
)

Refresh the entity instance state from the database.

public void
refresh(Object
The entity to be refreshed.
entity
,
LockMode
The LockMode to be applied.
lockMode
)

Refresh the entity instance state from the database.

public void
refresh(String
The entityName for the entity to be refreshed.
entityName
,
Object
The entity to be refreshed.
entity
,
LockMode
The LockMode to be applied.
lockMode
)

Refresh the entity instance state from the database.

public void
update(Object
a detached entity instance
entity
)

Update a row.

public void
update(String
The entityName for the entity to be updated
entityName
,
Object
a detached entity instance
entity
)

Update a row.

public void
upsert(Object
a detached entity instance
entity
)

Use a SQL merge into statement to perform an upsert.

public void
upsert(String
The entityName for the entity to be merged
entityName
,
Object
a detached entity instance
entity
)

Use a SQL merge into statement to perform an upsert.

Inherited from org.hibernate.SharedSessionContract:
beginTransactioncreateEntityGraphcreateEntityGraphcreateEntityGraphcreateNamedStoredProcedureQuerycreateStoredProcedureCallcreateStoredProcedureCallcreateStoredProcedureCallcreateStoredProcedureQuerycreateStoredProcedureQuerycreateStoredProcedureQuerydoReturningWorkdoWorkgetCriteriaBuildergetEntityGraphgetEntityGraphsgetFactorygetJdbcBatchSizegetNamedProcedureCallgetTenantIdentifiergetTenantIdentifierValuegetTransactionisConnectedisJoinedToTransactionisOpenjoinTransactionsetJdbcBatchSize

Method Detail

closeback to summary
public void close()

Redeclares org.hibernate.SharedSessionContract.close.

Close the stateless session and release the JDBC connection.

deleteback to summary
public void delete(Object entity)

Delete a row.

Parameters
entity:Object

a detached entity instance

deleteback to summary
public void delete(String entityName, Object entity)

Delete a row.

Parameters
entityName:String

The entityName for the entity to be deleted

entity:Object

a detached entity instance

fetchback to summary
public void fetch(Object association)

Fetch an association that's configured for lazy loading.

Warning

this operation in a stateless session is quite sensitive to data aliasing effects and should be used with great care.

Parameters
association:Object

a lazy-loaded association

Since
6.0
See Also
org.hibernate.Hibernate#initialize(Object)
getback to summary
public Object get(String entityName, Object id)

Retrieve a row.

Parameters
entityName:String

The name of the entity to retrieve

id:Object

The id of the entity to retrieve

Returns:Object

a detached entity instance

getback to summary
public <T> T get(Class<T> entityClass, Object id)

Retrieve a row.

Parameters
entityClass:Class<T>

The class of the entity to retrieve

id:Object

The id of the entity to retrieve

Returns:T

a detached entity instance

getback to summary
public Object get(String entityName, Object id, LockMode lockMode)

Retrieve a row, obtaining the specified lock mode.

Parameters
entityName:String

The name of the entity to retrieve

id:Object

The id of the entity to retrieve

lockMode:LockMode

The lock mode to apply to the entity

Returns:Object

a detached entity instance

getback to summary
public <T> T get(Class<T> entityClass, Object id, LockMode lockMode)

Retrieve a row, obtaining the specified lock mode.

Parameters
entityClass:Class<T>

The class of the entity to retrieve

id:Object

The id of the entity to retrieve

lockMode:LockMode

The lock mode to apply to the entity

Returns:T

a detached entity instance

getback to summary
public <T> T get(EntityGraph<T> graph, GraphSemantic graphSemantic, Object id)

Retrieve a row, fetching associations specified by the given EntityGraph.

Parameters
graph:EntityGraph<T>

The EntityGraph

graphSemantic:GraphSemantic

a GraphSemantic specifying how the graph should be interpreted

id:Object

The id of the entity to retrieve

Returns:T

a detached entity instance

Since
6.3
getback to summary
public <T> T get(EntityGraph<T> graph, GraphSemantic graphSemantic, Object id, LockMode lockMode)

Retrieve a row, fetching associations specified by the given EntityGraph, and obtaining the specified lock mode.

Parameters
graph:EntityGraph<T>

The EntityGraph

graphSemantic:GraphSemantic

a GraphSemantic specifying how the graph should be interpreted

id:Object

The id of the entity to retrieve

lockMode:LockMode

The lock mode to apply to the entity

Returns:T

a detached entity instance

Since
6.3
insertback to summary
public Object insert(Object entity)

Insert a row.

Parameters
entity:Object

a new transient instance

Returns:Object

The identifier of the inserted entity

insertback to summary
public Object insert(String entityName, Object entity)

Insert a row.

Parameters
entityName:String

The entityName for the entity to be inserted

entity:Object

a new transient instance

Returns:Object

the identifier of the instance

refreshback to summary
public void refresh(Object entity)

Refresh the entity instance state from the database.

Parameters
entity:Object

The entity to be refreshed.

refreshback to summary
public void refresh(String entityName, Object entity)

Refresh the entity instance state from the database.

Parameters
entityName:String

The entityName for the entity to be refreshed.

entity:Object

The entity to be refreshed.

refreshback to summary
public void refresh(Object entity, LockMode lockMode)

Refresh the entity instance state from the database.

Parameters
entity:Object

The entity to be refreshed.

lockMode:LockMode

The LockMode to be applied.

refreshback to summary
public void refresh(String entityName, Object entity, LockMode lockMode)

Refresh the entity instance state from the database.

Parameters
entityName:String

The entityName for the entity to be refreshed.

entity:Object

The entity to be refreshed.

lockMode:LockMode

The LockMode to be applied.

updateback to summary
public void update(Object entity)

Update a row.

Parameters
entity:Object

a detached entity instance

updateback to summary
public void update(String entityName, Object entity)

Update a row.

Parameters
entityName:String

The entityName for the entity to be updated

entity:Object

a detached entity instance

upsertback to summary
public void upsert(Object entity)

Use a SQL merge into statement to perform an upsert.

Parameters
entity:Object

a detached entity instance

Annotations
@Incubating
Exceptions
TransientObjectException:
is the entity is transient
Since
6.3
upsertback to summary
public void upsert(String entityName, Object entity)

Use a SQL merge into statement to perform an upsert.

Parameters
entityName:String

The entityName for the entity to be merged

entity:Object

a detached entity instance

Annotations
@Incubating
Exceptions
TransientObjectException:
is the entity is transient
Since
6.3