Package de.terrestris.shoguncore.dao
Class GenericHibernateDao<E extends PersistentObject,ID extends Serializable>
- java.lang.Object
-
- de.terrestris.shoguncore.dao.GenericHibernateDao<E,ID>
-
- Direct Known Subclasses:
AbstractTokenDao,ApplicationDao,ButtonDao,ExtentDao,FileDao,InterceptorRuleDao,LayerAppearanceDao,LayerDao,LayerDataSourceDao,LayoutDao,MapConfigDao,MapControlDao,ModuleDao,PermissionCollectionDao,PersonDao,PluginDao,RoleDao,TerritoryDao,TileGridDao,TreeFolderDao,TreeNodeDao,UserGroupDao,WpsParameterDao
@Repository("genericDao") public class GenericHibernateDao<E extends PersistentObject,ID extends Serializable> extends ObjectThe superclass for all data access objects. Provides basic CRUD functionality and a logger instance for all subclasses.- Author:
- Nils Bühner
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.apache.logging.log4j.LoggerloggerThe LOGGER instance (that will be available in all subclasses)
-
Constructor Summary
Constructors Modifier Constructor Description GenericHibernateDao()Default constructorprotectedGenericHibernateDao(Class<E> clazz)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.hibernate.CriteriacreateDistinctRootEntityCriteria(org.hibernate.criterion.Criterion... criterion)Helper method: Creates a criteria for theentityClassof this dao.voiddelete(E e)Deletes the passed entity.voidevict(E e)Detach an entity from the hibernate sessionList<E>findAll()Returns all Entities by calling findByCriteria(), i.e.Map<PersistentObject,PermissionCollection>findAllUserGroupPermissionsOfUserGroup(UserGroup userGroup)This method returns aMapthat mapsPersistentObjects to PermissionCollections for the passedUserGroup.Map<PersistentObject,PermissionCollection>findAllUserPermissionsOfUser(User user)List<E>findAllWhereFieldEquals(String fieldName, Object fieldEntity, org.hibernate.criterion.Criterion... criterion)Returns a list of entity objects that have field namedfieldName, which has an objectfieldEntityas value.List<E>findAllWithCollectionContaining(String fieldName, PersistentObject subElement, org.hibernate.criterion.Criterion... criterion)Returns a list of entity objects that have a collection namedfieldName, which contains the passedsubElement.List<E>findByCriteria(org.hibernate.criterion.Criterion... criterion)Gets the results, that match a variable number of passed criterions.List<E>findByCriteriaRestricted(List<String> restrictFieldNames, org.hibernate.criterion.Criterion... criterion)Gets the results, that match a variable number of passed criterions, but return a stripped version of the entities, where only the fieldNames inrestrictFieldNameshave their actual values set.PagingResult<E>findByCriteriaWithSortingAndPaging(Integer firstResult, Integer maxResults, List<org.hibernate.criterion.Order> sorters, org.hibernate.criterion.Criterion... criterion)Gets the results, that match a variable number of passed criterions, considering the paging- and sort-info at the same time.EfindById(ID id)Return the real object from the database.EfindByUniqueCriteria(org.hibernate.criterion.Criterion... criterion)Gets the unique result, that matches a variable number of passed criterions.Class<E>getEntityClass()NumbergetTotalCount(org.hibernate.criterion.Criterion... criterion)Returns the total count of db entries for the current type.EloadById(ID id)Return a proxy of the object (without hitting the database).voidsaveOrUpdate(E e)Saves or updates the passed entity.Eunproxy(E e)Unproxy the entity (and eagerly fetch properties).
-
-
-
Method Detail
-
findById
public E findById(ID id)
Return the real object from the database. Returns null if the object does not exist.- Parameters:
id-- Returns:
- The object from the database or null if it does not exist
-
findAllWhereFieldEquals
public List<E> findAllWhereFieldEquals(String fieldName, Object fieldEntity, org.hibernate.criterion.Criterion... criterion)
Returns a list of entity objects that have field namedfieldName, which has an objectfieldEntityas value.- Parameters:
fieldName- The name of the fieldfieldEntity- The element that should be set as valuecriterion- Additional criterions to apply (optional)- Returns:
- The list of objects
-
findAllWithCollectionContaining
public List<E> findAllWithCollectionContaining(String fieldName, PersistentObject subElement, org.hibernate.criterion.Criterion... criterion)
Returns a list of entity objects that have a collection namedfieldName, which contains the passedsubElement.The can e.g. be used to return all applications that contain a certain layer.
- Parameters:
fieldName- The name of the collection fieldsubElement- The element that should be contained in the collectioncriterion- Additional criterions to apply (optional)- Returns:
- The list of objects
-
loadById
public E loadById(ID id)
Return a proxy of the object (without hitting the database). This should only be used if it is assumed that the object really exists and where non-existence would be an actual error.- Parameters:
id-- Returns:
-
findAll
public List<E> findAll() throws org.hibernate.HibernateException
Returns all Entities by calling findByCriteria(), i.e. without arguments.- Returns:
- All entities
- Throws:
org.hibernate.HibernateException- See Also:
findByCriteria(Criterion...)
-
saveOrUpdate
public void saveOrUpdate(E e)
Saves or updates the passed entity.- Parameters:
e- The entity to save or update in the database.
-
delete
public void delete(E e)
Deletes the passed entity.- Parameters:
e- The entity to remove from the database.
-
evict
public void evict(E e)
Detach an entity from the hibernate session- Parameters:
e-
-
findByCriteria
public List<E> findByCriteria(org.hibernate.criterion.Criterion... criterion) throws org.hibernate.HibernateException
Gets the results, that match a variable number of passed criterions. Call this method without arguments to find all entities.- Parameters:
criterion- A variable number of hibernate criterions- Returns:
- Entities matching the passed hibernate criterions
- Throws:
org.hibernate.HibernateException
-
findByCriteriaRestricted
public List<E> findByCriteriaRestricted(List<String> restrictFieldNames, org.hibernate.criterion.Criterion... criterion) throws org.hibernate.HibernateException
Gets the results, that match a variable number of passed criterions, but return a stripped version of the entities, where only the fieldNames inrestrictFieldNameshave their actual values set.You can call this with
restrictFieldNames=nullto get the full entities back.You can call this method without criterion arguments to find all entities (stripped down to the
restrictFieldNames).If this is called as
findByCriteriaRestricted(null)the return value equals the return value offindByCriteria().- Parameters:
restrictFieldNames-criterion-- Returns:
- Throws:
org.hibernate.HibernateException
-
findByUniqueCriteria
public E findByUniqueCriteria(org.hibernate.criterion.Criterion... criterion) throws org.hibernate.HibernateException
Gets the unique result, that matches a variable number of passed criterions.- Parameters:
criterion- A variable number of hibernate criterions- Returns:
- Entity matching the passed hibernate criterions
- Throws:
org.hibernate.HibernateException- if there is more than one matching result
-
findByCriteriaWithSortingAndPaging
public PagingResult<E> findByCriteriaWithSortingAndPaging(Integer firstResult, Integer maxResults, List<org.hibernate.criterion.Order> sorters, org.hibernate.criterion.Criterion... criterion) throws org.hibernate.HibernateException
Gets the results, that match a variable number of passed criterions, considering the paging- and sort-info at the same time.- Parameters:
firstResult- Starting index for the paging request.maxResults- Max number of result size.criterion- A variable number of hibernate criterions- Returns:
- Throws:
org.hibernate.HibernateException
-
findAllUserPermissionsOfUser
public Map<PersistentObject,PermissionCollection> findAllUserPermissionsOfUser(User user)
This method returns aMapthat mapsPersistentObjects to PermissionCollections for the passedUser. I.e. the keySet of the map is the collection of allPersistentObjects where the user has at least one permission and the corresponding value contains thePermissionCollectionfor the passed user on the entity.- Parameters:
user-- Returns:
-
findAllUserGroupPermissionsOfUserGroup
public Map<PersistentObject,PermissionCollection> findAllUserGroupPermissionsOfUserGroup(UserGroup userGroup)
This method returns aMapthat mapsPersistentObjects to PermissionCollections for the passedUserGroup. I.e. the keySet of the map is the collection of allPersistentObjects where the user group has at least one permission and the corresponding value contains thePermissionCollectionfor the passed user group on the entity.- Parameters:
userGroup-- Returns:
-
createDistinctRootEntityCriteria
protected org.hibernate.Criteria createDistinctRootEntityCriteria(org.hibernate.criterion.Criterion... criterion)
Helper method: Creates a criteria for theentityClassof this dao. The query results will be handled with aDistinctRootEntityResultTransformer. The criteria will contain all passed criterions.- Returns:
-
getTotalCount
public Number getTotalCount(org.hibernate.criterion.Criterion... criterion) throws org.hibernate.HibernateException
Returns the total count of db entries for the current type.- Parameters:
criterion-- Returns:
- Throws:
org.hibernate.HibernateException
-
-