Class BaseMyBatisGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable>
- Type Parameters:
T- The entity type this DAO supports.PK- The primary key type this DAO supports.
- All Implemented Interfaces:
GenericDao<T,,PK> org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
BaseMyBatisFilterableDao,MyBatisSolarNodeMetadataDao
GenericDao using MyBatis via
SqlSessionDaoSupport.
The default configuration of this class allows implementations to be used
with minimal configuration, by following some simple naming conventions on
MyBatis query names. The queryForId, queryForAll,
insert, update, and delete query names all follow a
pattern using the name of the domain class as a parameter.
For example, if the domain class managed by this DAO is
some.package.Foo then the default query names used by this class will
be:
- get-Foo-by-id
- findall-Foo
- insert-Foo
- update-Foo
- delete-Foo
The handleRelation(Long, List, Class, Map) can be used by extending
classes to manage a to-many type relationship. The
relationQueryForParent, relationInsert,
relationUpdate, and relationDelete query names follow the
same pattern as above, with the relation domain class name appended at the
end. For example, if the relation domain class was some.package.Bar
then the default relationship query names used by this class will be:
- findall-Foo-Bar
- insert-Foo-Bar
- update-Foo-Bar
- delete-Foo-Bar
The ID_PROPERTY, INDEX_PROPERTY, and
BEAN_OBJECT_PROPERTY keys will be passed to the query as needed to
manage the related entity list.
The configurable properties of this class are:
- domainClass
- The implementation Class managed by this DAO.
- queryForId
- The name of the MyBatis SQL query to load an entity based on its primary
key (a Long). Defaults to
get-DomainClass-for-idwhere DatumClass is the simple name of the configureddomainClass. - queryForAll
- The name of the MyBatis SQL query to return a list of entities,
supporting a custom sort order. Defaults to
findall-DomainClasswhere DatumClass is the simple name of the configureddomainClass. - insert
- The name of the MyBatis SQL query to insert a new entity into the
database. Defaults to
insert-DomainClasswhere DatumClass is the simple name of the configureddomainClass. - update
- The name of the MyBatis SQL query to update an existing entity in the
database. Defaults to
update-DomainClasswhere DatumClass is the simple name of the configureddomainClass. - delete
- The name of the MyBatis SQL query to delete an existing entity from the
database. Defaults to
delete-DomainClasswhere DatumClass is the simple name of the configureddomainClass.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringA query property for a JavaBean style object.static final Stringstatic final Stringstatic final Stringstatic final StringA query property for a general date value.static final StringThe query name used for updates indelete(Entity).static final intError code to report that a named query was not found.static final StringA query property for a general ID value.static final StringA query property for an array index value.static final StringThe query name used for inserts instore(Entity).static final StringThe query name used forgetAll(List).static final StringThe query name used forget(Serializable).static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringThe query property for any custom sort descriptors that are provided.static final StringThe query name used for updates instore(Entity).Fields inherited from class net.solarnetwork.central.dao.mybatis.support.BaseMyBatisDao
FILTER_PROPERTY, FIRST_ROW, logFields inherited from class org.springframework.dao.support.DaoSupport
logger -
Constructor Summary
ConstructorsConstructorDescriptionBaseMyBatisGenericDao(Class<? extends T> domainClass, Class<? extends PK> pkClass) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidRemove an object from persistent storage in the database.protected <R> Rexecute(SqlSessionCallback<R> callback, Object errorObject) Execute a task with aSqlSessionCallback, providing standardized error message handling.Get a persisted domain object by its primary key.Get a list of persisted domain objects, optionally sorted in some way.protected StringgetMemberDomainKey(Class<?> memberClass) Get a "domain" for member queries.org.springframework.context.MessageSourceGet the class supported by this Dao.Get the primary key type used by this DAO.protected PKhandleAssignedPrimaryKeyStore(T datum) Supporting method for handling thestore(Entity)method for entities that use assigned primary keys, where the default logic of handling insert versus update will not work.handleChildRelation(T parent, E child, Class<? extends E> relationClass) Insert, update, and delete domain object child of the entity managed by this DAO.protected PKhandleInsert(T datum) Process the insert of a persisted entity.protected <E extends net.solarnetwork.domain.Identity<Long>>
voidhandleRelation(Long parentId, E newObject, Class<? extends E> relationClass, Map<String, ?> additionalProperties) Insert, update, and delete domain object related to the entity managed by this DAO.protected <E> voidhandleRelation(Long parentId, List<E> newList, Class<? extends E> relationClass, Map<String, ?> additionalProperties) Insert, update, and delete domain object list related to the entity managed by this DAO.protected PKhandleUpdate(T datum) Process the update of a persisted entity.protected RuntimeExceptionmapSqlMapException(RuntimeException e, Object errorObject) Attempt to map a runtime, SQL related exception to some friendlier exception.protected voidpreprocessInsert(T datum) Process a new unsaved entity for persisting.voidsetChildDelete(String childDelete) voidsetChildInsert(String childInsert) voidsetChildUpdate(String childUpdate) voidvoidvoidsetMessageSource(org.springframework.context.MessageSource messageSource) voidsetQueryForAll(String queryForAll) voidsetQueryForId(String queryForId) voidsetRelationDelete(String relationDelete) voidsetRelationInsert(String relationInsert) voidsetRelationObjectQueryForParent(String relationObjectQueryForParent) voidsetRelationQueryForParent(String relationQueryForParent) voidsetRelationUpdate(String relationUpdate) voidprotected final booleanspaceAppend(String value, StringBuilder buf) Append to a space-delimited string buffer.Persist the domainObject object into database, creating or updating as appropriate.Methods inherited from class net.solarnetwork.central.dao.mybatis.support.BaseMyBatisDao
executeCountQuery, selectFiltered, selectFiltered, selectFiltered, selectFiltered, selectFirst, selectList, selectLong, setSqlSessionFactoryMethods inherited from class org.mybatis.spring.support.SqlSessionDaoSupport
checkDaoConfig, createSqlSessionTemplate, getSqlSession, getSqlSessionFactory, getSqlSessionTemplate, setSqlSessionTemplateMethods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDao
-
Field Details
-
ERROR_CODE_INVALID_QUERY
public static final int ERROR_CODE_INVALID_QUERYError code to report that a named query was not found.- See Also:
-
QUERY_FOR_ID
The query name used forget(Serializable).- See Also:
-
QUERY_FOR_ALL
The query name used forgetAll(List).- See Also:
-
INSERT_OBJECT
The query name used for inserts instore(Entity).- See Also:
-
UPDATE_OBJECT
The query name used for updates instore(Entity).- See Also:
-
DELETE_OBJECT
The query name used for updates indelete(Entity).- See Also:
-
SORT_DESCRIPTORS_PROPERTY
The query property for any custom sort descriptors that are provided.- See Also:
-
ID_PROPERTY
A query property for a general ID value.- See Also:
-
INDEX_PROPERTY
A query property for an array index value.- See Also:
-
BEAN_OBJECT_PROPERTY
A query property for a JavaBean style object.- See Also:
-
DATE_PROPERTY
A query property for a general date value.- See Also:
-
RELATION_LIST_QUERY_FOR_PARENT
- See Also:
-
RELATION_OBJ_QUERY_FOR_PARENT
- See Also:
-
RELATION_INSERT
- See Also:
-
RELATION_UPDATE
- See Also:
-
RELATION_DELETE
- See Also:
-
CHILD_INSERT
- See Also:
-
CHILD_UPDATE
- See Also:
-
CHILD_DELETE
- See Also:
-
-
Constructor Details
-
BaseMyBatisGenericDao
Constructor.- Parameters:
domainClass- the domain classpkClass- the primary key class
-
-
Method Details
-
getObjectType
Description copied from interface:GenericDaoGet the class supported by this Dao.- Specified by:
getObjectTypein interfaceGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable> - Returns:
- class
-
getPrimaryKeyType
Get the primary key type used by this DAO.- Returns:
- the primary key type
-
get
Description copied from interface:GenericDaoGet a persisted domain object by its primary key.- Specified by:
getin interfaceGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable> - Parameters:
id- the primary key to retrieve- Returns:
- the domain object
-
getAll
Description copied from interface:GenericDaoGet a list of persisted domain objects, optionally sorted in some way.The
sortDescriptorsparameter can be null, in which case the sort order is not defined and implementation specific.- Specified by:
getAllin interfaceGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable> - Parameters:
sortDescriptors- list of sort descriptors to sort the results by- Returns:
- list of all persisted domain objects, or empty list if none available
-
store
Description copied from interface:GenericDaoPersist the domainObject object into database, creating or updating as appropriate.- Specified by:
storein interfaceGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable> - Parameters:
datum- the domain object so store- Returns:
- the primary key of the stored object
-
handleAssignedPrimaryKeyStore
Supporting method for handling thestore(Entity)method for entities that use assigned primary keys, where the default logic of handling insert versus update will not work.This implementation attempts to update the given entity first. If that does not actually update any rows,
preprocessInsert(Entity)is called, followed by an insert.- Parameters:
datum- the datum to store- Returns:
- the primary key
-
preprocessInsert
Process a new unsaved entity for persisting.This implementation will set the value of the
createdbean property of the datum instance to the current time ifEntity.getCreated()is null. Extending classes may want to extend or modify this behavior.- Parameters:
datum- the entity to be persisted
-
handleUpdate
Process the update of a persisted entity.This implementation merely calls
SqlSession.update(String, Object)using thegetUpdate()SqlMap.- Parameters:
datum- the datum to update- Returns:
Identity.getId()
-
handleInsert
Process the insert of a persisted entity.This implementation calls
SqlSession.insert(String, Object)using thegetInsert()SqlMap.- Parameters:
datum- the datum to insert- Returns:
- the result of the insert statement
-
delete
Description copied from interface:GenericDaoRemove an object from persistent storage in the database.- Specified by:
deletein interfaceGenericDao<T extends net.solarnetwork.dao.Entity<PK>,PK extends Serializable> - Parameters:
domainObject- the domain object to delete
-
handleRelation
protected <E> void handleRelation(Long parentId, List<E> newList, Class<? extends E> relationClass, Map<String, ?> additionalProperties) Insert, update, and delete domain object list related to the entity managed by this DAO.This method will use the
relationQueryForParent,relationInsert,relationUpdate, andrelationDeletequery names configured on this class to persist a set of related objects to a single parent entity. The related objects are not assumed to have surrogate primary keys.This method will pass the following query properties:
ID_PROPERTY- The
parentIdvalue, passed to all queries. INDEX_PROPERTY- The list index, starting at zero. This defines the ordering of the
related objects. Note the
relationDeletequery must support not having this property set, which signals that all related objects should be deleted. BEAN_OBJECT_PROPERTY- The related object to persist, passed to
relationInsertandrelationUpdatequeries.
Note that this method does not follow a pattern of deleting all related objects and then re-inserting objects. If there is a unique constraint defined on some columns, the constraint should be configured to defer the constraint check until after the end of the transaction. Otherwise constraint violations can occur while making the updates.
- Type Parameters:
E- the related object type- Parameters:
parentId- the ID of the parent entitynewList- the list of related objects to persist (may be null)relationClass- the Class of the related objectadditionalProperties- optional properties to pass to all queries
-
handleRelation
protected <E extends net.solarnetwork.domain.Identity<Long>> void handleRelation(Long parentId, E newObject, Class<? extends E> relationClass, Map<String, ?> additionalProperties) Insert, update, and delete domain object related to the entity managed by this DAO.This method will use the
relationObjectQueryForParent,relationInsert,relationUpdate, andrelationDeletequery names configured on this class to persist a related object to a single parent entity. The related object is not assumed to have surrogate primary keys.This method will pass the following query properties:
ID_PROPERTY- The
parentIdvalue, passed to all queries. BEAN_OBJECT_PROPERTY- The related object to persist, passed to
relationInsertandrelationUpdateandrelationDeletequeries.
- Type Parameters:
E- the related object type- Parameters:
parentId- the ID of the parent entitynewObject- the related object to persist (may be null)relationClass- the Class of the related objectadditionalProperties- optional properties to pass to all queries
-
handleChildRelation
protected <E extends net.solarnetwork.domain.Identity<Long>> Long handleChildRelation(T parent, E child, Class<? extends E> relationClass) Insert, update, and delete domain object child of the entity managed by this DAO.This method will use the
relationObjectQueryForParent,childInsert,childUpdate, andchildDeletequery names configured on this class to persist a related object to a single parent entity.This method will pass the following query objects:
- relationObjectQueryForParent
- A Long, taken from ID value of the
parent. - childDelete
- A Long, from the ID value of the
child. - childInsert
- The
childobject. This query is expected to return the child's primary key in the form of a Long object. - childUpdate
- The
childobject.
- Type Parameters:
E- the related object type- Parameters:
parent- the parent entitychild- the child object to persist (may be null)relationClass- the Class of the related object- Returns:
- the child entity's primary key
-
execute
Execute a task with aSqlSessionCallback, providing standardized error message handling. This method will catch runtime exceptions and attempt to map those to message codes. If the exception can be mapped, a newValidationExceptionwill be thrown instead.- Type Parameters:
R- the result object type- Parameters:
callback- the callbackerrorObject- the root validation error object- Returns:
- the result of
SqlSessionCallback.doWithSqlSession(org.apache.ibatis.session.SqlSession) - See Also:
-
mapSqlMapException
Attempt to map a runtime, SQL related exception to some friendlier exception.- Parameters:
e- the original exceptionerrorObject- a validation error object- Returns:
- an exception, never null and might be the exception instance passed in
-
getMemberDomainKey
Get a "domain" for member queries.This returns a composite string based from
getDomainClass()and thememberClasspassed in.- Parameters:
memberClass- the member class type- Returns:
- domain key
-
spaceAppend
Append to a space-delimited string buffer.This is designed with full-text search in mind, for building up a query string.
- Parameters:
value- the value to append if not emptybuf- the buffer to append to- Returns:
- true if
valuewas appended tobuf
-
getQueryForId
-
setQueryForId
-
getInsert
-
setInsert
-
getUpdate
-
setUpdate
-
getDomainClass
-
getQueryForAll
-
setQueryForAll
-
getDelete
-
setDelete
-
getRelationQueryForParent
-
setRelationQueryForParent
-
getRelationInsert
-
setRelationInsert
-
getRelationUpdate
-
setRelationUpdate
-
getRelationDelete
-
setRelationDelete
-
getRelationObjectQueryForParent
-
setRelationObjectQueryForParent
-
getChildInsert
-
setChildInsert
-
getChildUpdate
-
setChildUpdate
-
getChildDelete
-
setChildDelete
-
getMessageSource
public org.springframework.context.MessageSource getMessageSource() -
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource)
-