Package io.dropwizard.hibernate
Class AbstractDAO<E>
- java.lang.Object
-
- io.dropwizard.hibernate.AbstractDAO<E>
-
- Type Parameters:
E
- the class which this DAO manages
public class AbstractDAO<E> extends Object
An abstract base class for Hibernate DAO classes.
-
-
Constructor Summary
Constructors Constructor Description AbstractDAO(org.hibernate.SessionFactory sessionFactory)
Creates a new DAO with a given session provider.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected org.hibernate.Criteria
criteria()
Deprecated.UsecriteriaQuery()
instead.protected javax.persistence.criteria.CriteriaQuery<E>
criteriaQuery()
Creates a newCriteriaQuery
for<E>
.protected org.hibernate.Session
currentSession()
Returns the currentSession
.protected E
get(Serializable id)
Return the persistent instance of<E>
with the given identifier, ornull
if there is no such persistent instance.Class<E>
getEntityClass()
Returns the entity class managed by this DAO.protected <T> T
initialize(T proxy)
Force initialization of a proxy or persistent collection.protected List<E>
list(javax.persistence.criteria.CriteriaQuery<E> criteria)
Get the results of aCriteriaQuery
query.protected List<E>
list(org.hibernate.Criteria criteria)
Get the results of aCriteria
query.protected List<E>
list(org.hibernate.query.Query<E> query)
Get the results of a query.protected org.hibernate.query.Query<?>
namedQuery(String queryName)
Returns a namedQuery
.protected E
persist(E entity)
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>
query(String queryString)
Returns a typedQuery
protected E
uniqueResult(javax.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.Criteria criteria)
Convenience method to return a single instance that matches the criteria, 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.
-
-
-
Method Detail
-
currentSession
protected org.hibernate.Session currentSession()
Returns the currentSession
.- Returns:
- the current session
-
criteria
@Deprecated protected org.hibernate.Criteria criteria()
Deprecated.UsecriteriaQuery()
instead.Creates a newCriteria
query for<E>
.- Returns:
- a new
Criteria
query - See Also:
SharedSessionContract.createCriteria(Class)
-
criteriaQuery
protected javax.persistence.criteria.CriteriaQuery<E> 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:
SharedSessionContract.getNamedQuery(String)
-
query
protected org.hibernate.query.Query<E> query(String queryString)
Returns a typedQuery
- Parameters:
queryString
- HQL query- Returns:
- typed query
-
getEntityClass
public Class<E> getEntityClass()
Returns the entity class managed by this DAO.- Returns:
- the entity class managed by this DAO
-
uniqueResult
protected E uniqueResult(javax.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.Criteria criteria) throws org.hibernate.HibernateException
Convenience method to return a single instance that matches the criteria, or null if the criteria returns no results.- Parameters:
criteria
- theCriteria
query to run- Returns:
- the single result or
null
- Throws:
org.hibernate.HibernateException
- if there is more than one matching result- See Also:
Criteria.uniqueResult()
-
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(org.hibernate.Criteria criteria) throws org.hibernate.HibernateException
Get the results of aCriteria
query.- Parameters:
criteria
- theCriteria
query to run- Returns:
- the list of matched query results
- Throws:
org.hibernate.HibernateException
- See Also:
Criteria.list()
-
list
protected List<E> list(javax.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
protected List<E> list(org.hibernate.query.Query<E> query) throws org.hibernate.HibernateException
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
protected E get(Serializable id)
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, Serializable)
-
persist
protected E persist(E entity) throws org.hibernate.HibernateException
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
-
-