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

    Modifier and Type
    Method
    Description
    protected jakarta.persistence.criteria.CriteriaQuery<E>
    Creates a new CriteriaQuery for <E>.
    protected org.hibernate.Session
    Returns the current Session.
    protected E
    get(Object id)
    Return the persistent instance of <E> with the given identifier, or null 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.
    protected List<E>
    list(jakarta.persistence.criteria.CriteriaQuery<E> criteria)
    Get the results of a CriteriaQuery 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 named Query.
    protected org.hibernate.query.Query<E>
    Returns a named and type-safe Query.
    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 typed org.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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 current Session.
      Returns:
      the current session
    • criteriaQuery

      protected jakarta.persistence.criteria.CriteriaQuery<E> criteriaQuery()
      Creates a new CriteriaQuery for <E>.
      Returns:
      a new CriteriaQuery query
    • namedQuery

      protected org.hibernate.query.Query<?> namedQuery(String queryName) throws org.hibernate.HibernateException
      Returns a named Query.
      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-safe Query.
      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

      protected org.hibernate.query.Query<E> query(String queryString)
      Returns a typed org.hibernate.query.Query<E>
      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 @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 - the CriteriaQuery 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 a CriteriaQuery query.
      Parameters:
      criteria - the CriteriaQuery 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(Object id)
      Return the persistent instance of <E> with the given identifier, or null 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

      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 or null
      Throws:
      org.hibernate.HibernateException - if we can't initialize the proxy at this time, e.g. the Session was closed