Class HibernateObjectDao<T extends com.atlassian.core.bean.EntityObject>

java.lang.Object
org.springframework.dao.support.DaoSupport
com.atlassian.confluence.core.persistence.hibernate.HibernateObjectDao<T>
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
ConfluenceHibernateObjectDao, DefaultGlobalDescriptionDao, HibernateAbstractPageDao, HibernateAttachmentDataDao, HibernateAuditRecordDao, HibernateContentPermissionSetDao, HibernateDraftDao, HibernateLabelDao, HibernateVersionHistoryDao, HibernateZduStatusDao

public abstract class HibernateObjectDao<T extends com.atlassian.core.bean.EntityObject> extends org.springframework.dao.support.DaoSupport
Generic DAO for hibernate objects. Takes care of indexing and unindexing, and setting creation and update times for objects.

For this reason, you should always do object CRUD through the DAO, you shouldn't trust Hibernate to do it for you: Hibernate will save the object, but skip the additional stuff the DAO does.

Subclasses will need to implement the getPersistentClass() method to return the class that the DAO is serving: that way getByClassId() will perform correctly.

Subclasses should also override updateModificationData(EntityObject) if they want to perform any additional modifications on objects that are being saved through the DAO. Just remember to call super. (updateModificationData will not be called if an object is saved through #saveRawEntity(EntityObject))

Since:
6.1.0
  • Constructor Details

    • HibernateObjectDao

      public HibernateObjectDao()
  • Method Details

    • getPersistentClass

      protected abstract Class<T> getPersistentClass()
    • getSessionFactory

      public final org.hibernate.SessionFactory getSessionFactory()
    • setSessionFactory

      public final void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
    • getHibernateTemplate

      @Deprecated(forRemoval=true) public final org.springframework.orm.hibernate5.HibernateTemplate getHibernateTemplate()
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 8.6 Use getSessionFactory() and SessionFactory.getCurrentSession()
    • setHibernateTemplate

      @Deprecated(forRemoval=true) public final void setHibernateTemplate(org.springframework.orm.hibernate5.HibernateTemplate hibernateTemplate)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 8.6
    • createHibernateTemplate

      @Deprecated(forRemoval=true) protected org.springframework.orm.hibernate5.HibernateTemplate createHibernateTemplate(org.hibernate.SessionFactory sessionFactory)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 8.6
    • getByClassId

      protected @Nullable T getByClassId(long id)
      Retrieve the object with a particular id from the database. The object must be of the same class as the DAO's getPersistentClass() method returns.
      Parameters:
      id - the id of the object to look up
      Returns:
      the corresponding object, or null if the object does not exist with the appropriate class and id.
    • findAll

      public @NonNull List<T> findAll()
      Find all objects of the class provided by getPersistentClass()
      Returns:
      a list of all objects of the appropriate class, or the empty list if no objects are found
    • findAllSorted

      public @NonNull List<T> findAllSorted(String sortField)
    • findAllSorted

      public @NonNull List<T> findAllSorted(String sortField, boolean cacheable, int offset, int maxResultCount)
    • save

      public void save(T objectToSave)
      Save an object. The object's last-modified time will be updated, and if the object is searchable it will be indexed.
      Parameters:
      objectToSave - the object to save
    • saveEntity

      @Deprecated(forRemoval=true) public final void saveEntity(T objectToSave)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 9.0 use save(T)
    • updateModificationData

      protected void updateModificationData(T objectToSave)
      Perform any modifications that need to be done to an object every time it is saved. The default implementation updates the last-modified timestamp, and sets the creation timestamp if it has not yet been set.
      Parameters:
      objectToSave - the object that is about to be saved
    • saveRaw

      protected void saveRaw(T objectToSave)
      Save the object without updateModificationData being called. Modification/creation time will not be set, but the object will still be indexed if it is searchable.
      Parameters:
      objectToSave - object to save
    • remove

      protected void remove(T objectToRemove)
      Remove an object from the database
      Parameters:
      objectToRemove - the object to remove
    • publishEvent

      protected void publishEvent(IndexerEventListener.VisitorEvent event)
    • reIndex

      protected void reIndex(com.atlassian.core.bean.EntityObject objectToSave)
    • findNamedQuery

      @Deprecated protected List findNamedQuery(String queryName)
    • findNamedQuery

      protected List findNamedQuery(String queryName, HibernateObjectDao.Cacheability cacheability)
    • findNamedQuery

      protected List findNamedQuery(String queryName, HibernateObjectDao.Cacheability cacheability, int maxResultCount)
    • findNamedQueryStringParam

      protected List findNamedQueryStringParam(String queryName, String paramName, Object paramValue)
    • findNamedQueryStringParam

      protected List findNamedQueryStringParam(String queryName, String paramName, Object paramValue, HibernateObjectDao.Cacheability cacheability)
    • findNamedQueryStringParam

      protected List findNamedQueryStringParam(String queryName, String paramName, Object paramValue, HibernateObjectDao.Cacheability cacheability, int maxResultCount)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, HibernateObjectDao.Cacheability cacheability)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, HibernateObjectDao.Cacheability cacheability, int maxResultCount)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value, HibernateObjectDao.Cacheability cacheability)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value, HibernateObjectDao.Cacheability cacheability, int maxResultCount)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value, String param4Name, Object param4Value)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value, String param4Name, Object param4Value, HibernateObjectDao.Cacheability cacheability)
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, String paramName, Object paramValue, String param2Name, Object param2Value, String param3Name, Object param3Value, String param4Name, Object param4Value, HibernateObjectDao.Cacheability cacheability, int maxResultCount)
    • findNamedQueryStringParams

      @Deprecated protected List findNamedQueryStringParams(String queryName, boolean cacheable, int offset, int maxResultCount, Object... paramNamesAndValues)
      Deprecated.
      since 5.8 Use the equivalent method that uses the HibernateObjectDao.Cacheability enum instead of the cacheable boolean flag
    • findNamedQueryStringParams

      protected List findNamedQueryStringParams(String queryName, HibernateObjectDao.Cacheability cacheability, int offset, int maxResultCount, Object... paramNamesAndValues)
    • getCountResult

      @Deprecated protected int getCountResult(@Nullable List<Number> results)
      Deprecated.
      since 7.0.1. Use DataAccessUtils.intResult(Collection) instead.
    • findSingleObject

      @Deprecated protected <T extends com.atlassian.core.bean.EntityObject> @Nullable T findSingleObject(List<T> results)
      Deprecated.
      since 5.2. Entity ID comparison is not a recommended way to select a result. Use uniqueResult(java.util.List), Query.uniqueResult(), Criteria.uniqueResult(), or manually handle it yourself.
      Attempts to select a single result from a list of results. If the list has only one item, then that item is returned. If the list has more than one item, then the item with the lowest ID is returned. If the list is empty, null is returned.
    • uniqueResult

      protected <E> E uniqueResult(List<E> results) throws org.hibernate.NonUniqueResultException
      Attempts to select a single result from a list of results. If the list has only one item, then that item is returned. If the list has more than one item, then an exception will be thrown unless all items in the list are the same object. If the list is empty, null is returned.
      Returns:
      The single unique object in the list, or null if list is empty
      Throws:
      org.hibernate.NonUniqueResultException - If more than one unique objects is found in the list.
    • checkDaoConfig

      protected final void checkDaoConfig()
      Specified by:
      checkDaoConfig in class org.springframework.dao.support.DaoSupport
    • applyTransactionTimeout

      protected static void applyTransactionTimeout(org.hibernate.query.Query query, org.hibernate.SessionFactory sessionFactory)
    • applyTransactionTimeout

      protected static void applyTransactionTimeout(org.hibernate.Criteria criteria, org.hibernate.SessionFactory sessionFactory)