Class GenericBoJdbcDao<T>

  • All Implemented Interfaces:
    IGenericBoDao<T>, IJdbcHelper, java.lang.AutoCloseable

    public abstract class GenericBoJdbcDao<T>
    extends BaseJdbcDao
    implements IGenericBoDao<T>
    Generic implementation of IGenericBoDao

    Note: this class must be *abstract* in order to correctly detect the generic typed parameter!

    Since:
    0.8.0
    Author:
    Thanh Nguyen
    • Constructor Detail

      • GenericBoJdbcDao

        public GenericBoJdbcDao()
    • Method Detail

      • isUpsertInTransaction

        public boolean isUpsertInTransaction()
        Should "upsert" (createOrUpdate(Object) and updateOrCreate(Object) be done in a transaction context?
        Returns:
        true if "upsert" should be done in a transaction context, false otherwise
        Since:
        0.9.0.5
      • setUpsertInTransaction

        public void setUpsertInTransaction​(boolean upsertInTransaction)
        Should "upsert" (createOrUpdate(Object) and updateOrCreate(Object) be done in a transaction context?
        Parameters:
        upsertInTransaction - true if "upsert" should be done in a transaction context, false otherwise
        Since:
        0.9.0.5
      • getTypeClass

        protected java.lang.Class<T> getTypeClass()
        Returns:
        Since:
        0.8.0.4
      • getTableName

        public java.lang.String getTableName()
      • setTableName

        public GenericBoJdbcDao<T> setTableName​(java.lang.String tableName)
      • getCacheName

        public java.lang.String getCacheName()
      • setCacheName

        public GenericBoJdbcDao<T> setCacheName​(java.lang.String cacheName)
      • calcTableName

        protected java.lang.String calcTableName​(BoId id)
        For data partitioning: Sub-class can override this method to calculate name of DB table to access the BO specified by supplied id.

        This method of class GenericBoJdbcDao simple returns getTableName().

        Parameters:
        id -
        Returns:
        Since:
        0.8.0.2
      • calcTableName

        protected java.lang.String calcTableName​(T bo)
        For data partitioning: Sub-class can override this method to calculate name of DB table to access the BO specified by supplied bo.

        This method of class GenericBoJdbcDao simple returns getTableName().

        Parameters:
        bo -
        Returns:
        Since:
        0.8.0.2
      • calcSqlInsert

        protected java.lang.String calcSqlInsert​(BoId id)
        For data partitioning: Sub-class can override this method to calculate the SQL query to insert the BO by supplied id to DB table.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(BoId).

        Parameters:
        id -
        Returns:
        Since:
        0.8.0.2
      • calcSqlInsert

        protected java.lang.String calcSqlInsert​(T bo)
        For data partitioning: Sub-class can override this method to calculate the SQL query to insert the BO by supplied bo to DB table.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(Object).

        Parameters:
        bo -
        Returns:
        Since:
        0.8.0.2
      • calcSqlDeleteOne

        protected java.lang.String calcSqlDeleteOne​(BoId id)
        For data partitioning: Sub-class can override this method to calculate the SQL query to delete the BO by supplied id.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(BoId).

        Parameters:
        id -
        Returns:
        Since:
        0.8.0.2
      • calcSqlDeleteOne

        protected java.lang.String calcSqlDeleteOne​(T bo)
        For data partitioning: Sub-class can override this method to calculate the SQL query to delete the BO by supplied bo.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(Object).

        Parameters:
        bo -
        Returns:
        Since:
        0.8.0.2
      • calcSqlSelectOne

        protected java.lang.String calcSqlSelectOne​(BoId id)
        For data partitioning: Sub-class can override this method to calculate the SQL query to select the BO by supplied id.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(BoId).

        Parameters:
        id -
        Returns:
        Since:
        0.8.0.2
      • calcSqlSelectOne

        protected java.lang.String calcSqlSelectOne​(T bo)
        For data partitioning: Sub-class can override this method to calculate the SQL query to select the BO by supplied bo.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(Object).

        Parameters:
        bo -
        Returns:
        Since:
        0.8.0.2
      • calcSqlSelectAll

        protected java.lang.String calcSqlSelectAll()
        Calculate the SQL query to select all rows.
        Returns:
        Since:
        0.9.0
      • calcSqlSelectAllSorted

        protected java.lang.String calcSqlSelectAllSorted()
        Calculate the SQL query to select all rows.
        Returns:
        Since:
        0.9.0
      • calcSqlUpdateOne

        protected java.lang.String calcSqlUpdateOne​(BoId id)
        For data partitioning: Sub-class can override this method to calculate the SQL query to update the BO by supplied id.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(BoId).

        Parameters:
        id -
        Returns:
        Since:
        0.8.0.2
      • calcSqlUpdateOne

        protected java.lang.String calcSqlUpdateOne​(T bo)
        For data partitioning: Sub-class can override this method to calculate the SQL query to update the BO by supplied bo.

        This method of class GenericBoJdbcDao simple returns its own pre-calculated SQL query with table name substituted by value returned from calcTableName(Object).

        Parameters:
        bo -
        Returns:
        Since:
        0.8.0.2
      • getCacheKeyPrefix

        public java.lang.String getCacheKeyPrefix()
        Get string prefixed to cache key.
        Returns:
        Since:
        0.8.2
      • setCacheKeyPrefix

        public void setCacheKeyPrefix​(java.lang.String cacheKeyPrefix)
        Set string prefixed to cache key.
        Parameters:
        cacheKeyPrefix -
        Since:
        0.8.2
      • cacheKey

        protected java.lang.String cacheKey​(BoId id)
        Calculate cache key for a BO.
        Parameters:
        id -
        Returns:
      • cacheKey

        protected java.lang.String cacheKey​(T bo)
        Calculate cache key for a BO.
        Parameters:
        bo -
        Returns:
      • invalidateCache

        protected void invalidateCache​(T bo,
                                       CacheInvalidationReason reason)
        Invalidate a BO from cache.
        Parameters:
        bo -
        reason -
      • create

        protected DaoResult create​(java.sql.Connection conn,
                                   T bo)
        Create/Persist a new BO to storage.
        Parameters:
        conn -
        bo -
        Returns:
        Since:
        0.8.1
      • delete

        protected DaoResult delete​(java.sql.Connection conn,
                                   T bo)
        Delete an existing BO from storage.
        Parameters:
        conn -
        bo -
        Returns:
        Since:
        0.8.1
      • get

        protected T get​(java.sql.Connection conn,
                        BoId id)
        Fetch an existing BO from storage by id.
        Parameters:
        conn -
        id -
        Returns:
      • get

        public T get​(BoId id)
        Fetch an existing BO from storage by id.
        Specified by:
        get in interface IGenericBoDao<T>
        Returns:
      • get

        protected T[] get​(java.sql.Connection conn,
                          BoId... idList)
        Fetch list of existing BOs from storage by id.
        Parameters:
        conn -
        idList -
        Returns:
        Since:
        0.8.1
      • get

        public T[] get​(BoId... idList)
        Fetch list of existing BOs from storage by id.
        Specified by:
        get in interface IGenericBoDao<T>
        Returns:
      • getAll

        protected java.util.stream.Stream<T> getAll​(java.sql.Connection conn)
        Fetch all existing BOs from storage and return the result as a stream.
        Parameters:
        conn -
        Returns:
        Since:
        0.9.0
      • getAll

        public java.util.stream.Stream<T> getAll()
        Fetch all existing BOs from storage and return the result as a stream.
        Specified by:
        getAll in interface IGenericBoDao<T>
        Returns:
      • getAllSorted

        protected java.util.stream.Stream<T> getAllSorted​(java.sql.Connection conn)
        Fetch all existing BOs from storage, sorted by primary key(s) and return the result as a stream.
        Parameters:
        conn -
        Returns:
        Since:
        0.9.0
      • getAllSorted

        public java.util.stream.Stream<T> getAllSorted()
        Fetch all existing BOs from storage, sorted by primary key(s) and return the result as a stream.
        Specified by:
        getAllSorted in interface IGenericBoDao<T>
        Returns:
      • update

        protected DaoResult update​(java.sql.Connection conn,
                                   T bo)
        Update an existing BO.
        Parameters:
        conn -
        bo -
        Returns:
        Since:
        0.8.1
      • createOrUpdate

        protected DaoResult createOrUpdate​(java.sql.Connection conn,
                                           T bo)
        Create a new BO or update an existing one.
        Parameters:
        conn -
        bo -
        Returns:
        Since:
        0.8.1
      • updateOrCreate

        protected DaoResult updateOrCreate​(java.sql.Connection conn,
                                           T bo)
        Update an existing BO or create a new one.
        Parameters:
        conn -
        bo -
        Returns:
        Since:
        0.8.1