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,LDNMessageDaoImpl,MetadataFieldDAOImpl,MetadataSchemaDAOImpl,MetadataValueDAOImpl,MostRecentChecksumDAOImpl,NotifyPatternToTriggerDaoImpl,NotifyServiceDaoImpl,NotifyServiceInboundPatternDaoImpl,OpenURLTrackerDAOImpl,OrcidHistoryDAOImpl,OrcidQueueDAOImpl,OrcidTokenDAOImpl,PoolTaskDAOImpl,ProcessDAOImpl,QAEventsDAOImpl,RegistrationDataDAOImpl,RegistrationDataMetadataDAOImpl,RelationshipDAOImpl,RelationshipTypeDAOImpl,RequestItemDAOImpl,ResourcePolicyDAOImpl,SiteDAOImpl,SubscriptionDAOImpl,SubscriptionParameterDAOImpl,SupervisionOrderDaoImpl,SystemWideAlertDAOImpl,VersionDAOImpl,VersionHistoryDAOImpl,WebAppDAOImpl,WorkflowItemRoleDAOImpl,WorkspaceItemDAOImpl,XmlWorkflowItemDAOImpl
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 -
Method Summary
Modifier and TypeMethodDescriptionintcount(jakarta.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 format that'll return one record that contains the amountintcount(Context context, jakarta.persistence.criteria.CriteriaQuery criteriaQuery, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder, jakarta.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, jakarta.persistence.criteria.CriteriaQuery criteriaQuery, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder, jakarta.persistence.criteria.Root<T> root) This method will return the count of items for this query as a longCreate a new instance of this type in the database.jakarta.persistence.QuerycreateQuery(Context context, jakarta.persistence.criteria.CriteriaQuery criteriaQuery) Create a Query object from a CriteriaQueryjakarta.persistence.QuerycreateQuery(Context context, String query) Create a parsed query from a query expression.voidRemove an instance from the database.executeCriteriaQuery(Context context, jakarta.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.Fetch all persisted instances of a given object type.Fetch all persisted instances of a given object type.Fetch the entity identified by its legacy database identifier.Fetch the entity identified by its String primary key.Fetch the entity identified by its UUID primary key.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 hashmapExecute a JPA Criteria query and return a collection of results.Execute a JPQL query and return a collection of results.findUnique(Context context, String query) Execute a JPQL query returning a unique result.jakarta.persistence.criteria.CriteriaBuildergetCriteriaBuilder(Context context) This method should always be used in order to retrieve a CriteriaBuilder for the given contextjakarta.persistence.criteria.CriteriaQuery<T>getCriteriaQuery(jakarta.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.iterate(jakarta.persistence.Query query) This method will return an Iterator for the given Querylist(jakarta.persistence.Query query) This method will be used to return a list of results for the given querylist(jakarta.persistence.Query query, int limit, int offset) This method will return a list of results for the given Query and parameterslist(Context context, jakarta.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(Context context, jakarta.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 parametersvoidPersist this instance in the database.singleResult(jakarta.persistence.Query query) This method will return the first result from the given query or null if no results were foundsingleResult(Context context, jakarta.persistence.criteria.CriteriaQuery criteriaQuery) Retrieve a single result from the query.uniqueResult(jakarta.persistence.Query query) This method will return a singular result for the given queryuniqueResult(Context context, jakarta.persistence.criteria.CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz) Retrieve a unique result from the query.
-
Constructor Details
-
AbstractHibernateDAO
protected AbstractHibernateDAO()
-
-
Method Details
-
create
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
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
The Session used to manipulate entities of this type.- Parameters:
context- current DSpace context.- Returns:
- the current Session.
- Throws:
SQLException
-
delete
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
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
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
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
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.
-
findByID
Description copied from interface:GenericDAOFetch the entity identified by its String 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
-
findMany
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
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
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, jakarta.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, jakarta.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
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
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, jakarta.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, jakarta.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
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
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
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, jakarta.persistence.criteria.CriteriaQuery criteriaQuery, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder, jakarta.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(jakarta.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 format 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, jakarta.persistence.criteria.CriteriaQuery criteriaQuery, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder, jakarta.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 jakarta.persistence.criteria.CriteriaQuery<T> getCriteriaQuery(jakarta.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 jakarta.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, jakarta.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 SQLExceptionThis 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
-
createQuery
public jakarta.persistence.Query createQuery(Context context, jakarta.persistence.criteria.CriteriaQuery criteriaQuery) throws SQLException Create a Query object from a CriteriaQuery- Parameters:
context- current ContextcriteriaQuery- CriteriaQuery built via CriteriaBuilder- Returns:
- corresponding Query
- Throws:
SQLException- if error occurs
-