Package ubic.gemma.persistence.service
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
,CoexpressionNodeDegreeDaoImpl
,CompoundDaoImpl
,ContactDaoImpl
,DifferentialExpressionEvidenceDaoImpl
,DifferentialExpressionResultDaoImpl
,ExperimentalDesignDaoImpl
,ExperimentalEvidenceDaoImpl
,ExpressionExperimentSubSetDaoImpl
,ExternalDatabaseDaoImpl
,Gene2GOAssociationDaoImpl
,GeneAliasDaoImpl
,GeneDiffExMetaAnalysisDaoImpl
,GenericEvidenceDaoImpl
,GenericExperimentDaoImpl
,GeneSetDaoImpl
,GeneSetMemberDaoImpl
,LiteratureEvidenceDaoImpl
,PersonDaoImpl
,PhenotypeAssociationDaoImpl
,PrincipalComponentAnalysisDaoImpl
,ProtocolDaoImpl
,ReferenceAssociationDaoImpl
,SingleExperimentAnalysisDaoBase
,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 Summary
Fields Modifier and Type Field Description protected Class<? extends T>
elementClass
protected static org.apache.commons.logging.Log
log
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractDao(Class<? extends T> elementClass, SessionFactory sessionFactory)
protected
AbstractDao(Class<? extends T> elementClass, SessionFactory sessionFactory, ClassMetadata classMetadata)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
countAll()
Counts all instances of specific class in the persitent storage.Collection<T>
create(Collection<T> entities)
Crates all the given entities in the persistent storage.T
create(T entity)
Create an object.T
find(T entity)
Does a look up for the given entity in the persistent storage, usually looking for a specific identifier ( either id or a string property).protected List<T>
findByProperty(String propertyName, Object propertyValue)
Does a search on given property and its value.protected List<T>
findByPropertyIn(String propertyName, Collection<?> propertyValues)
Perform a search on a given property and all its possible values.protected T
findOneByProperty(String propertyName, Object propertyValue)
Retrieve one entity whose given property matches the given value.T
findOrCreate(T entity)
Calls the find method, and if this method returns null, creates a new instance in the persistent storage.Class<? extends T>
getElementClass()
Obtain the element class ofBaseDao
.String
getIdentifierPropertyName()
Obtain the identifiable property name forBaseDao
.protected SessionFactory
getSessionFactory()
T
load(Long id)
Loads the entity with given id from the persistent storage.Collection<T>
load(Collection<Long> ids)
Loads entities with given ids form the persistent storage.Collection<T>
loadAll()
Loads all instanced of specific class from the persistent storage.T
loadReference(Long id)
Load reference for an entity.Collection<T>
loadReference(Collection<Long> ids)
Load references for all the given IDs.void
remove(Long id)
Remove a persistent instance based on its ID.void
remove(Collection<T> entities)
void
remove(T entity)
Remove a persistent instanceCollection<T>
save(Collection<T> entities)
Save all the given entities in the persistent storage.T
save(T entity)
Create or update an entity whether it is transient.void
update(Collection<T> entities)
void
update(T entity)
-
-
-
Field Detail
-
log
protected static final org.apache.commons.logging.Log log
-
elementClass
protected final Class<? extends T extends Identifiable> elementClass
-
-
Constructor Detail
-
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 aboutAbstractDao
-
-
Method Detail
-
getElementClass
public Class<? extends T> getElementClass()
Description copied from interface:BaseDao
Obtain the element class ofBaseDao
.- Specified by:
getElementClass
in interfaceBaseDao<T extends Identifiable>
-
getIdentifierPropertyName
public String getIdentifierPropertyName()
Description copied from interface:BaseDao
Obtain the identifiable property name forBaseDao
.- Specified by:
getIdentifierPropertyName
in interfaceBaseDao<T extends Identifiable>
-
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 interfaceBaseDao<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 interfaceBaseDao<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 interfaceBaseDao<T extends Identifiable>
- See Also:
Session.persist(Object)
,Session.merge(Object)
-
save
@OverridingMethodsMustInvokeSuper public T save(T entity)
Description copied from interface:BaseDao
Create or update an entity whether it is transient.Unlike
BaseDao.update(Object)
, this method does not attach the given entity to the persistence context and the returned value must be used instead.- Specified by:
save
in interfaceBaseDao<T extends Identifiable>
- See Also:
Session.persist(Object)
,Session.merge(Object)
-
load
public Collection<T> load(Collection<Long> ids)
Description copied from interface:BaseDao
Loads entities with given ids form the persistent storage.- Specified by:
load
in interfaceBaseDao<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 interfaceBaseDao<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:
Session.get(Class, Serializable)
-
loadAll
public Collection<T> loadAll()
Description copied from interface:BaseDao
Loads all instanced of specific class from the persistent storage.- Specified by:
loadAll
in interfaceBaseDao<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 interfaceBaseDao<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 interfaceBaseDao<T extends Identifiable>
- See Also:
Session.load(Object, Serializable)
-
countAll
public long countAll()
Description copied from interface:BaseDao
Counts all instances of specific class in the persitent storage.- Specified by:
countAll
in interfaceBaseDao<T extends Identifiable>
- Returns:
- number that is the amount of instances currently accessible.
-
remove
public void remove(Collection<T> entities)
- Specified by:
remove
in interfaceBaseDao<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 forSecurable
, 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 interfaceBaseDao<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 interfaceBaseDao<T extends Identifiable>
- Parameters:
entity
- the entity to be removed
-
update
public void update(Collection<T> entities)
- Specified by:
update
in interfaceBaseDao<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 interfaceBaseDao<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 interfaceBaseDao<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 interfaceBaseDao<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()
-
findOneByProperty
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.
-
-