Class AbstractDao<T extends Identifiable>

java.lang.Object
ubic.gemma.persistence.service.AbstractDao<T>
All Implemented Interfaces:
BaseDao<T>
Direct Known Subclasses:
AbstractDesignElementDataVectorDao, AbstractVoEnabledDao, AnnotationAssociationDaoImpl, AuditEventDaoImpl, AuditTrailDaoImpl, BlatAssociationDaoImpl, ChromosomeDaoImpl, CoexpressionAnalysisDaoImpl, CoexpressionNodeDegreeDaoImpl, CompoundDaoImpl, ContactDaoImpl, DifferentialExpressionResultDaoImpl, ExperimentalDesignDaoImpl, ExpressionExperimentSubSetDaoImpl, ExternalDatabaseDaoImpl, Gene2GOAssociationDaoImpl, GeneAliasDaoImpl, GeneDiffExMetaAnalysisDaoImpl, GeneSetDaoImpl, GeneSetMemberDaoImpl, PersonDaoImpl, PrincipalComponentAnalysisDaoImpl, ProtocolDaoImpl, ReferenceAssociationDaoImpl, StatementDaoImpl, UnitDaoImpl, UserDaoImpl, UserGroupDaoImpl

public abstract class AbstractDao<T extends Identifiable> extends Object implements BaseDao<T>
AbstractDao can find the generic type at runtime and simplify the code implementation of the BaseDao interface
Author:
Anton, Nicolas
  • Field Details

    • log

      protected final org.apache.commons.logging.Log log
  • Constructor Details

    • AbstractDao

      protected AbstractDao(Class<? extends T> elementClass, SessionFactory sessionFactory)
    • AbstractDao

      protected AbstractDao(Class<? extends T> elementClass, SessionFactory sessionFactory, ClassMetadata classMetadata)
      Parameters:
      classMetadata - the class metadata to use to retrieve information about AbstractDao
  • Method Details

    • getElementClass

      public Class<? extends T> getElementClass()
      Description copied from interface: BaseDao
      Obtain the element class of BaseDao.
      Specified by:
      getElementClass in interface BaseDao<T extends Identifiable>
    • getEntityName

      protected String getEntityName()
    • getIdentifierPropertyName

      protected String getIdentifierPropertyName()
    • create

      public Collection<T> create(Collection<T> entities)
      Description copied from interface: BaseDao
      Crates all the given entities in the persistent storage.
      Specified by:
      create in interface BaseDao<T extends Identifiable>
      Parameters:
      entities - the entities to be crated.
      Returns:
      collection of entities representing the instances in the persistent storage that were created.
    • create

      @OverridingMethodsMustInvokeSuper public T create(T entity)
      Description copied from interface: BaseDao
      Create an object. If the entity type is immutable, this may also remove any existing entities identified by an appropriate 'find' method.
      Specified by:
      create in interface BaseDao<T extends Identifiable>
      Parameters:
      entity - the entity to create
      Returns:
      the persistent version of the entity
    • save

      public Collection<T> save(Collection<T> entities)
      Description copied from interface: BaseDao
      Save all the given entities in the persistent storage.

      Unlike BaseDao.update(Collection), this method does not attach the given entities to the persistence context; the returned values must be used instead.

      Specified by:
      save in interface BaseDao<T extends Identifiable>
      See Also:
    • save

      @OverridingMethodsMustInvokeSuper public T save(T entity)
      Description copied from interface: BaseDao
      Create or update an entity whether it is transient.

      Unlike BaseDao.update(Identifiable), this method does not attach the given entity to the persistence context and the returned value must be used instead.

      Specified by:
      save in interface BaseDao<T extends Identifiable>
      See Also:
    • load

      public Collection<T> load(Collection<Long> ids)
      This implementation is temporary and attempts to best replicate the behaviour of loading entities by multiple IDs introduced in Hibernate 5. Read more about this.
      Specified by:
      load in interface BaseDao<T extends Identifiable>
      Parameters:
      ids - the IDs of entities to be loaded. If some IDs are not found or null, they are skipped.
      Returns:
      collection of entities with given ids.
    • load

      public T load(Long id)
      Description copied from interface: BaseDao
      Loads the entity with given id from the persistent storage.
      Specified by:
      load in interface BaseDao<T extends Identifiable>
      Parameters:
      id - the id of entity to load.
      Returns:
      the entity with given ID, or null if such entity does not exist or if the passed ID was null
      See Also:
    • loadAll

      public Collection<T> loadAll()
      Description copied from interface: BaseDao
      Loads all instanced of specific class from the persistent storage.
      Specified by:
      loadAll in interface BaseDao<T extends Identifiable>
      Returns:
      a collection containing all instances that are currently accessible.
    • loadReference

      public Collection<T> loadReference(Collection<Long> ids)
      Description copied from interface: BaseDao
      Load references for all the given IDs.

      Entities already in the session will be returned directly.

      Specified by:
      loadReference in interface BaseDao<T extends Identifiable>
    • loadReference

      @Nonnull public T loadReference(Long id)
      Description copied from interface: BaseDao
      Load reference for an entity.

      If the entity is already in the session, it will be returned instead. Note that unlike BaseDao.load(Long), this method will not return null if the entity does not exist.

      You may freely access the Identifiable.getId() field without triggering proxy initialization.

      Specified by:
      loadReference in interface BaseDao<T extends Identifiable>
      See Also:
    • reload

      @Nonnull public T reload(T entity)
      Description copied from interface: BaseDao
      Reload an entity from the persistent storage.

      This does nothing if the entity is already in the session.

      Specified by:
      reload in interface BaseDao<T extends Identifiable>
    • reload

      @Nonnull public Collection<T> reload(Collection<T> entities)
      Description copied from interface: BaseDao
      Reload an entity from the persistent storage.

      This does nothing for entities already in the session.

      Specified by:
      reload in interface BaseDao<T extends Identifiable>
    • countAll

      public long countAll()
      Description copied from interface: BaseDao
      Counts all instances of specific class in the persistent storage.
      Specified by:
      countAll in interface BaseDao<T extends Identifiable>
      Returns:
      number that is the amount of instances currently accessible.
    • streamAll

      public Stream<T> streamAll()
      Description copied from interface: BaseDao
      Stream all instance of BaseDao from the persistent storage.
      Specified by:
      streamAll in interface BaseDao<T extends Identifiable>
    • streamAll

      public Stream<T> streamAll(boolean createNewSession)
      Description copied from interface: BaseDao
      Stream all instances of BaseDao from the persistent storage.
      Specified by:
      streamAll in interface BaseDao<T extends Identifiable>
      Parameters:
      createNewSession - whether to create a new session for the stream, it will be closed when the stream is closed
    • streamQuery

      protected <U> Stream<U> streamQuery(Function<Session,Query> queryCreator, Class<U> resultType, int fetchSize, boolean useCursorFetchIfSupported, boolean isStateless, boolean createNewSession)
      Produce a stream over a Query with a new session if desired.
      Parameters:
      createNewSession - if true, a new session is created and will be closed when the stream is closed. Be extremely careful with the resulting stream. Use a try-with-resources block to ensure the session is closed properly.
      See Also:
    • remove

      public void remove(Collection<T> entities)
      Specified by:
      remove in interface BaseDao<T extends Identifiable>
    • remove

      public void remove(Long id)
      Description copied from interface: BaseDao
      Remove a persistent instance based on its ID.

      The implementer is trusted to know what type of object to remove.

      Note that this method is to be avoided for Securable, because it will leave cruft in the ACL tables. We may fix this by having this method return the removed object.

      Specified by:
      remove in interface BaseDao<T extends Identifiable>
      Parameters:
      id - the ID of the entity to be removed
    • remove

      @OverridingMethodsMustInvokeSuper public void remove(T entity)
      Description copied from interface: BaseDao
      Remove a persistent instance
      Specified by:
      remove in interface BaseDao<T extends Identifiable>
      Parameters:
      entity - the entity to be removed
    • update

      public void update(Collection<T> entities)
      Specified by:
      update in interface BaseDao<T extends Identifiable>
      Parameters:
      entities - Update the entities. Not supported if the entities are immutable.
    • update

      @OverridingMethodsMustInvokeSuper public void update(T entity)
      Specified by:
      update in interface BaseDao<T extends Identifiable>
      Parameters:
      entity - Update the entity. Not supported if the entity is immutable.
    • find

      public T find(T entity)
      Description copied from interface: BaseDao
      Does a look up for the given entity in the persistent storage, usually looking for a specific identifier ( either id or a string property).
      Specified by:
      find in interface BaseDao<T extends Identifiable>
      Parameters:
      entity - the entity to look for.
      Returns:
      an entity that was found in the persistent storage, or null if no such entity was found.
    • findOrCreate

      public T findOrCreate(T entity)
      Description copied from interface: BaseDao
      Calls the find method, and if this method returns null, creates a new instance in the persistent storage.
      Specified by:
      findOrCreate in interface BaseDao<T extends Identifiable>
      Parameters:
      entity - the entity to look for and persist if not found.
      Returns:
      the given entity, guaranteed to be representing an entity present in the persistent storage.
    • getSessionFactory

      protected final SessionFactory getSessionFactory()
    • getBatchSize

      protected final int getBatchSize()
    • findOneByProperty

      @Nullable protected T findOneByProperty(String propertyName, Object propertyValue)
      Retrieve one entity whose given property matches the given value.

      Note: the property should have a unique index, otherwise a NonUniqueResultException will be raised.

      Parameters:
      propertyName - the name of property to be matched.
      propertyValue - the value to look for.
      Returns:
      an entity whose property matched the given value
    • findByProperty

      protected List<T> findByProperty(String propertyName, Object propertyValue)
      Does a search on given property and its value.
      Parameters:
      propertyName - the name of property to be matched.
      propertyValue - the value to look for.
      Returns:
      an entity whose property first matched the given value.
    • findByPropertyIn

      protected List<T> findByPropertyIn(String propertyName, Collection<?> propertyValues)
      Perform a search on a given property and all its possible values.