Class HibernateDao<T,ID extends Serializable>

java.lang.Object
com.peterphi.std.guice.hibernate.dao.HibernateDao<T,ID>
Type Parameters:
T - entity type
ID - primary key type
All Implemented Interfaces:
Dao<T,ID>
Direct Known Subclasses:
OAuthDelegatedTokenDaoImpl, OAuthServiceDaoImpl, OAuthSessionContextDaoImpl, OAuthSessionDaoImpl, RoleDaoImpl, UserDaoImpl

public class HibernateDao<T,ID extends Serializable> extends Object implements Dao<T,ID>
The default implementation of a Dao for Hibernate; often it is necessary to extend this to produce richer queries
This DAO can be used in two main ways:
  1. With singleton instances of this class created for each entity (where only simple CRUD methods are required)
  2. With subclasses of this HibernateDao implementing additional queries (often with corresponding extensions to the Dao type)
  • Field Details

    • clazz

      protected Class<T> clazz
    • isLargeTable

      protected boolean isLargeTable
    • searchExecutor

      @Inject public JPASearchExecutor searchExecutor
  • Constructor Details

    • HibernateDao

      public HibernateDao()
  • Method Details

    • setTypeLiteral

      @Inject public void setTypeLiteral(com.google.inject.TypeLiteral<T> clazz)
      Called by guice to provide the Class associated with T
      Parameters:
      clazz - The TypeLiteral associated with T (the entity type)
    • createQueryBuilder

      public JPAQueryBuilder<T,ID> createQueryBuilder()
    • getEntityType

      public Class<T> getEntityType()
      Description copied from interface: Dao
      Get the type of Entity this DAO processes
      Specified by:
      getEntityType in interface Dao<T,ID extends Serializable>
      Returns:
      the entity type
    • getQEntity

      public QEntity getQEntity()
    • idProperty

      @Deprecated protected String idProperty()
      Deprecated.
    • getByIds

      @Deprecated public List<T> getByIds(Collection<ID> ids)
      Deprecated.
      Specified by:
      getByIds in interface Dao<T,ID extends Serializable>
      See Also:
    • getListById

      public List<T> getListById(Collection<ID> ids)
      Description copied from interface: Dao
      Query the database for all items with the given primary keys (convenience method for an ORred id query)
      Specified by:
      getListById in interface Dao<T,ID extends Serializable>
      Parameters:
      ids - a collection of primary keys (may be empty)
      Returns:
      any items whose id are contained within ids. May be empty if no matches were found. May be smaller than ids. May be ordered differently from ids
    • getListById

      public List<T> getListById(List<ID> ids)
    • getReference

      public T getReference(ID id)
      Specified by:
      getReference in interface Dao<T,ID extends Serializable>
    • getById

      public T getById(ID id)
      Description copied from interface: Dao
      Retrieve an item by its primary key
      Specified by:
      getById in interface Dao<T,ID extends Serializable>
      Parameters:
      id - the id of an entity
      Returns:
      the item (or null if it is not present)
      See Also:
      • Session.get(Class, java.io.Serializable)
    • getAll

      public List<T> getAll()
      Description copied from interface: Dao
      Retrieve every object accessible through this DAO
      Specified by:
      getAll in interface Dao<T,ID extends Serializable>
      Returns:
      all entities of this type in the database
    • deleteById

      public void deleteById(ID id)
      Description copied from interface: Dao
      Delete an item by its primary key
      Specified by:
      deleteById in interface Dao<T,ID extends Serializable>
      Parameters:
      id - the id of an entity
    • delete

      public void delete(T obj)
      Description copied from interface: Dao
      Delete an item from the database
      Specified by:
      delete in interface Dao<T,ID extends Serializable>
      Parameters:
      obj - the entity
      See Also:
      • Session.delete(Object)
    • saveOrUpdate

      public void saveOrUpdate(T obj)
      Description copied from interface: Dao
      Create or Update an item in the database
      Specified by:
      saveOrUpdate in interface Dao<T,ID extends Serializable>
      Parameters:
      obj - the entity
      See Also:
      • Session.saveOrUpdate(Object)
    • save

      public ID save(T obj)
      Description copied from interface: Dao
      Save a new item in the database, returning its primary key
      Specified by:
      save in interface Dao<T,ID extends Serializable>
      Parameters:
      obj - the entity
      Returns:
      the primary key of the newly saved object
      See Also:
      • Session.save(Object)
    • update

      public void update(T obj)
      Description copied from interface: Dao
      Update an existing item in the database
      Specified by:
      update in interface Dao<T,ID extends Serializable>
      Parameters:
      obj - the entity
      See Also:
      • Session.update(Object)
    • merge

      public T merge(T obj)
      Description copied from interface: Dao
      Merge an unmanaged entity into a (new or existing) managed entity
      Specified by:
      merge in interface Dao<T,ID extends Serializable>
      Parameters:
      obj - the entity
      Returns:
      a Hibernate entity version of obj
      See Also:
      • Session.merge(Object)
    • getByUniqueProperty

      public T getByUniqueProperty(String propertyName, Object value)
      Description copied from interface: Dao
      Convenience method which retrieves an entity by the equality of a single property (may return null if no entities match)
      Specified by:
      getByUniqueProperty in interface Dao<T,ID extends Serializable>
      Parameters:
      propertyName - the property name (MUST be a simple property)
      value - the property value
      Returns:
      the entity, or null if no results were found
    • getSession

      protected org.hibernate.Session getSession()
    • getWriteSession

      protected org.hibernate.Session getWriteSession()
      Get a session and confirm that it is permitted to perform write operations
      Returns:
      Throws:
      ReadOnlyTransactionException - if we cannot perform writes in this session
    • createCriteria

      @Deprecated protected org.hibernate.Criteria createCriteria()
      Deprecated.
      use JPA2 Criteria with createCriteriaQuery(), Hibernate has deprecated their old criteria query and it will produce runtime warnings
      Create a Criteria instance for the entity type of this DAO, matching this entity and any subclasses/implementors
      Returns:
      The criteria instance for manipulation and execution
    • createCriteriaQuery

      protected <O> javax.persistence.criteria.CriteriaQuery<O> createCriteriaQuery()
      Create a JPA2 CriteriaQuery, which should have constraints generated using getCriteriaBuilder()
      Type Parameters:
      O - the return type of the query
      Returns:
    • createCriteriaQuery

      protected <O> javax.persistence.criteria.CriteriaQuery<O> createCriteriaQuery(Class<O> clazz)
      Create a JPA2 CriteriaQuery, which should have constraints generated using the methods in getCriteriaBuilder()
      Type Parameters:
      O - the return type of the query
      Parameters:
      clazz - the return type of the query
      Returns:
    • getCriteriaBuilder

      protected javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()
    • createReadQuery

      protected org.hibernate.query.Query createReadQuery(String hql)
      Create a Query instance for the given HQL query string, signalling no intent to write (and as such working if the TX is read-only)
      This method also makes an effort to prevent accidental update operations being called. This protection cannot be relied upon for untrusted input!
      Parameters:
      hql - The HQL query
      Returns:
      The query instance for manipulation and execution
    • createWriteQuery

      protected org.hibernate.query.Query createWriteQuery(String hql) throws ReadOnlyTransactionException
      Create a Query instance for the given HQL query string, signalling intent to write (and failing immediately if TX is read-only)
      Parameters:
      hql - The HQL query
      Returns:
      The query instance for manipulation and execution
      Throws:
      ReadOnlyTransactionException
    • getList

      protected List<T> getList(org.hibernate.Criteria criteria)
      Execute a Criteria search, returning the results as a checked list
      Parameters:
      criteria - a criteria created by this DAO
      Returns:
      The list of matched query results, implicitly cast to a List of this DAO type
      Throws:
      org.hibernate.HibernateException - Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
    • uniqueResult

      protected T uniqueResult(org.hibernate.Criteria criteria)
      Convenience method to return a single instance that matches the query, or null if the query returns no results.
      Parameters:
      criteria - a criteria created by this DAO
      Returns:
      the single result or null
      Throws:
      IllegalStateException - if there is more than one matching result
    • uniqueResult

      protected T uniqueResult(WebQuery query)
    • getList

      protected List<T> getList(org.hibernate.query.Query query)
      Execute a Criteria-based search, returning the results as a checked list
      Parameters:
      query - a query that returns an object compatible with the entity type of this DAO
      Returns:
      The list of matched query results, implicitly cast to a List of this DAO type
      Throws:
      org.hibernate.HibernateException - Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
    • getList

      protected List<T> getList(WebQuery query)
    • getIdList

      protected List<ID> getIdList(org.hibernate.Criteria criteria)
      Execute a Criteria-based search, returning the results as a checked list of primary key types
      Parameters:
      criteria - the criteria (note, its projection must have been set to Projections.id())
      Returns:
      Throws:
      org.hibernate.HibernateException - Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
    • getIdList

      public List<ID> getIdList(WebQuery query)
    • getIdList

      protected List<ID> getIdList(org.hibernate.query.Query query)
      Execute a Query search, returning the results as a checked list of primary key types
      Parameters:
      query - the query (note, its projection must have been set to the primary key)
      Returns:
      Throws:
      org.hibernate.HibernateException - Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
    • uniqueResult

      protected T uniqueResult(org.hibernate.query.Query query)
      Convenience method to return a single instance that matches the query, or null if the query returns no results.
      Parameters:
      query - a query that returns an object compatible with the entity type of this DAO
      Returns:
      the single result or null
      Throws:
      org.hibernate.HibernateException - if there is more than one matching result
    • getSessionFactory

      protected org.hibernate.SessionFactory getSessionFactory()
      Return the SessionFactory, useful for dynamic query building, etc.
      NOTE: Using this this is potentially disruptive to the normal way of writing applications in our guice framework
      Returns:
    • findIdsByUriQuery

      public ConstrainedResultSet<ID> findIdsByUriQuery(WebQuery query)
      Description copied from interface: Dao
      Given a query, return the IDs of the matching entities, returning the result as a ConstrainedResultSet of this entity's id type
      Specified by:
      findIdsByUriQuery in interface Dao<T,ID extends Serializable>
      Returns:
    • findByUriQuery

      public ConstrainedResultSet<T> findByUriQuery(WebQuery query)
      Description copied from interface: Dao
      Execute a Dynamic query using the specified constraints, returning the result as a ConstrainedResultSet of this entity type
      Specified by:
      findByUriQuery in interface Dao<T,ID extends Serializable>
      Parameters:
      query - the constraints to apply
      Returns:
      a resultset containing the requested number of results
    • find

      public ConstrainedResultSet<T> find(WebQuery query)
      Description copied from interface: Dao
      Execute a query, returning entities
      Specified by:
      find in interface Dao<T,ID extends Serializable>
      Returns:
    • find

      public ConstrainedResultSet<T> find(WebQuery query, JPASearchStrategy strategy)
      Description copied from interface: Dao
      Execute a query, using the named strategy (defaults to AUTO if null)
      Specified by:
      find in interface Dao<T,ID extends Serializable>
      strategy - strategy, or null to default to AUTO
      Returns:
    • findIds

      public ConstrainedResultSet<ID> findIds(WebQuery query)
      Description copied from interface: Dao
      Execute a query, using the ID strategy
      Specified by:
      findIds in interface Dao<T,ID extends Serializable>
      Returns:
    • count

      public long count(WebQuery query)
      Description copied from interface: Dao
      Return the total number of results for a given query (ignoring offset/limit values)
      The value returned is the same as the total field returned by setting query.computeSize(true)
      Specified by:
      count in interface Dao<T,ID extends Serializable>
      Returns:
      the total number of results
    • find

      public <RT> ConstrainedResultSet<RT> find(WebQuery query, JPASearchStrategy strategy, Function<?,RT> serialiser)
      Description copied from interface: Dao
      Execute a query, using the named strategy (defaults to AUTO if null) and optionally serialising the resulting rows using the provided serialiser
      Specified by:
      find in interface Dao<T,ID extends Serializable>
      strategy - strategy, or null to default to AUTO
      serialiser - the serialiser function to use to convert each row returned to the resulting resultset
      Returns:
    • getIds

      public Collection<ID> getIds(WebQuery constraints)
      Get a list of IDs matching a WebQuery
      Parameters:
      constraints -
      Returns: