Class HibernateDao<T,ID extends Serializable>
java.lang.Object
com.peterphi.std.guice.hibernate.dao.HibernateDao<T,ID>
- Type Parameters:
T
- entity typeID
- primary key type
- All Implemented Interfaces:
Dao<T,
ID>
- Direct Known Subclasses:
OAuthDelegatedTokenDaoImpl
,OAuthServiceDaoImpl
,OAuthSessionContextDaoImpl
,OAuthSessionDaoImpl
,RoleDaoImpl
,UserDaoImpl
The default implementation of a Dao for Hibernate; often it is necessary to extend this to produce richer queries
This DAO can be used in two main ways:
This DAO can be used in two main ways:
- With singleton instances of this class created for each entity (where only simple CRUD methods are required)
- With subclasses of this HibernateDao implementing additional queries (often with corresponding extensions to the Dao type)
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionlong
Return the total number of results for a given query (ignoring offset/limit values)
The value returned is the same as thetotal
field returned by settingquery.computeSize(true)
protected org.hibernate.Criteria
Deprecated.protected <O> javax.persistence.criteria.CriteriaQuery<O>
Create a JPA2 CriteriaQuery, which should have constraints generated usinggetCriteriaBuilder()
protected <O> javax.persistence.criteria.CriteriaQuery<O>
createCriteriaQuery
(Class<O> clazz) Create a JPA2 CriteriaQuery, which should have constraints generated using the methods ingetCriteriaBuilder()
protected org.hibernate.query.Query
createReadQuery
(String hql) Create aQuery
instance for the given HQL query string, signalling no intent to write (and as such working if the TX is read-only)
This method also makes an effort to prevent accidental update operations being called.protected org.hibernate.query.Query
createWriteQuery
(String hql) Create aQuery
instance for the given HQL query string, signalling intent to write (and failing immediately if TX is read-only)void
Delete an item from the databasevoid
deleteById
(ID id) Delete an item by its primary keyExecute a query, returning entitiesfind
(WebQuery query, JPASearchStrategy strategy) Execute a query, using the named strategy (defaults to AUTO if null)<RT> ConstrainedResultSet<RT>
find
(WebQuery query, JPASearchStrategy strategy, Function<?, RT> serialiser) Execute a query, using the named strategy (defaults to AUTO if null) and optionally serialising the resulting rows using the provided serialiserfindByUriQuery
(WebQuery query) Execute a Dynamic query using the specified constraints, returning the result as a ConstrainedResultSet of this entity typeExecute a query, using the ID strategyfindIdsByUriQuery
(WebQuery query) Given a query, return the IDs of the matching entities, returning the result as a ConstrainedResultSet of this entity's id typegetAll()
Retrieve every object accessible through this DAORetrieve an item by its primary keygetByIds
(Collection<ID> ids) Deprecated.getByUniqueProperty
(String propertyName, Object value) Convenience method which retrieves an entity by the equality of a single property (may return null if no entities match)protected javax.persistence.criteria.CriteriaBuilder
Get the type of Entity this DAO processesgetIdList
(org.hibernate.Criteria criteria) Execute a Criteria-based search, returning the results as a checked list of primary key typesgetIdList
(org.hibernate.query.Query query) Execute a Query search, returning the results as a checked list of primary key typesGet a list of IDs matching a WebQuerygetList
(org.hibernate.Criteria criteria) Execute a Criteria search, returning the results as a checked listgetList
(org.hibernate.query.Query query) Execute a Criteria-based search, returning the results as a checked listgetListById
(Collection<ID> ids) Query the database for all items with the given primary keys (convenience method for an ORred id query)getListById
(List<ID> ids) getReference
(ID id) protected org.hibernate.Session
protected org.hibernate.SessionFactory
Return the SessionFactory, useful for dynamic query building, etc.
NOTE: Using this this is potentially disruptive to the normal way of writing applications in our guice frameworkprotected org.hibernate.Session
Get a session and confirm that it is permitted to perform write operationsprotected String
Deprecated.Merge an unmanaged entity into a (new or existing) managed entitySave a new item in the database, returning its primary keyvoid
saveOrUpdate
(T obj) Create or Update an item in the databasevoid
setTypeLiteral
(com.google.inject.TypeLiteral<T> clazz) Called by guice to provide the Class associated with Tprotected T
uniqueResult
(WebQuery query) protected T
uniqueResult
(org.hibernate.Criteria criteria) Convenience method to return a single instance that matches the query, or null if the query returns no results.protected T
uniqueResult
(org.hibernate.query.Query query) Convenience method to return a single instance that matches the query, or null if the query returns no results.void
Update an existing item in the database
-
Field Details
-
clazz
-
isLargeTable
protected boolean isLargeTable -
searchExecutor
-
-
Constructor Details
-
HibernateDao
public HibernateDao()
-
-
Method Details
-
setTypeLiteral
Called by guice to provide the Class associated with T- Parameters:
clazz
- The TypeLiteral associated with T (the entity type)
-
createQueryBuilder
-
getEntityType
Description copied from interface:Dao
Get the type of Entity this DAO processes- Specified by:
getEntityType
in interfaceDao<T,
ID extends Serializable> - Returns:
- the entity type
-
getQEntity
-
idProperty
Deprecated. -
getByIds
Deprecated.- Specified by:
getByIds
in interfaceDao<T,
ID extends Serializable> - See Also:
-
getListById
Description copied from interface:Dao
Query the database for all items with the given primary keys (convenience method for an ORred id query)- Specified by:
getListById
in interfaceDao<T,
ID extends Serializable> - Parameters:
ids
- a collection of primary keys (may be empty)- Returns:
- any items whose id are contained within
ids
. May be empty if no matches were found. May be smaller thanids
. May be ordered differently fromids
-
getListById
-
getReference
- Specified by:
getReference
in interfaceDao<T,
ID extends Serializable>
-
getById
Description copied from interface:Dao
Retrieve an item by its primary key- Specified by:
getById
in interfaceDao<T,
ID extends Serializable> - Parameters:
id
- the id of an entity- Returns:
- the item (or null if it is not present)
- See Also:
-
Session.get(Class, java.io.Serializable)
-
getAll
Description copied from interface:Dao
Retrieve every object accessible through this DAO- Specified by:
getAll
in interfaceDao<T,
ID extends Serializable> - Returns:
- all entities of this type in the database
-
deleteById
Description copied from interface:Dao
Delete an item by its primary key- Specified by:
deleteById
in interfaceDao<T,
ID extends Serializable> - Parameters:
id
- the id of an entity
-
delete
Description copied from interface:Dao
Delete an item from the database- Specified by:
delete
in interfaceDao<T,
ID extends Serializable> - Parameters:
obj
- the entity- See Also:
-
Session.delete(Object)
-
saveOrUpdate
Description copied from interface:Dao
Create or Update an item in the database- Specified by:
saveOrUpdate
in interfaceDao<T,
ID extends Serializable> - Parameters:
obj
- the entity- See Also:
-
Session.saveOrUpdate(Object)
-
save
Description copied from interface:Dao
Save a new item in the database, returning its primary key- Specified by:
save
in interfaceDao<T,
ID extends Serializable> - Parameters:
obj
- the entity- Returns:
- the primary key of the newly saved object
- See Also:
-
Session.save(Object)
-
update
Description copied from interface:Dao
Update an existing item in the database- Specified by:
update
in interfaceDao<T,
ID extends Serializable> - Parameters:
obj
- the entity- See Also:
-
Session.update(Object)
-
merge
Description copied from interface:Dao
Merge an unmanaged entity into a (new or existing) managed entity- Specified by:
merge
in interfaceDao<T,
ID extends Serializable> - Parameters:
obj
- the entity- Returns:
- a Hibernate entity version of obj
- See Also:
-
Session.merge(Object)
-
getByUniqueProperty
Description copied from interface:Dao
Convenience method which retrieves an entity by the equality of a single property (may return null if no entities match)- Specified by:
getByUniqueProperty
in interfaceDao<T,
ID extends Serializable> - Parameters:
propertyName
- the property name (MUST be a simple property)value
- the property value- Returns:
- the entity, or null if no results were found
-
getSession
protected org.hibernate.Session getSession() -
getWriteSession
protected org.hibernate.Session getWriteSession()Get a session and confirm that it is permitted to perform write operations- Returns:
- Throws:
ReadOnlyTransactionException
- if we cannot perform writes in this session
-
createCriteria
Deprecated.use JPA2 Criteria withcreateCriteriaQuery()
, Hibernate has deprecated their old criteria query and it will produce runtime warningsCreate aCriteria
instance for the entity type of this DAO, matching this entity and any subclasses/implementors- Returns:
- The criteria instance for manipulation and execution
-
createCriteriaQuery
protected <O> javax.persistence.criteria.CriteriaQuery<O> createCriteriaQuery()Create a JPA2 CriteriaQuery, which should have constraints generated usinggetCriteriaBuilder()
- Type Parameters:
O
- the return type of the query- Returns:
-
createCriteriaQuery
Create a JPA2 CriteriaQuery, which should have constraints generated using the methods ingetCriteriaBuilder()
- Type Parameters:
O
- the return type of the query- Parameters:
clazz
- the return type of the query- Returns:
-
getCriteriaBuilder
protected javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder() -
createReadQuery
Create aQuery
instance for the given HQL query string, signalling no intent to write (and as such working if the TX is read-only)
This method also makes an effort to prevent accidental update operations being called. This protection cannot be relied upon for untrusted input!- Parameters:
hql
- The HQL query- Returns:
- The query instance for manipulation and execution
-
createWriteQuery
protected org.hibernate.query.Query createWriteQuery(String hql) throws ReadOnlyTransactionException Create aQuery
instance for the given HQL query string, signalling intent to write (and failing immediately if TX is read-only)- Parameters:
hql
- The HQL query- Returns:
- The query instance for manipulation and execution
- Throws:
ReadOnlyTransactionException
-
getList
Execute a Criteria search, returning the results as a checked list- Parameters:
criteria
- a criteria created by this DAO- Returns:
- The list of matched query results, implicitly cast to a List of this DAO type
- Throws:
org.hibernate.HibernateException
- Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
-
uniqueResult
Convenience method to return a single instance that matches the query, or null if the query returns no results.- Parameters:
criteria
- a criteria created by this DAO- Returns:
- the single result or null
- Throws:
IllegalStateException
- if there is more than one matching result
-
uniqueResult
-
getList
Execute a Criteria-based search, returning the results as a checked list- Parameters:
query
- a query that returns an object compatible with the entity type of this DAO- Returns:
- The list of matched query results, implicitly cast to a List of this DAO type
- Throws:
org.hibernate.HibernateException
- Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
-
getList
-
getIdList
Execute a Criteria-based search, returning the results as a checked list of primary key types- Parameters:
criteria
- the criteria (note, its projection must have been set to Projections.id())- Returns:
- Throws:
org.hibernate.HibernateException
- Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
-
getIdList
-
getIdList
Execute a Query search, returning the results as a checked list of primary key types- Parameters:
query
- the query (note, its projection must have been set to the primary key)- Returns:
- Throws:
org.hibernate.HibernateException
- Indicates a problem either translating the criteria to SQL, executing the SQL or processing the SQL results.
-
uniqueResult
Convenience method to return a single instance that matches the query, or null if the query returns no results.- Parameters:
query
- a query that returns an object compatible with the entity type of this DAO- Returns:
- the single result or null
- Throws:
org.hibernate.HibernateException
- if there is more than one matching result
-
getSessionFactory
protected org.hibernate.SessionFactory getSessionFactory()Return the SessionFactory, useful for dynamic query building, etc.
NOTE: Using this this is potentially disruptive to the normal way of writing applications in our guice framework- Returns:
-
findIdsByUriQuery
Description copied from interface:Dao
Given a query, return the IDs of the matching entities, returning the result as a ConstrainedResultSet of this entity's id type- Specified by:
findIdsByUriQuery
in interfaceDao<T,
ID extends Serializable> - Returns:
-
findByUriQuery
Description copied from interface:Dao
Execute a Dynamic query using the specified constraints, returning the result as a ConstrainedResultSet of this entity type- Specified by:
findByUriQuery
in interfaceDao<T,
ID extends Serializable> - Parameters:
query
- the constraints to apply- Returns:
- a resultset containing the requested number of results
-
find
Description copied from interface:Dao
Execute a query, returning entities- Specified by:
find
in interfaceDao<T,
ID extends Serializable> - Returns:
-
find
Description copied from interface:Dao
Execute a query, using the named strategy (defaults to AUTO if null)- Specified by:
find
in interfaceDao<T,
ID extends Serializable> strategy
- strategy, or null to default to AUTO- Returns:
-
findIds
Description copied from interface:Dao
Execute a query, using the ID strategy- Specified by:
findIds
in interfaceDao<T,
ID extends Serializable> - Returns:
-
count
Description copied from interface:Dao
Return the total number of results for a given query (ignoring offset/limit values)
The value returned is the same as thetotal
field returned by settingquery.computeSize(true)
- Specified by:
count
in interfaceDao<T,
ID extends Serializable> - Returns:
- the total number of results
-
find
public <RT> ConstrainedResultSet<RT> find(WebQuery query, JPASearchStrategy strategy, Function<?, RT> serialiser) Description copied from interface:Dao
Execute a query, using the named strategy (defaults to AUTO if null) and optionally serialising the resulting rows using the provided serialiser- Specified by:
find
in interfaceDao<T,
ID extends Serializable> strategy
- strategy, or null to default to AUTOserialiser
- the serialiser function to use to convert each row returned to the resulting resultset- Returns:
-
getIds
Get a list of IDs matching a WebQuery- Parameters:
constraints
-- Returns:
-
createCriteriaQuery()
, Hibernate has deprecated their old criteria query and it will produce runtime warnings