Class BaseMyBatisGenericDaoSupport<T extends net.solarnetwork.dao.Entity<K>,K>

java.lang.Object
org.springframework.dao.support.DaoSupport
org.mybatis.spring.support.SqlSessionDaoSupport
net.solarnetwork.central.dao.mybatis.support.BaseMyBatisDao
net.solarnetwork.central.dao.mybatis.support.BaseMyBatisGenericDaoSupport<T,K>
All Implemented Interfaces:
net.solarnetwork.dao.GenericDao<T,K>, org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
BaseMyBatisFilterableDaoSupport

public abstract class BaseMyBatisGenericDaoSupport<T extends net.solarnetwork.dao.Entity<K>,K> extends BaseMyBatisDao implements net.solarnetwork.dao.GenericDao<T,K>
Base implementation of GenericDao using MyBatis via SqlSessionDaoSupport.
Since:
2.1
  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.solarnetwork.dao.GenericDao

    net.solarnetwork.dao.GenericDao.EntityEventType, net.solarnetwork.dao.GenericDao.StandardSortKey
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The query name used for updates in GenericDao.delete(Entity).
    static final int
    Error code to report that a named query was not found.
    static final String
    The query property for a filter (search criteria) object.
    static final String
    The query name used for inserts in GenericDao.save(Entity).
    static final String
    The query name used for GenericDao.getAll(List).
    static final String
    The query name used for GenericDao.get(Object).
    static final String
    The query property for any custom sort descriptors that are provided.
    static final String
    The query name used for updates in GenericDao.save(Entity).

    Fields inherited from class net.solarnetwork.central.dao.mybatis.support.BaseMyBatisDao

    FIRST_ROW, log

    Fields inherited from class org.springframework.dao.support.DaoSupport

    logger

    Fields inherited from interface net.solarnetwork.dao.GenericDao

    ENTITY_EVENT_ENTITY_ID_PROPERTY, ENTITY_EVENT_ENTITY_PROPERTY, ENTITY_EVENT_TOPIC_TEMPLATE, SORT_BY_CREATED_ASCENDING, SORT_BY_CREATED_DESCENDING, SORT_BY_CREATED_ID_ASCENDING, SORT_BY_CREATED_ID_DESCENDING, SORT_BY_ID_ASCENDING, SORT_BY_ID_DESCENDING
  • Constructor Summary

    Constructors
    Constructor
    Description
    BaseMyBatisGenericDaoSupport(Class<? extends T> objectType, Class<? extends K> keyType)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    delete(T entity)
     
    get(K id)
     
    getAll(List<net.solarnetwork.domain.SortDescriptor> sorts)
     
    Get the query name to delete an entity.
    Get the query name to insert an entity.
    Class<? extends K>
    Get the primary key type.
    protected int
    getLastUpdateCount(int count)
    Get the last updated count, supporting batch operations.
    Class<? extends T>
    Get the main domain object type.
    Get the query name to query for all entities.
    Get the query name to query by primary key.
    Get the query name to update an entity.
    protected int
    Delete an entity based on its primary key.
    protected K
    handleInsert(T entity)
    Process the insert of a persisted entity.
    protected K
    handleUpdate(T entity)
    Process the update of a persisted entity.
    protected boolean
    Tell if entities used assigned primary keys.
    protected void
    Process a new unsaved entity for persisting.
    save(T entity)
     
    protected K
    Save an entity that uses an assigned primary key.
    void
    setDelete(String delete)
    Set the query name to delete an entity.
    void
    setInsert(String insert)
    Set the query name to insert an entity.
    void
    setQueryForAll(String queryForAll)
    Set the query name to query for all entities.
    void
    setQueryForId(String queryForId)
    Set the query name to query by primary key.
    void
    setUpdate(String update)
    Set the query name to update an entity.

    Methods inherited from class net.solarnetwork.central.dao.mybatis.support.BaseMyBatisDao

    executeCountQuery, selectFiltered, selectFiltered, selectFiltered, selectFiltered, selectFirst, selectList, selectLong, setSqlSessionFactory

    Methods inherited from class org.mybatis.spring.support.SqlSessionDaoSupport

    checkDaoConfig, createSqlSessionTemplate, getSqlSession, getSqlSessionFactory, getSqlSessionTemplate, setSqlSessionTemplate

    Methods inherited from class org.springframework.dao.support.DaoSupport

    afterPropertiesSet, initDao

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface net.solarnetwork.dao.GenericDao

    entityEventTopic, entityKey
  • Field Details

    • ERROR_CODE_INVALID_QUERY

      public static final int ERROR_CODE_INVALID_QUERY
      Error code to report that a named query was not found.
      See Also:
    • QUERY_FOR_ID

      public static final String QUERY_FOR_ID
      The query name used for GenericDao.get(Object).
      See Also:
    • QUERY_FOR_ALL

      public static final String QUERY_FOR_ALL
      The query name used for GenericDao.getAll(List).
      See Also:
    • INSERT_OBJECT

      public static final String INSERT_OBJECT
      The query name used for inserts in GenericDao.save(Entity).
      See Also:
    • UPDATE_OBJECT

      public static final String UPDATE_OBJECT
      The query name used for updates in GenericDao.save(Entity).
      See Also:
    • DELETE_OBJECT

      public static final String DELETE_OBJECT
      The query name used for updates in GenericDao.delete(Entity).
      See Also:
    • SORT_DESCRIPTORS_PROPERTY

      public static final String SORT_DESCRIPTORS_PROPERTY
      The query property for any custom sort descriptors that are provided.
      See Also:
    • FILTER_PROPERTY

      public static final String FILTER_PROPERTY
      The query property for a filter (search criteria) object.
      See Also:
  • Constructor Details

    • BaseMyBatisGenericDaoSupport

      public BaseMyBatisGenericDaoSupport(Class<? extends T> objectType, Class<? extends K> keyType)
      Constructor.
      Parameters:
      objectType - the entity type
      keyType - the key type
      Throws:
      IllegalArgumentException - if any parameter is null
  • Method Details

    • getObjectType

      public Class<? extends T> getObjectType()
      Get the main domain object type.
      Specified by:
      getObjectType in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
      Returns:
      the object type, never null
    • getKeyType

      public Class<? extends K> getKeyType()
      Get the primary key type.
      Returns:
      the key type, never null
    • get

      public T get(K id)
      Specified by:
      get in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • getAll

      public Collection<T> getAll(List<net.solarnetwork.domain.SortDescriptor> sorts)
      Specified by:
      getAll in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • save

      public K save(T entity)
      Specified by:
      save in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • saveWithAssignedPrimaryKey

      protected K saveWithAssignedPrimaryKey(T entity)
      Save an entity that uses an assigned primary key.

      This method is called by #save(Entity) when isAssignedPrimaryKeys() returns true.

      Parameters:
      entity - the entity to save
      Returns:
      the primary key
    • getLastUpdateCount

      protected int getLastUpdateCount(int count)
      Get the last updated count, supporting batch operations.
      Parameters:
      count - the last returned count from calling SqlSession#update()
      Returns:
      the count, extracted from batch updates if necessary
    • delete

      public void delete(T entity)
      Specified by:
      delete in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • handleDelete

      protected int handleDelete(K id)
      Delete an entity based on its primary key.

      This method is called from delete(Entity).

      Parameters:
      id - the primary key of the entity to delete
      Returns:
      the number of deleted rows
    • preprocessInsert

      protected void preprocessInsert(T entity)
      Process a new unsaved entity for persisting.

      This implementation will set the value of a writable created bean property of the datum instance to the current time if Entity.getCreated() is null. Extending classes may want to extend or modify this behavior.

      Parameters:
      entity - the entity to be persisted
    • handleUpdate

      protected K handleUpdate(T entity)
      Process the update of a persisted entity.

      This implementation merely calls SqlSession.update(String, Object) using the getUpdate() SqlMap.

      Parameters:
      entity - the datum to update
      Returns:
      Identity.getId()
    • handleInsert

      protected K handleInsert(T entity)
      Process the insert of a persisted entity.

      This implementation calls SqlSession.insert(String, Object) using the getInsert() SqlMap.

      Parameters:
      entity - the datum to insert
      Returns:
      the result of the insert statement
    • isAssignedPrimaryKeys

      protected boolean isAssignedPrimaryKeys()
      Tell if entities used assigned primary keys.

      This method returns false. Extending classes can override to change the setting.

      Returns:
      true if entities use assigned primary keys, or false if keys are generated by the database
    • getQueryForId

      public String getQueryForId()
      Get the query name to query by primary key.
      Returns:
      the query name; defaults to QUERY_FOR_ID
    • setQueryForId

      public void setQueryForId(String queryForId)
      Set the query name to query by primary key.
      Parameters:
      queryForId - the query name to set
    • getQueryForAll

      public String getQueryForAll()
      Get the query name to query for all entities.
      Returns:
      the query name; defaults to QUERY_FOR_ALL
    • setQueryForAll

      public void setQueryForAll(String queryForAll)
      Set the query name to query for all entities.
      Parameters:
      queryForAll - the query name to set
    • getInsert

      public String getInsert()
      Get the query name to insert an entity.
      Returns:
      the query name; defaults to INSERT_OBJECT
    • setInsert

      public void setInsert(String insert)
      Set the query name to insert an entity.
      Parameters:
      insert - the query name to set
    • setUpdate

      public void setUpdate(String update)
      Set the query name to update an entity.
      Parameters:
      update - the query name to set
    • getUpdate

      public String getUpdate()
      Get the query name to update an entity.
      Returns:
      the query name; defaults to UPDATE_OBJECT
    • getDelete

      public String getDelete()
      Get the query name to delete an entity.
      Returns:
      the query name; defaults to DELETE_OBJECT
    • setDelete

      public void setDelete(String delete)
      Set the query name to delete an entity.
      Parameters:
      delete - the delete to set