Top Description Methods
org.springframework.orm.hibernate5

public Interface HibernateOperations

Known Direct Implementers
org.springframework.orm.hibernate5.HibernateTemplate
Imports
java.io.Serializable, java.util.Collection, .Iterator, .List, org.hibernate.Filter, .LockMode, .ReplicationMode, org.hibernate.criterion.DetachedCriteria, org.springframework.dao.DataAccessException, org.springframework.lang.Nullable

Interface that specifies a common set of Hibernate operations as well as a general execute method for Session-based lambda expressions. Implemented by HibernateTemplate. Not often used, but a useful option to enhance testability, as it can easily be mocked or stubbed.

Defines HibernateTemplate's data access methods that mirror various org.hibernate.Session methods. Users are strongly encouraged to read the Hibernate Session javadocs for details on the semantics of those methods.

A deprecation note: While HibernateTemplate and this operations interface are being kept around for backwards compatibility in terms of the data access implementation style in Spring applications, we strongly recommend the use of native org.hibernate.Session access code for non-trivial interactions. This in particular affects parameterized queries where - on Java 8+ - a custom HibernateCallback lambda code block with createQuery and several setParameter calls on the org.hibernate.query.Query interface is an elegant solution, to be executed via the general execute method. All such operations which benefit from a lambda variant have been marked as deprecated on this interface.

A Hibernate compatibility note: HibernateTemplate and the operations on this interface generally aim to be applicable across all Hibernate versions. In terms of binary compatibility, Spring ships a variant for each major generation of Hibernate (in the present case: Hibernate ORM 5.x). However, due to refactorings and removals in Hibernate ORM 5.3, some variants - in particular legacy positional parameters starting from index 0 - do not work anymore. All affected operations are marked as deprecated; please replace them with the general execute method and custom lambda blocks creating the queries, ideally setting named parameters through org.hibernate.query.Query. Please be aware that deprecated operations are known to work with Hibernate ORM 5.2 but may not work with Hibernate ORM 5.3 and higher anymore.

