Package org.dspace.core
Class AbstractHibernateDAO<T>
- java.lang.Object
-
- org.dspace.core.AbstractHibernateDAO<T>
-
- Type Parameters:
T- class type
- All Implemented Interfaces:
GenericDAO<T>
- Direct Known Subclasses:
AbstractHibernateDSODAO,BitstreamFormatDAOImpl,ChecksumHistoryDAOImpl,ChecksumResultDAOImpl,ClaimedTaskDAOImpl,CollectionRoleDAOImpl,DOIDAOImpl,EntityTypeDAOImpl,Group2GroupCacheDAOImpl,HandleDAOImpl,HarvestedCollectionDAOImpl,HarvestedItemDAOImpl,InProgressUserDAOImpl,MetadataFieldDAOImpl,MetadataSchemaDAOImpl,MetadataValueDAOImpl,MostRecentChecksumDAOImpl,OpenURLTrackerDAOImpl,OrcidHistoryDAOImpl,OrcidQueueDAOImpl,OrcidTokenDAOImpl,PoolTaskDAOImpl,ProcessDAOImpl,RegistrationDataDAOImpl,RelationshipDAOImpl,RelationshipTypeDAOImpl,RequestItemDAOImpl,ResourcePolicyDAOImpl,SiteDAOImpl,SubscriptionDAOImpl,SubscriptionParameterDAOImpl,SupervisionOrderDaoImpl,SystemWideAlertDAOImpl,VersionDAOImpl,VersionHistoryDAOImpl,WebAppDAOImpl,WorkflowItemRoleDAOImpl,WorkspaceItemDAOImpl,XmlWorkflowItemDAOImpl
public abstract class AbstractHibernateDAO<T> extends Object implements GenericDAO<T>
Hibernate implementation for generic DAO interface. Also includes additional Hibernate calls that are commonly used. Each DAO should extend this class to prevent code duplication.- Author:
- kevinvandevelde at atmire.com
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractHibernateDAO()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcount(javax.persistence.Query query)This method will return the count of items for this query as an integer This query needs to already be in a formate that'll return one record that contains the amountintcount(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, javax.persistence.criteria.CriteriaBuilder criteriaBuilder, javax.persistence.criteria.Root<T> root)This method will return the amount of results that would be generated for this CriteriaQuery as an integerlongcountLong(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, javax.persistence.criteria.CriteriaBuilder criteriaBuilder, javax.persistence.criteria.Root<T> root)This method will return the count of items for this query as a longTcreate(Context context, T t)Create a new instance of this type in the database.javax.persistence.QuerycreateQuery(Context context, String query)Create a parsed query from a query expression.voiddelete(Context context, T t)Remove an instance from the database.List<T>executeCriteriaQuery(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, int maxResults, int offset)This method will return a list of objects to be returned that match the given criteriaQuery and parameters.List<T>findAll(Context context, Class<T> clazz)Fetch all persisted instances of a given object type.List<T>findAll(Context context, Class<T> clazz, Integer limit, Integer offset)Fetch all persisted instances of a given object type.TfindByID(Context context, Class clazz, int id)Fetch the entity identified by its legacy database identifier.TfindByID(Context context, Class clazz, UUID id)Fetch the entity identified by its UUID primary key.List<T>findByX(Context context, Class clazz, Map<String,Object> equals, boolean cacheable, int maxResults, int offset)This method can be used to construct a query for which there needs to be a bunch of equal properties These properties can be passed along in the equals hashmapList<T>findMany(Context context, String query)Execute a JPQL query and return a collection of results.List<T>findMany(Context context, javax.persistence.Query query)Execute a JPA Criteria query and return a collection of results.TfindUnique(Context context, String query)Execute a JPQL query returning a unique result.javax.persistence.criteria.CriteriaBuildergetCriteriaBuilder(Context context)This method should always be used in order to retrieve a CriteriaBuilder for the given contextjavax.persistence.criteria.CriteriaQuery<T>getCriteriaQuery(javax.persistence.criteria.CriteriaBuilder criteriaBuilder, Class<T> clazz)This method should always be used in order to retrieve the CriteriaQuery in order to start creating a query that has to be executedprotected org.hibernate.SessiongetHibernateSession(Context context)The Session used to manipulate entities of this type.Iterator<T>iterate(javax.persistence.Query query)This method will return an Iterator for the given QueryList<T>list(javax.persistence.Query query)This method will be used to return a list of results for the given queryList<T>list(javax.persistence.Query query, int limit, int offset)This method will return a list of results for the given Query and parametersList<T>list(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset)This method will return a list with unique results, no duplicates, made by the given CriteriaQuery and parametersList<T>list(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset, boolean distinct)This method will return a list of results for the given CriteriaQuery and parametersvoidsave(Context context, T t)Persist this instance in the database.TsingleResult(javax.persistence.Query query)This method will return the first result from the given query or null if no results were foundTsingleResult(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery)Retrieve a single result from the query.TuniqueResult(javax.persistence.Query query)This method will return a singular result for the given queryTuniqueResult(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz)Retrieve a unique result from the query.
-
-
-
Method Detail
-
create
public T create(Context context, T t) throws SQLException
Description copied from interface:GenericDAOCreate a new instance of this type in the database.- Specified by:
createin interfaceGenericDAO<T>- Parameters:
context- current DSpace context.t- type to be created.- Returns:
- entity tracking the created instance.
- Throws:
SQLException
-
save
public void save(Context context, T t) throws SQLException
Description copied from interface:GenericDAOPersist this instance in the database.- Specified by:
savein interfaceGenericDAO<T>- Parameters:
context- current DSpace context.t- type created here.- Throws:
SQLException- passed through.
-
getHibernateSession
protected org.hibernate.Session getHibernateSession(Context context) throws SQLException
The Session used to manipulate entities of this type.- Parameters:
context- current DSpace context.- Returns:
- the current Session.
- Throws:
SQLException
-
delete
public void delete(Context context, T t) throws SQLException
Description copied from interface:GenericDAORemove an instance from the database.- Specified by:
deletein interfaceGenericDAO<T>- Parameters:
context- current DSpace context.t- type of the instance to be removed.- Throws:
SQLException- passed through.
-
findAll
public List<T> findAll(Context context, Class<T> clazz) throws SQLException
Description copied from interface:GenericDAOFetch all persisted instances of a given object type.- Specified by:
findAllin interfaceGenericDAO<T>- Parameters:
context- The relevant DSpace Context.clazz- the desired type.- Returns:
- list of DAOs of the same type as clazz
- Throws:
SQLException- if database error
-
findAll
public List<T> findAll(Context context, Class<T> clazz, Integer limit, Integer offset) throws SQLException
Description copied from interface:GenericDAOFetch all persisted instances of a given object type.- Specified by:
findAllin interfaceGenericDAO<T>- Parameters:
context- The relevant DSpace Context.clazz- the desired type.limit- paging limitoffset- paging offset- Returns:
- list of DAOs of the same type as clazz
- Throws:
SQLException- if database error
-
findUnique
public T findUnique(Context context, String query) throws SQLException
Description copied from interface:GenericDAOExecute a JPQL query returning a unique result.- Specified by:
findUniquein interfaceGenericDAO<T>- Parameters:
context- The relevant DSpace Context.query- JPQL query string- Returns:
- a DAO specified by the query string
- Throws:
SQLException- if database error
-
findByID
public T findByID(Context context, Class clazz, UUID id) throws SQLException
Description copied from interface:GenericDAOFetch the entity identified by its UUID primary key.- Specified by:
findByIDin interfaceGenericDAO<T>- Parameters:
context- current DSpace context.clazz- class of entity to be found.id- primary key of the database record.- Returns:
- the found entity.
- Throws:
SQLException
-
findByID
public T findByID(Context context, Class clazz, int id) throws SQLException
Description copied from interface:GenericDAOFetch the entity identified by its legacy database identifier.- Specified by:
findByIDin interfaceGenericDAO<T>- Parameters:
context- current DSpace context.clazz- class of entity to be found.id- legacy database record ID.- Returns:
- the found entity.
- Throws:
SQLException- passed through.
-
findMany
public List<T> findMany(Context context, String query) throws SQLException
Description copied from interface:GenericDAOExecute a JPQL query and return a collection of results.- Specified by:
findManyin interfaceGenericDAO<T>- Parameters:
context- The relevant DSpace Context.query- JPQL query string- Returns:
- list of DAOs specified by the query string
- Throws:
SQLException- if database error
-
findMany
public List<T> findMany(Context context, javax.persistence.Query query) throws SQLException
Execute a JPA Criteria query and return a collection of results.- Parameters:
context- The relevant DSpace Context.query- JPQL query string- Returns:
- list of DAOs specified by the query string
- Throws:
SQLException- if database error
-
createQuery
public javax.persistence.Query createQuery(Context context, String query) throws SQLException
Create a parsed query from a query expression.- Parameters:
context- current DSpace context.query- textual form of the query.- Returns:
- parsed form of the query.
- Throws:
SQLException
-
list
public List<T> list(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset) throws SQLException
This method will return a list with unique results, no duplicates, made by the given CriteriaQuery and parameters- Parameters:
context- The standard DSpace context objectcriteriaQuery- The CriteriaQuery for which this list will be retrievedcacheable- Whether or not this query should be cacheableclazz- The class for which this CriteriaQuery will be executed onmaxResults- The maximum amount of results that will be returned for this CriteriaQueryoffset- The offset to be used for the CriteriaQuery- Returns:
- A list of distinct results as depicted by the CriteriaQuery and parameters
- Throws:
SQLException
-
list
public List<T> list(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset, boolean distinct) throws SQLException
This method will return a list of results for the given CriteriaQuery and parameters- Parameters:
context- The standard DSpace context objectcriteriaQuery- The CriteriaQuery to be used to find the list of resultscacheable- A boolean value indicating whether this query should be cached or notclazz- The class on which the CriteriaQuery will searchmaxResults- The maximum amount of results to be returnedoffset- The offset to be used for the CriteriaQuerydistinct- A boolean value indicating whether this list should be distinct or not- Returns:
- A list of results determined by the CriteriaQuery and parameters
- Throws:
SQLException
-
list
public List<T> list(javax.persistence.Query query)
This method will be used to return a list of results for the given query- Parameters:
query- The query for which the resulting list will be returned- Returns:
- The list of results for the given query
-
list
public List<T> list(javax.persistence.Query query, int limit, int offset)
This method will return a list of results for the given Query and parameters- Parameters:
query- The query for which the resulting list will be returnedlimit- The maximum amount of results to be returnedoffset- The offset to be used for the Query- Returns:
- A list of results determined by the Query and parameters
-
uniqueResult
public T uniqueResult(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz) throws SQLException
Retrieve a unique result from the query. If multiple results CAN be retrieved an exception will be thrown, so only use when the criteria state uniqueness in the database.- Parameters:
context- current DSpace session.criteriaQuery- JPA criteriacacheable- whether or not this query should be cacheable.clazz- type of object that should match the query.- Returns:
- the single model object specified by the criteria,
or
nullif none match. - Throws:
SQLException- passed through.IllegalArgumentException- if multiple objects match.
-
singleResult
public T singleResult(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery) throws SQLException
Retrieve a single result from the query. Best used if you expect a single result, but this isn't enforced on the database.- Parameters:
context- current DSpace sessioncriteriaQuery- JPA criteria- Returns:
- a DAO specified by the criteria
- Throws:
SQLException- passed through.
-
singleResult
public T singleResult(javax.persistence.Query query)
This method will return the first result from the given query or null if no results were found- Parameters:
query- The query that is to be executed- Returns:
- One result from the given query or null if none was found
-
uniqueResult
public T uniqueResult(javax.persistence.Query query)
This method will return a singular result for the given query- Parameters:
query- The query for which a single result will be given- Returns:
- The single result for this query
-
iterate
public Iterator<T> iterate(javax.persistence.Query query)
This method will return an Iterator for the given Query- Parameters:
query- The query for which an Iterator will be made- Returns:
- The Iterator for the results of this query
-
count
public int count(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, javax.persistence.criteria.CriteriaBuilder criteriaBuilder, javax.persistence.criteria.Root<T> root) throws SQLException
This method will return the amount of results that would be generated for this CriteriaQuery as an integer- Parameters:
context- The standard DSpace Context objectcriteriaQuery- The CriteriaQuery for which this result will be retrievedcriteriaBuilder- The CriteriaBuilder that accompanies the CriteriaQueryroot- The root that'll determine on which class object we need to calculate the result- Returns:
- The amount of results that would be found by this CriteriaQuery as an integer value
- Throws:
SQLException
-
count
public int count(javax.persistence.Query query)
This method will return the count of items for this query as an integer This query needs to already be in a formate that'll return one record that contains the amount- Parameters:
query- The query for which the amount of results will be returned.- Returns:
- The amount of results
-
countLong
public long countLong(Context context, javax.persistence.criteria.CriteriaQuery criteriaQuery, javax.persistence.criteria.CriteriaBuilder criteriaBuilder, javax.persistence.criteria.Root<T> root) throws SQLException
This method will return the count of items for this query as a long- Parameters:
context- The standard DSpace Context objectcriteriaQuery- The CriteriaQuery for which the amount of results will be retrievedcriteriaBuilder- The CriteriaBuilder that goes along with this CriteriaQueryroot- The root created for a DSpace class on which this query will search- Returns:
- A long value that depicts the amount of results this query has found
- Throws:
SQLException
-
getCriteriaQuery
public javax.persistence.criteria.CriteriaQuery<T> getCriteriaQuery(javax.persistence.criteria.CriteriaBuilder criteriaBuilder, Class<T> clazz)
This method should always be used in order to retrieve the CriteriaQuery in order to start creating a query that has to be executed- Parameters:
criteriaBuilder- The CriteriaBuilder for which this CriteriaQuery will be constructedclazz- The class that this CriteriaQuery will be constructed for- Returns:
- A CriteriaQuery on which a query can be built
-
getCriteriaBuilder
public javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder(Context context) throws SQLException
This method should always be used in order to retrieve a CriteriaBuilder for the given context- Parameters:
context- The standard DSpace Context class for which a CriteriaBuilder will be made- Returns:
- A CriteriaBuilder that can be used to create the query
- Throws:
SQLException
-
executeCriteriaQuery
public List<T> executeCriteriaQuery(Context context, javax.persistence.criteria.CriteriaQuery<T> criteriaQuery, boolean cacheable, int maxResults, int offset) throws SQLException
This method will return a list of objects to be returned that match the given criteriaQuery and parameters. The maxResults and offSet can be circumvented by entering the value -1 for them.- Parameters:
context- The standard context DSpace objectcriteriaQuery- The CriteriaQuery that will be used for executing the querycacheable- Whether or not this query is able to be cachedmaxResults- The maximum amount of results that this query will return This can be circumvented by passing along -1 as the valueoffset- The offset to be used in this query This can be circumvented by passing along -1 as the value- Returns:
- This will return a list of objects that conform to the made query
- Throws:
SQLException
-
findByX
public List<T> findByX(Context context, Class clazz, Map<String,Object> equals, boolean cacheable, int maxResults, int offset) throws SQLException
This method can be used to construct a query for which there needs to be a bunch of equal properties These properties can be passed along in the equals hashmap- Parameters:
context- The standard DSpace context objectclazz- The class on which the criteriaQuery will be builtequals- A hashmap that can be used to store the String representation of the column and the value that should match that in the DBcacheable- A boolean indicating whether this query should be cacheable or notmaxResults- The max amount of results to be returned by this queryoffset- The offset to be used in this query- Returns:
- Will return a list of objects that correspond with the constructed query and parameters
- Throws:
SQLException
-
-