Class Dao<POJO extends DbPojo>

java.lang.Object
tenapull.data.persistence.Dao<POJO>
Type Parameters:
POJO - the type parameter
Direct Known Subclasses:
AbstractPojoLookupDao, SimpleStringLookupDao

public class Dao<POJO extends DbPojo>
extends java.lang.Object
Standard generic dao.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    protected static class  Dao.SessionTracker
    Session tracker used to track the daos and invocations that are using an open session
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected org.apache.logging.log4j.Logger logger
    The logger for the pojo class which the dao is for
  • Constructor Summary

    Constructors 
    Constructor Description
    Dao​(java.lang.Class<POJO> pojoType)
    Instantiates a new Dao for the provided pojoType, and adds it to the map of pojoTypes and dao.
  • Method Summary

    Modifier and Type Method Description
    void borrowSessionsFrom​(java.lang.Class<? extends DbPojo> pojoType)
    Instructs the dao to borrow any open sessions on the current thread from the dao of the other pojo type.
    protected POJO checkedUnproxy​(org.hibernate.proxy.HibernateProxy pojo)
    Invoked by unproxy after it has confirmed that the pojo is not null and is an uninitialized instance of HibernateProxy
    void delete​(POJO pojo)
    Delete a POJO
    void delete​(POJO pojo, boolean runPrepare)
    Delete.
    POJO findByExactPojo​(POJO searchPojo)
    Search on all fields EXCEPT id.
    java.util.List<POJO> findByPojoNonNull​(POJO searchPojo)
    Search on all non-null fields except id.
    java.util.List<POJO> findByPropertyEqual​(java.lang.String propertyName, java.lang.Object value)
    Finds entities by one of its properties.
    java.util.List<POJO> findByPropertyEqual​(java.util.Map<java.lang.String,​java.lang.Object> propertyMap)
    Finds entities by multiple properties.
    static <P extends DbPojo,​ D extends Dao<P>>
    D
    get​(java.lang.Class<P> pojoType)
    Get the dao for the provided pojo type
    java.util.Map<java.lang.String,​org.hibernate.property.access.spi.PropertyAccess> getAccessMap()
    Gets or constructs the map of attribute accessors from Hibernate
    java.util.List<POJO> getAll()
    Return a list of all POJOs
    java.util.Map<java.lang.String,​javax.persistence.metamodel.Attribute<? super POJO,​?>> getAttributeMap()
    Gets or constructs the map of pojo attributes from Hibernate
    POJO getById​(int id)
    Get POJO by id
    java.util.Map<java.lang.String,​org.hibernate.property.access.spi.Getter> getGetterMap()
    Gets or creates the map of attribute getters from Hibernate
    java.util.Map<java.lang.String,​javax.persistence.metamodel.Attribute<? super POJO,​?>> getIdMap()
    Gets or creates map of id fields from Hibernate
    org.apache.logging.log4j.Logger getLogger()
    Gets logger for the dao's pojo type.
    java.lang.Class<POJO> getPojoType()
    Gets pojo type for the dao.
    protected Dao.SessionTracker getSession()
    Gets or creates a session tracker which holds a session to be used
    boolean hasActiveSession()
    Returns whether the dao has any active sessions on the current thread
    void holdSession()
    Holds a session open for all invocations on the current thread until releaseSession has been called.
    int insert​(POJO pojo)
    insert POJO
    protected int insert​(POJO pojo, boolean runPrepare)
    Insert int.
    protected java.util.List<POJO> keyValueSearch​(java.lang.String propertyName, java.lang.Object value)
    Key value search list.
    boolean lenderHasActiveSession()
    Checks whether any other dao that lends session to this dao has an active session on the current thread
    protected java.util.Map<java.lang.String,​java.lang.Object> makeExactPojoSearchMap​(POJO searchPojo)
    Makes the search make to be used by findExactPojo, using the attribute maps created from Hibernate.
    protected java.util.Map<java.lang.String,​java.lang.Object> makePojoNonNullSearchMap​(POJO searchPojo)
    Make the search make to use for findByPojoNonNull
    protected java.util.List<POJO> mapSearch​(java.util.Map<java.lang.String,​java.lang.Object> propertyMap)
    Map search list.
    void releaseSession()
    Release a session being held open.
    void saveOrUpdate​(POJO pojo)
    Save or update.
    protected void saveOrUpdate​(POJO pojo, boolean runPrepare)
    update POJO
    java.lang.String toString()  
    POJO unproxy​(POJO pojo)
    Unproxies a pojo if it is a Hibernate Proxy, or if not posssible because its session has already closed, this re-fetches the record with a new session and unproxies the new record.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • logger

      protected final org.apache.logging.log4j.Logger logger
      The logger for the pojo class which the dao is for
  • Constructor Details

    • Dao

      public Dao​(java.lang.Class<POJO> pojoType) throws java.lang.IllegalArgumentException
      Instantiates a new Dao for the provided pojoType, and adds it to the map of pojoTypes and dao.
      Parameters:
      pojoType - the pojo type
      Throws:
      java.lang.IllegalArgumentException - if a dao has already been instantiated for the provided pojoType
  • Method Details

    • get

      public static <P extends DbPojo,​ D extends Dao<P>> D get​(java.lang.Class<P> pojoType)
      Get the dao for the provided pojo type
      Type Parameters:
      P - the pojo type
      D - the dao for the provided pojo type
      Parameters:
      pojoType - the pojo type
      Returns:
      the dao for the provided pojo type
    • getPojoType

      public java.lang.Class<POJO> getPojoType()
      Gets pojo type for the dao.
      Returns:
      the pojo type
    • getLogger

      public org.apache.logging.log4j.Logger getLogger()
      Gets logger for the dao's pojo type.
      Returns:
      the logger
    • borrowSessionsFrom

      public void borrowSessionsFrom​(java.lang.Class<? extends DbPojo> pojoType)
      Instructs the dao to borrow any open sessions on the current thread from the dao of the other pojo type. This is not currently used, but may be needed in future use-cases involved database session synchronization and Hibernate proxies.
      Parameters:
      pojoType - the pojo type
    • holdSession

      public void holdSession()
      Holds a session open for all invocations on the current thread until releaseSession has been called. This is needed in some cases to initialize a HibernateProxy that was lazily fetched.
      NOTE: IT IS VERY IMPORTANT TO INCLUDE RELEASE SESSION IN THE FINALLY BLOCK OF A TRY ... (catch optional) ... FINALLY CONSTRUCT WHEN USING THIS METHOD. Otherwise the number of available sessions will become quickly exhausted, and the application will be deadlocked waiting for sessions to open up.
      It should also be noted that if holdSession is invoked multiple times on a thread before releaseSession is called, then releaseSession must be invoked the same number of times before the session will be closed.
    • releaseSession

      public void releaseSession()
      Release a session being held open.
      NOTE: IT IS VERY IMPORTANT TO INCLUDE RELEASE SESSION IN THE FINALLY BLOCK OF A TRY ... (catch optional) ... FINALLY CONSTRUCT WHEN USING THIS METHOD. Otherwise the number of available sessions will become quickly exhausted, and the application will be deadlocked waiting for sessions to open up.
      It should also be noted that if holdSession is invoked multiple times on a thread before releaseSession is called, then releaseSession must be invoked the same number of times before the session will be closed.
    • getSession

      protected Dao.SessionTracker getSession()
      Gets or creates a session tracker which holds a session to be used
      Returns:
      the session tracker with an open session
    • hasActiveSession

      public boolean hasActiveSession()
      Returns whether the dao has any active sessions on the current thread
      Returns:
      the boolean
    • lenderHasActiveSession

      public boolean lenderHasActiveSession()
      Checks whether any other dao that lends session to this dao has an active session on the current thread
      Returns:
      whether any session-lending daos to this dao have active sessions on the current thread
    • getById

      public POJO getById​(int id)
      Get POJO by id
      Parameters:
      id - the id
      Returns:
      the by id
    • saveOrUpdate

      public void saveOrUpdate​(POJO pojo)
      Save or update.
      Parameters:
      pojo - the pojo
    • saveOrUpdate

      protected void saveOrUpdate​(POJO pojo, boolean runPrepare)
      update POJO
      Parameters:
      pojo - POJO to be inserted or updated
      runPrepare - the run prepare
    • insert

      public int insert​(POJO pojo)
      insert POJO
      Parameters:
      pojo - POJO to be inserted
      Returns:
      the int
    • insert

      protected int insert​(POJO pojo, boolean runPrepare)
      Insert int.
      Parameters:
      pojo - the pojo
      runPrepare - the run prepare
      Returns:
      the int
    • delete

      public void delete​(POJO pojo)
      Delete a POJO
      Parameters:
      pojo - pojo to be deleted
    • delete

      public void delete​(POJO pojo, boolean runPrepare)
      Delete.
      Parameters:
      pojo - the pojo
      runPrepare - the run prepare
    • getAll

      public java.util.List<POJO> getAll()
      Return a list of all POJOs
      Returns:
      All POJOs
    • findByPropertyEqual

      public java.util.List<POJO> findByPropertyEqual​(java.lang.String propertyName, java.lang.Object value)
      Finds entities by one of its properties.
      Parameters:
      propertyName - the property name.
      value - the value by which to find.
      Returns:
      list
    • findByPropertyEqual

      public java.util.List<POJO> findByPropertyEqual​(java.util.Map<java.lang.String,​java.lang.Object> propertyMap)
      Finds entities by multiple properties. Inspired by https://stackoverflow.com/questions/11138118/really-dynamic-jpa-criteriabuilder
      Parameters:
      propertyMap - property and value pairs
      Returns:
      entities with properties equal to those passed in the map
    • keyValueSearch

      protected java.util.List<POJO> keyValueSearch​(java.lang.String propertyName, java.lang.Object value)
      Key value search list.
      Parameters:
      propertyName - the property name
      value - the value
      Returns:
      the list
    • mapSearch

      protected java.util.List<POJO> mapSearch​(java.util.Map<java.lang.String,​java.lang.Object> propertyMap)
      Map search list. this method is kept seperate so that the methods which need to call it can be overridden in MapLookupDao without overriding this functionality.
      Parameters:
      propertyMap - the property map
      Returns:
      the list
    • findByExactPojo

      public POJO findByExactPojo​(POJO searchPojo)
      Search on all fields EXCEPT id. Return exact match only. IMPORTANT: the id of the passed searchPojo will be mutated in order to test equality. IF no match is found, its id will be restored to the original value. If a match is found the id of the match will be left in the searchPojo.
      Parameters:
      searchPojo - the pojo to search for
      Returns:
      the pojo found
    • makeExactPojoSearchMap

      protected java.util.Map<java.lang.String,​java.lang.Object> makeExactPojoSearchMap​(POJO searchPojo)
      Makes the search make to be used by findExactPojo, using the attribute maps created from Hibernate.
      Parameters:
      searchPojo - the search pojo
      Returns:
      the map
    • findByPojoNonNull

      public java.util.List<POJO> findByPojoNonNull​(POJO searchPojo)
      Search on all non-null fields except id. Return a list of matching entries
      Parameters:
      searchPojo - the search pojo
      Returns:
      the list
    • makePojoNonNullSearchMap

      protected java.util.Map<java.lang.String,​java.lang.Object> makePojoNonNullSearchMap​(POJO searchPojo)
      Make the search make to use for findByPojoNonNull
      Parameters:
      searchPojo - the search pojo
      Returns:
      the map
    • getAttributeMap

      public java.util.Map<java.lang.String,​javax.persistence.metamodel.Attribute<? super POJO,​?>> getAttributeMap()
      Gets or constructs the map of pojo attributes from Hibernate
      Returns:
      the attribute map
    • getAccessMap

      public java.util.Map<java.lang.String,​org.hibernate.property.access.spi.PropertyAccess> getAccessMap()
      Gets or constructs the map of attribute accessors from Hibernate
      Returns:
      the access map
    • getGetterMap

      public java.util.Map<java.lang.String,​org.hibernate.property.access.spi.Getter> getGetterMap()
      Gets or creates the map of attribute getters from Hibernate
      Returns:
      the getter map
    • getIdMap

      public java.util.Map<java.lang.String,​javax.persistence.metamodel.Attribute<? super POJO,​?>> getIdMap()
      Gets or creates map of id fields from Hibernate
      Returns:
      the id map
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • unproxy

      public POJO unproxy​(POJO pojo)
      Unproxies a pojo if it is a Hibernate Proxy, or if not posssible because its session has already closed, this re-fetches the record with a new session and unproxies the new record.
      Parameters:
      pojo - the pojo
      Returns:
      the pojo
    • checkedUnproxy

      protected POJO checkedUnproxy​(@NotNull org.hibernate.proxy.HibernateProxy pojo)
      Invoked by unproxy after it has confirmed that the pojo is not null and is an uninitialized instance of HibernateProxy
      Parameters:
      pojo - the pojo
      Returns:
      the pojo