Author
Juergen Hoeller
Since
4.2
See Also
HibernateTemplate, org.hibernate.Session, HibernateTransactionManager

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the number of instances updated/deleted
bulkUpdate
(String
an update/delete query expressed in Hibernate's query language
queryString
,
Object...
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Update/delete all objects according to the given query, binding a number of values to "?" parameters in the query string.

public void
clear()

Remove all objects from the org.hibernate.Session cache, and cancel all pending saves, updates and deletes.

public void
closeIterator(Iterator<?>
the Iterator to close
it
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Immediately close an Iterator created by any of the various iterate(..) operations, instead of waiting until the session is closed or disconnected.

public boolean

Returns:

whether the given object is in the Session cache
contains
(Object
the persistence instance to check
entity
)

Check whether the given object is in the Session cache.

public void
delete(Object
the persistent instance to delete
entity
)

Delete the given persistent instance.

public void
delete(Object
the persistent instance to delete
entity
,
LockMode
the lock mode to obtain
lockMode
)

Delete the given persistent instance.

public void
delete(String
the name of the persistent entity
entityName
,
Object
the persistent instance to delete
entity
)

Delete the given persistent instance.

public void
delete(String
the name of the persistent entity
entityName
,
Object
the persistent instance to delete
entity
,
LockMode
the lock mode to obtain
lockMode
)

Delete the given persistent instance.

public void
deleteAll(Collection<?>
the persistent instances to delete
entities
)

Delete all given persistent instances.

public Filter

Returns:

the enabled Hibernate Filter (either already enabled or enabled on the fly by this operation)
enableFilter
(String
the name of the filter
filterName
)

Return an enabled Hibernate Filter for the given filter name.

public void
evict(Object
the persistent instance to evict
entity
)

Remove the given object from the org.hibernate.Session cache.

public <T> T

Returns:

a result object returned by the action, or null
execute
(HibernateCallback<T>
callback object that specifies the Hibernate action
action
)

Execute the action specified by the given action object within a org.hibernate.Session.

public List<?>

Returns:

a List containing the results of the query execution
find
(String
a query expressed in Hibernate's query language
queryString
,
Object...
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute an HQL query, binding a number of values to "?" parameters in the query string.

public List<?>

Returns:

a List containing 0 or more persistent instances
findByCriteria
(DetachedCriteria
the detached Hibernate criteria object.

Note

Do not reuse criteria objects! They need to recreated per execution, due to the suboptimal design of Hibernate's criteria facility.

criteria
)

Execute a query based on a given Hibernate criteria object.

public List<?>

Returns:

a List containing 0 or more persistent instances
findByCriteria
(DetachedCriteria
the detached Hibernate criteria object.

Note

Do not reuse criteria objects! They need to recreated per execution, due to the suboptimal design of Hibernate's criteria facility.

criteria
,
int
the index of the first result object to be retrieved (numbered from 0)
firstResult
,
int
the maximum number of result objects to retrieve (or <=0 for no limit)
maxResults
)

Execute a query based on the given Hibernate criteria object.

public <T> List<T>

Returns:

a List containing 0 or more persistent instances
findByExample
(T
an instance of the desired entity, serving as example for "query-by-example"
exampleEntity
)

Execute a query based on the given example entity object.

public <T> List<T>

Returns:

a List containing 0 or more persistent instances
findByExample
(String
the name of the persistent entity
entityName
,
T
an instance of the desired entity, serving as example for "query-by-example"
exampleEntity
)

Execute a query based on the given example entity object.

public <T> List<T>

Returns:

a List containing 0 or more persistent instances
findByExample
(T
an instance of the desired entity, serving as example for "query-by-example"
exampleEntity
,
int
the index of the first result object to be retrieved (numbered from 0)
firstResult
,
int
the maximum number of result objects to retrieve (or <=0 for no limit)
maxResults
)

Execute a query based on a given example entity object.

public <T> List<T>

Returns:

a List containing 0 or more persistent instances
findByExample
(String
the name of the persistent entity
entityName
,
T
an instance of the desired entity, serving as example for "query-by-example"
exampleEntity
,
int
the index of the first result object to be retrieved (numbered from 0)
firstResult
,
int
the maximum number of result objects to retrieve (or <=0 for no limit)
maxResults
)

Execute a query based on a given example entity object.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedParam
(String
a query expressed in Hibernate's query language
queryString
,
String
the name of the parameter
paramName
,
Object
the value of the parameter
value
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute an HQL query, binding one value to a ":" named parameter in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedParam
(String
a query expressed in Hibernate's query language
queryString
,
String[]
the names of the parameters
paramNames
,
Object[]
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute an HQL query, binding a number of values to ":" named parameters in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedQuery
(String
the name of a Hibernate query in a mapping file
queryName
,
Object...
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute a named query binding a number of values to "?" parameters in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedQueryAndNamedParam
(String
the name of a Hibernate query in a mapping file
queryName
,
String
the name of parameter
paramName
,
Object
the value of the parameter
value
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute a named query, binding one value to a ":" named parameter in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedQueryAndNamedParam
(String
the name of a Hibernate query in a mapping file
queryName
,
String[]
the names of the parameters
paramNames
,
Object[]
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute a named query, binding a number of values to ":" named parameters in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByNamedQueryAndValueBean
(String
the name of a Hibernate query in a mapping file
queryName
,
Object
the values of the parameters
valueBean
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute a named query, binding the properties of the given bean to ":" named parameters in the query string.

public List<?>

Returns:

a List containing the results of the query execution
findByValueBean
(String
a query expressed in Hibernate's query language
queryString
,
Object
the values of the parameters
valueBean
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute an HQL query, binding the properties of the given bean to named parameters in the query string.

public void
flush()

Flush all pending saves, updates and deletes to the database.

public <T> T

Returns:

the persistent instance, or null if not found
get
(Class<T>
a persistent class
entityClass
,
Serializable
the identifier of the persistent instance
id
)

Return the persistent instance of the given entity class with the given identifier, or null if not found.

public <T> T

Returns:

the persistent instance, or null if not found
get
(Class<T>
a persistent class
entityClass
,
Serializable
the identifier of the persistent instance
id
,
LockMode
the lock mode to obtain
lockMode
)

Return the persistent instance of the given entity class with the given identifier, or null if not found.

public Object

Returns:

the persistent instance, or null if not found
get
(String
the name of the persistent entity
entityName
,
Serializable
the identifier of the persistent instance
id
)

Return the persistent instance of the given entity class with the given identifier, or null if not found.

public Object

Returns:

the persistent instance, or null if not found
get
(String
the name of the persistent entity
entityName
,
Serializable
the identifier of the persistent instance
id
,
LockMode
the lock mode to obtain
lockMode
)

Return the persistent instance of the given entity class with the given identifier, or null if not found.

public void
initialize(Object
a proxy for a persistent object or a persistent collection
proxy
)

Force initialization of a Hibernate proxy or persistent collection.

public Iterator<?>

Returns:

an Iterator containing 0 or more persistent instances
iterate
(String
a query expressed in Hibernate's query language
queryString
,
Object...
the values of the parameters
values
)
Deprecated as of 5.0.4, in favor of a custom HibernateCallback lambda code block passed to the general execute method

Execute a query for persistent instances, binding a number of values to "?" parameters in the query string.

public <T> T

Returns:

the persistent instance
load
(Class<T>
a persistent class
entityClass
,
Serializable
the identifier of the persistent instance
id
)

Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.

public <T> T

Returns:

the persistent instance
load
(Class<T>
a persistent class
entityClass
,
Serializable
the identifier of the persistent instance
id
,
LockMode
the lock mode to obtain
lockMode
)

Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.

public Object

Returns:

the persistent instance
load
(String
the name of the persistent entity
entityName
,
Serializable
the identifier of the persistent instance
id
)

Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.

public Object

Returns:

the persistent instance
load
(String
the name of the persistent entity
entityName
,
Serializable
the identifier of the persistent instance
id
,
LockMode
the lock mode to obtain
lockMode
)

Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.

public void
load(Object
the object (of the target class) to load into
entity
,
Serializable
the identifier of the persistent instance
id
)

Load the persistent instance with the given identifier into the given object, throwing an exception if not found.

public <T> List<T>

Returns:

a List containing 0 or more persistent instances
loadAll
(Class<T>
a persistent class
entityClass
)

Return all persistent instances of the given entity class.

public void
lock(Object
the persistent instance to lock
entity
,
LockMode
the lock mode to obtain
lockMode
)

Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.

public void
lock(String
the name of the persistent entity
entityName
,
Object
the persistent instance to lock
entity
,
LockMode
the lock mode to obtain
lockMode
)

Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.

public <T> T

Returns:

the updated, registered persistent instance
merge
(T
the object to merge with the corresponding persistence instance
entity
)

Copy the state of the given object onto the persistent object with the same identifier.

public <T> T

Returns:

the updated, registered persistent instance
merge
(String
the name of the persistent entity
entityName
,
T
the object to merge with the corresponding persistence instance
entity
)

Copy the state of the given object onto the persistent object with the same identifier.

public void
persist(Object
the persistent instance to persist
entity
)

Persist the given transient instance.

public void
persist(String
the name of the persistent entity
entityName
,
Object
the persistent instance to persist
entity
)

Persist the given transient instance.

public void
refresh(Object
the persistent instance to re-read
entity
)

Re-read the state of the given persistent instance.

public void
refresh(Object
the persistent instance to re-read
entity
,
LockMode
the lock mode to obtain
lockMode
)

Re-read the state of the given persistent instance.

public void
replicate(Object
the persistent object to replicate
entity
,
ReplicationMode
the Hibernate ReplicationMode
replicationMode
)

Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.

public void
replicate(String
the name of the persistent entity
entityName
,
Object
the persistent object to replicate
entity
,
ReplicationMode
the Hibernate ReplicationMode
replicationMode
)

Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.

public Serializable

Returns:

the generated identifier
save
(Object
the transient instance to persist
entity
)

Persist the given transient instance.

public Serializable

Returns:

the generated identifier
save
(String
the name of the persistent entity
entityName
,
Object
the transient instance to persist
entity
)

Persist the given transient instance.

public void
saveOrUpdate(Object
the persistent instance to save or update (to be associated with the Hibernate Session)
entity
)

Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).

public void
saveOrUpdate(String
the name of the persistent entity
entityName
,
Object
the persistent instance to save or update (to be associated with the Hibernate Session)
entity
)

Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).

public void
update(Object
the persistent instance to update
entity
)

Update the given persistent instance, associating it with the current Hibernate org.hibernate.Session.

public void
update(Object
the persistent instance to update
entity
,
LockMode
the lock mode to obtain
lockMode
)

Update the given persistent instance, associating it with the current Hibernate org.hibernate.Session.

public void
update(String
the name of the persistent entity
entityName
,
Object
the persistent instance to update
entity
)

Update the given persistent instance, associating it with the current Hibernate org.hibernate.Session.

public void
update(String
the name of the persistent entity
entityName
,
Object
the persistent instance to update
entity
,
LockMode
the lock mode to obtain
lockMode
)

Update the given persistent instance, associating it with the current Hibernate org.hibernate.Session.