Package io.dropwizard.hibernate
Class AbstractDAO<E>
java.lang.Object
io.dropwizard.hibernate.AbstractDAO<E>
- Type Parameters:
E
- the class which this DAO manages
An abstract base class for Hibernate DAO classes.
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractDAO
(org.hibernate.SessionFactory sessionFactory) Creates a new DAO with a given session provider. -
Method Summary
Modifier and TypeMethodDescriptionprotected jakarta.persistence.criteria.CriteriaQuery<E>
Creates a newCriteriaQuery
for<E>
.protected org.hibernate.Session
Returns the currentSession
.protected E
Return the persistent instance of<E>
with the given identifier, ornull
if there is no such persistent instance.Returns the entity class managed by this DAO.protected <T> T
initialize
(T proxy) Force initialization of a proxy or persistent collection.Get the results of aCriteriaQuery
query.Get the results of a query.protected org.hibernate.query.Query<?>
namedQuery
(String queryName) Returns a namedQuery
.protected org.hibernate.query.Query<E>
namedTypedQuery
(String queryName) Returns a named and type-safeQuery
.protected E
Either save or update the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).protected org.hibernate.query.Query<E>
Returns a typedorg.hibernate.query.Query<E>
protected @Nullable E
uniqueResult
(jakarta.persistence.criteria.CriteriaQuery<E> criteriaQuery) Convenience method to return a single instance that matches the criteria query, or null if the criteria returns no results.protected E
uniqueResult
(org.hibernate.query.Query<E> query) Convenience method to return a single instance that matches the query, or null if the query returns no results.
-
Constructor Details
-
AbstractDAO
public AbstractDAO(org.hibernate.SessionFactory sessionFactory) Creates a new DAO with a given session provider.- Parameters:
sessionFactory
- a session provider
-
-
Method Details
-
currentSession
protected org.hibernate.Session currentSession()Returns the currentSession
.- Returns:
- the current session
-
criteriaQuery
Creates a newCriteriaQuery
for<E>
.- Returns:
- a new
CriteriaQuery
query
-
namedQuery
protected org.hibernate.query.Query<?> namedQuery(String queryName) throws org.hibernate.HibernateException Returns a namedQuery
.- Parameters:
queryName
- the name of the query- Returns:
- the named query
- Throws:
org.hibernate.HibernateException
- See Also:
-
QueryProducer.getNamedQuery(String)
-
namedTypedQuery
protected org.hibernate.query.Query<E> namedTypedQuery(String queryName) throws org.hibernate.HibernateException Returns a named and type-safeQuery
.- Parameters:
queryName
- the name of the query- Returns:
- the named query
- Throws:
org.hibernate.HibernateException
- Since:
- 2.0.22
- See Also:
-
Session.createNamedQuery(String, Class)
-
query
Returns a typedorg.hibernate.query.Query<E>
- Parameters:
queryString
- HQL query- Returns:
- typed query
-
getEntityClass
Returns the entity class managed by this DAO.- Returns:
- the entity class managed by this DAO
-
uniqueResult
protected @Nullable E uniqueResult(jakarta.persistence.criteria.CriteriaQuery<E> criteriaQuery) throws org.hibernate.HibernateException Convenience method to return a single instance that matches the criteria query, or null if the criteria returns no results.- Parameters:
criteriaQuery
- theCriteriaQuery
query to run- Returns:
- the single result or
null
- Throws:
org.hibernate.HibernateException
- if there is more than one matching result
-
uniqueResult
protected E uniqueResult(org.hibernate.query.Query<E> query) throws org.hibernate.HibernateException Convenience method to return a single instance that matches the query, or null if the query returns no results.- Parameters:
query
- the query to run- Returns:
- the single result or
null
- Throws:
org.hibernate.HibernateException
- if there is more than one matching result- See Also:
-
Query.uniqueResult()
-
list
protected List<E> list(jakarta.persistence.criteria.CriteriaQuery<E> criteria) throws org.hibernate.HibernateException Get the results of aCriteriaQuery
query.- Parameters:
criteria
- theCriteriaQuery
query to run- Returns:
- the list of matched query results
- Throws:
org.hibernate.HibernateException
-
list
Get the results of a query.- Parameters:
query
- the query to run- Returns:
- the list of matched query results
- Throws:
org.hibernate.HibernateException
- See Also:
-
Query.list()
-
get
Return the persistent instance of<E>
with the given identifier, ornull
if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)- Parameters:
id
- an identifier- Returns:
- a persistent instance or
null
- Throws:
org.hibernate.HibernateException
- See Also:
-
Session.get(Class, Object)
-
persist
Either save or update the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking). This operation cascades to associated instances if the association is mapped with cascade="save-update".- Parameters:
entity
- a transient or detached instance containing new or updated state- Throws:
org.hibernate.HibernateException
- See Also:
-
Session.saveOrUpdate(Object)
-
initialize
protected <T> T initialize(T proxy) throws org.hibernate.HibernateException Force initialization of a proxy or persistent collection. Note: This only ensures initialization of a proxy object or collection; it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.- Parameters:
proxy
- a persistable object, proxy, persistent collection ornull
- Throws:
org.hibernate.HibernateException
- if we can't initialize the proxy at this time, eg. theSession
was closed
-