Class AbstractDAO<E>

  • Type Parameters:
    E - the class which this DAO manages
    Direct Known Subclasses:
    PersonDAO

    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.
      Use criteriaQuery() instead.
      protected javax.persistence.criteria.CriteriaQuery<E> criteriaQuery()
      Creates a new CriteriaQuery for <E>.
      protected org.hibernate.Session currentSession()
      Returns the current Session.
      protected E get​(Serializable id)
      Return the persistent instance of <E> with the given identifier, or null 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 a CriteriaQuery query.
      protected List<E> list​(org.hibernate.Criteria criteria)
      Get the results of a Criteria 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> namedTypedQuery​(String queryName)
      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 Query
      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.
    • Constructor Detail

      • AbstractDAO

        public AbstractDAO​(org.hibernate.SessionFactory sessionFactory)
        Creates a new DAO with a given session provider.
        Parameters:
        sessionFactory - a session provider
    • Method Detail

      • currentSession

        protected org.hibernate.Session currentSession()
        Returns the current Session.
        Returns:
        the current session
      • criteria

        @Deprecated
        protected org.hibernate.Criteria criteria()
        Deprecated.
        Use criteriaQuery() instead.
        Creates a new Criteria query for <E>.
        Returns:
        a new Criteria query
        See Also:
        SharedSessionContract.createCriteria(Class)
      • criteriaQuery

        protected javax.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:
        SharedSessionContract.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 Query
        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 - 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.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 - the Criteria 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 a Criteria query.
        Parameters:
        criteria - the Criteria 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 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​(Serializable 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, 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 or null
        Throws:
        org.hibernate.HibernateException - if we can't initialize the proxy at this time, eg. the Session was closed