Class GenericBoJdbcDao<T>
- java.lang.Object
-
- com.github.ddth.dao.BaseDao
-
- com.github.ddth.dao.jdbc.BaseJdbcDao
-
- com.github.ddth.dao.jdbc.GenericBoJdbcDao<T>
-
- All Implemented Interfaces:
IGenericBoDao<T>
,IJdbcHelper
,java.lang.AutoCloseable
public abstract class GenericBoJdbcDao<T> extends BaseJdbcDao implements IGenericBoDao<T>
Generic implementation ofIGenericBoDao
Note: this class must be *abstract* in order to correctly detect the generic typed parameter!
- Since:
- 0.8.0
- Author:
- Thanh Nguyen
-
-
Field Summary
-
Fields inherited from interface com.github.ddth.dao.jdbc.IJdbcHelper
DEFAULT_DATASOURCE
-
-
Constructor Summary
Constructors Constructor Description GenericBoJdbcDao()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
cacheKey(BoId id)
Calculate cache key for a BO.protected java.lang.String
cacheKey(T bo)
Calculate cache key for a BO.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.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.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.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.protected java.lang.String
calcSqlSelectAll()
Calculate the SQL query to select all rows.protected java.lang.String
calcSqlSelectAllSorted()
Calculate the SQL query to select all rows.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.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.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.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.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.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.protected DaoResult
create(java.sql.Connection conn, T bo)
Create/Persist a new BO to storage.DaoResult
create(T bo)
Create/Persist a new BO to storage.protected DaoResult
createOrUpdate(java.sql.Connection conn, T bo)
Create a new BO or update an existing one.DaoResult
createOrUpdate(T bo)
Create a new BO or update an existing one.protected DaoResult
delete(java.sql.Connection conn, T bo)
Delete an existing BO from storage.DaoResult
delete(T bo)
Delete an existing BO from storage.T
get(BoId id)
Fetch an existing BO from storage by id.T[]
get(BoId... idList)
Fetch list of existing BOs from storage by id.protected T
get(java.sql.Connection conn, BoId id)
Fetch an existing BO from storage by id.protected T[]
get(java.sql.Connection conn, BoId... idList)
Fetch list of existing BOs from storage by id.java.util.stream.Stream<T>
getAll()
Fetch all existing BOs from storage and return the result as a stream.protected java.util.stream.Stream<T>
getAll(java.sql.Connection conn)
Fetch all existing BOs from storage and return the result as a stream.java.util.stream.Stream<T>
getAllSorted()
Fetch all existing BOs from storage, sorted by primary key(s) and return the result as a stream.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.java.lang.String
getCacheKeyPrefix()
Get string prefixed to cache key.java.lang.String
getCacheName()
AbstractGenericRowMapper<T>
getRowMapper()
java.lang.String
getTableName()
protected java.lang.Class<T>
getTypeClass()
GenericBoJdbcDao<T>
init()
Initializing method.protected void
invalidateCache(T bo, CacheInvalidationReason reason)
Invalidate a BO from cache.boolean
isUpsertInTransaction()
Should "upsert" (createOrUpdate(Object)
andupdateOrCreate(Object)
be done in a transaction context?void
setCacheKeyPrefix(java.lang.String cacheKeyPrefix)
Set string prefixed to cache key.GenericBoJdbcDao<T>
setCacheName(java.lang.String cacheName)
GenericBoJdbcDao<T>
setRowMapper(AbstractGenericRowMapper<T> rowMapper)
GenericBoJdbcDao<T>
setTableName(java.lang.String tableName)
void
setUpsertInTransaction(boolean upsertInTransaction)
Should "upsert" (createOrUpdate(Object)
andupdateOrCreate(Object)
be done in a transaction context?protected DaoResult
update(java.sql.Connection conn, T bo)
Update an existing BO.DaoResult
update(T bo)
Update an existing BO.protected DaoResult
updateOrCreate(java.sql.Connection conn, T bo)
Update an existing BO or create a new one.DaoResult
updateOrCreate(T bo)
Update an existing BO or create a new one.-
Methods inherited from class com.github.ddth.dao.jdbc.BaseJdbcDao
commitTransaction, execute, execute, execute, execute, executeSelect, executeSelect, executeSelect, executeSelect, executeSelect, executeSelect, executeSelect, executeSelect, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectAsStream, executeSelectOne, executeSelectOne, executeSelectOne, executeSelectOne, executeSelectOne, executeSelectOne, executeSelectOne, executeSelectOne, getConnection, getConnection, getConnection, getConnection, getJdbcHelper, returnConnection, rollbackTransaction, setJdbcHelper, startTransaction
-
Methods inherited from class com.github.ddth.dao.BaseDao
addProfiling, clearProfiling, close, destroy, getCache, getCacheFactory, getFromCache, getFromCache, getProfiling, isCacheEnabled, isCacheItemsExpireAfterWrite, putToCache, putToCache, putToCache, removeFromCache, setCacheFactory, setCacheItemsExpireAfterWrite, startProfiling
-
-
-
-
Method Detail
-
isUpsertInTransaction
public boolean isUpsertInTransaction()
Should "upsert" (createOrUpdate(Object)
andupdateOrCreate(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)
andupdateOrCreate(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
-
getRowMapper
public AbstractGenericRowMapper<T> getRowMapper()
-
setRowMapper
public GenericBoJdbcDao<T> setRowMapper(AbstractGenericRowMapper<T> rowMapper)
-
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)
-
init
public GenericBoJdbcDao<T> init()
Description copied from class:BaseDao
Initializing method.
-
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 returnsgetTableName()
.- 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 returnsgetTableName()
.- 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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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 fromcalcTableName(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
-
create
public DaoResult create(T bo)
Create/Persist a new BO to storage.- Specified by:
create
in interfaceIGenericBoDao<T>
- Returns:
-
delete
protected DaoResult delete(java.sql.Connection conn, T bo)
Delete an existing BO from storage.- Parameters:
conn
-bo
-- Returns:
- Since:
- 0.8.1
-
delete
public DaoResult delete(T bo)
Delete an existing BO from storage.- Specified by:
delete
in interfaceIGenericBoDao<T>
- Returns:
-
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 interfaceIGenericBoDao<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 interfaceIGenericBoDao<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 interfaceIGenericBoDao<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 interfaceIGenericBoDao<T>
- Returns:
-
update
protected DaoResult update(java.sql.Connection conn, T bo)
Update an existing BO.- Parameters:
conn
-bo
-- Returns:
- Since:
- 0.8.1
-
update
public DaoResult update(T bo)
Update an existing BO.- Specified by:
update
in interfaceIGenericBoDao<T>
- Returns:
-
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
-
createOrUpdate
public DaoResult createOrUpdate(T bo)
Create a new BO or update an existing one.- Specified by:
createOrUpdate
in interfaceIGenericBoDao<T>
- 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
-
updateOrCreate
public DaoResult updateOrCreate(T bo)
Update an existing BO or create a new one.- Specified by:
updateOrCreate
in interfaceIGenericBoDao<T>
- Returns:
- Since:
- 0.8.1
-
-