2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
;
12 import java
.lang
.reflect
.Field
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.EnumSet
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
22 import java
.util
.UUID
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.hibernate
.Criteria
;
26 import org
.hibernate
.FlushMode
;
27 import org
.hibernate
.HibernateException
;
28 import org
.hibernate
.LockOptions
;
29 import org
.hibernate
.NonUniqueObjectException
;
30 import org
.hibernate
.Query
;
31 import org
.hibernate
.Session
;
32 import org
.hibernate
.criterion
.Criterion
;
33 import org
.hibernate
.criterion
.DetachedCriteria
;
34 import org
.hibernate
.criterion
.Example
;
35 import org
.hibernate
.criterion
.Example
.PropertySelector
;
36 import org
.hibernate
.criterion
.LogicalExpression
;
37 import org
.hibernate
.criterion
.Order
;
38 import org
.hibernate
.criterion
.ProjectionList
;
39 import org
.hibernate
.criterion
.Projections
;
40 import org
.hibernate
.criterion
.Restrictions
;
41 import org
.hibernate
.criterion
.Subqueries
;
42 import org
.hibernate
.envers
.AuditReader
;
43 import org
.hibernate
.envers
.AuditReaderFactory
;
44 import org
.hibernate
.envers
.query
.AuditQuery
;
45 import org
.hibernate
.metadata
.ClassMetadata
;
46 import org
.hibernate
.sql
.JoinType
;
47 import org
.hibernate
.type
.Type
;
48 import org
.joda
.time
.DateTime
;
49 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
50 import org
.springframework
.dao
.DataAccessException
;
51 import org
.springframework
.dao
.InvalidDataAccessApiUsageException
;
52 import org
.springframework
.security
.core
.Authentication
;
53 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
54 import org
.springframework
.stereotype
.Repository
;
55 import org
.springframework
.util
.ReflectionUtils
;
57 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
58 import eu
.etaxonomy
.cdm
.model
.common
.IPublishable
;
59 import eu
.etaxonomy
.cdm
.model
.common
.User
;
60 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
61 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
62 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
63 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.Restriction
;
64 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.Restriction
.Operator
;
65 import eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
.IBeanInitializer
;
66 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
67 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.PostMergeEntityListener
;
68 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.replace
.ReferringObjectMetadata
;
69 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.replace
.ReferringObjectMetadataFactory
;
70 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
71 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
72 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
75 * @author a.mueller FIXME CdmEntityDaoBase is abstract, can it be annotated
79 public abstract class CdmEntityDaoBase
<T
extends CdmBase
> extends DaoBase
implements ICdmEntityDao
<T
> {
81 private static final Logger logger
= Logger
.getLogger(CdmEntityDaoBase
.class);
83 protected int flushAfterNo
= 1000; // large numbers may cause
84 // synchronisation errors when commiting
87 protected Class
<T
> type
;
90 // @Qualifier("defaultBeanInitializer")
91 protected IBeanInitializer defaultBeanInitializer
;
93 public void setDefaultBeanInitializer(IBeanInitializer defaultBeanInitializer
) {
94 this.defaultBeanInitializer
= defaultBeanInitializer
;
98 private ReferringObjectMetadataFactory referringObjectMetadataFactory
;
100 protected static final EnumSet
<Operator
> LEFTOUTER_OPS
= EnumSet
.of(Operator
.AND_NOT
, Operator
.OR
, Operator
.OR_NOT
);
102 public CdmEntityDaoBase(Class
<T
> type
) {
105 logger
.debug("Creating DAO of type [" + type
.getSimpleName() + "]");
109 public void lock(T t
, LockOptions lockOptions
) {
110 getSession().buildLockRequest(lockOptions
).lock(t
);
114 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
) {
115 getSession().refresh(t
, lockOptions
);
116 defaultBeanInitializer
.initialize(t
, propertyPaths
);
119 // TODO this method should be moved to a concrete class (not typed)
120 public UUID
saveCdmObj(CdmBase cdmObj
) throws DataAccessException
{
121 getSession().saveOrUpdate(cdmObj
);
122 return cdmObj
.getUuid();
125 // TODO: Replace saveCdmObj() by saveCdmObject_
126 private UUID
saveCdmObject_(T cdmObj
) {
127 getSession().saveOrUpdate(cdmObj
);
128 return cdmObj
.getUuid();
131 // TODO: Use everywhere CdmEntityDaoBase.saveAll() instead of
132 // ServiceBase.saveCdmObjectAll()?
133 // TODO: why does this use saveCdmObject_ which actually savesOrUpdateds
136 public Map
<UUID
, T
> saveAll(Collection
<T
> cdmObjCollection
) {
137 int types
= cdmObjCollection
.getClass().getTypeParameters().length
;
139 if (logger
.isDebugEnabled()) {
140 logger
.debug("ClassType: + " + cdmObjCollection
.getClass().getTypeParameters()[0]);
144 Map
<UUID
, T
> resultMap
= new HashMap
<>();
145 Iterator
<T
> iterator
= cdmObjCollection
.iterator();
147 while (iterator
.hasNext()) {
148 if (((i
% 2000) == 0) && (i
> 0)) {
149 logger
.debug("Saved " + i
+ " objects");
151 T cdmObj
= iterator
.next();
152 UUID uuid
= saveCdmObject_(cdmObj
);
153 if (logger
.isDebugEnabled()) {
154 logger
.debug("Save cdmObj: " + (cdmObj
== null ?
null : cdmObj
.toString()));
156 resultMap
.put(uuid
, cdmObj
);
158 if ((i
% flushAfterNo
) == 0) {
160 if (logger
.isDebugEnabled()) {
161 logger
.debug("flush");
164 } catch (Exception e
) {
165 logger
.error("An exception occurred when trying to flush data");
167 throw new RuntimeException(e
);
172 if (logger
.isInfoEnabled()) {
173 logger
.info("Saved " + i
+ " objects");
178 private UUID
saveOrUpdateCdmObject(T cdmObj
) {
179 getSession().saveOrUpdate(cdmObj
);
180 return cdmObj
.getUuid();
184 public Map
<UUID
, T
> saveOrUpdateAll(Collection
<T
> cdmObjCollection
) {
185 int types
= cdmObjCollection
.getClass().getTypeParameters().length
;
187 if (logger
.isDebugEnabled()) {
188 logger
.debug("ClassType: + " + cdmObjCollection
.getClass().getTypeParameters()[0]);
192 Map
<UUID
, T
> resultMap
= new HashMap
<>();
193 Iterator
<T
> iterator
= cdmObjCollection
.iterator();
195 while (iterator
.hasNext()) {
196 if (((i
% 2000) == 0) && (i
> 0)) {
197 logger
.debug("Saved " + i
+ " objects");
199 T cdmObj
= iterator
.next();
200 UUID uuid
= saveOrUpdateCdmObject(cdmObj
);
201 if (logger
.isDebugEnabled()) {
202 logger
.debug("Save cdmObj: " + (cdmObj
== null ?
null : cdmObj
.toString()));
204 resultMap
.put(uuid
, cdmObj
);
206 if ((i
% flushAfterNo
) == 0) {
208 if (logger
.isDebugEnabled()) {
209 logger
.debug("flush");
212 } catch (Exception e
) {
213 logger
.error("An exception occurred when trying to flush data");
215 throw new RuntimeException(e
);
220 if (logger
.isInfoEnabled()) {
221 logger
.info("Saved " + i
+ " objects");
227 public T
replace(T x
, T y
) {
232 Class
<?
> commonClass
= x
.getClass();
234 while (!commonClass
.isAssignableFrom(y
.getClass())) {
235 if (commonClass
.equals(type
)) {
236 throw new RuntimeException();
238 commonClass
= commonClass
.getSuperclass();
242 getSession().merge(x
);
244 Set
<ReferringObjectMetadata
> referringObjectMetas
= referringObjectMetadataFactory
.get(x
.getClass());
246 for (ReferringObjectMetadata referringObjectMetadata
: referringObjectMetas
) {
248 List
<CdmBase
> referringObjects
= referringObjectMetadata
.getReferringObjects(x
, getSession());
250 for (CdmBase referringObject
: referringObjects
) {
252 referringObjectMetadata
.replace(referringObject
, x
, y
);
253 getSession().update(referringObject
);
255 } catch (IllegalArgumentException e
) {
256 throw new RuntimeException(e
.getMessage(), e
);
257 } catch (IllegalAccessException e
) {
258 throw new RuntimeException(e
.getMessage(), e
);
266 public Session
getSession() throws DataAccessException
{
267 return super.getSession();
271 public void clear() throws DataAccessException
{
272 Session session
= getSession();
274 if (logger
.isDebugEnabled()) {
275 logger
.debug("dao clear end");
280 public MergeResult
<T
> merge(T transientObject
, boolean returnTransientEntity
) throws DataAccessException
{
281 Session session
= getSession();
282 PostMergeEntityListener
.addSession(session
);
283 MergeResult
<T
> result
= null;
285 @SuppressWarnings("unchecked")
286 T persistentObject
= (T
) session
.merge(transientObject
);
287 if (logger
.isDebugEnabled()) {
288 logger
.debug("dao merge end");
291 if (returnTransientEntity
) {
292 if (transientObject
!= null && persistentObject
!= null) {
293 transientObject
.setId(persistentObject
.getId());
295 result
= new MergeResult(transientObject
, PostMergeEntityListener
.getNewEntities(session
));
297 result
= new MergeResult(persistentObject
, null);
301 PostMergeEntityListener
.removeSession(session
);
306 public T
merge(T transientObject
) throws DataAccessException
{
307 Session session
= getSession();
308 @SuppressWarnings("unchecked")
309 T persistentObject
= (T
) session
.merge(transientObject
);
310 if (logger
.isDebugEnabled()) {
311 logger
.debug("dao merge end");
313 return persistentObject
;
317 public UUID
saveOrUpdate(T transientObject
) throws DataAccessException
{
318 if (transientObject
== null) {
319 logger
.warn("Object to save should not be null. NOP");
323 if (logger
.isDebugEnabled()) {
324 logger
.debug("dao saveOrUpdate start...");
326 if (logger
.isDebugEnabled()) {
327 logger
.debug("transientObject(" + transientObject
.getClass().getSimpleName() + ") ID:"
328 + transientObject
.getId() + ", UUID: " + transientObject
.getUuid());
330 Session session
= getSession();
331 if (transientObject
.getId() != 0 && VersionableEntity
.class.isAssignableFrom(transientObject
.getClass())) {
332 VersionableEntity versionableEntity
= (VersionableEntity
) transientObject
;
333 versionableEntity
.setUpdated(new DateTime());
334 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
335 if (authentication
!= null && authentication
.getPrincipal() != null
336 && authentication
.getPrincipal() instanceof User
) {
337 User user
= (User
) authentication
.getPrincipal();
338 versionableEntity
.setUpdatedBy(user
);
341 session
.saveOrUpdate(transientObject
);
342 if (logger
.isDebugEnabled()) {
343 logger
.debug("dao saveOrUpdate end");
345 return transientObject
.getUuid();
346 } catch (NonUniqueObjectException e
) {
347 logger
.error("Error in CdmEntityDaoBase.saveOrUpdate(obj). ID=" + e
.getIdentifier() + ". Class="
348 + e
.getEntityName());
349 logger
.error(e
.getMessage());
353 } catch (HibernateException e
) {
361 public T
save(T newInstance
) throws DataAccessException
{
362 if (newInstance
== null) {
363 logger
.warn("Object to save should not be null. NOP");
366 getSession().save(newInstance
);
371 public UUID
update(T transientObject
) throws DataAccessException
{
372 if (transientObject
== null) {
373 logger
.warn("Object to update should not be null. NOP");
376 getSession().update(transientObject
);
377 return transientObject
.getUuid();
381 public UUID
refresh(T persistentObject
) throws DataAccessException
{
382 getSession().refresh(persistentObject
);
383 return persistentObject
.getUuid();
387 public UUID
delete(T persistentObject
) throws DataAccessException
{
388 if (persistentObject
== null) {
389 logger
.warn(type
.getName() + " was 'null'");
393 // Merge the object in if it is detached
395 // I think this is preferable to catching lazy initialization errors
396 // as that solution only swallows and hides the exception, but doesn't
397 // actually solve it.
398 persistentObject
= (T
) getSession().merge(persistentObject
);
399 getSession().delete(persistentObject
);
400 return persistentObject
.getUuid();
404 public T
findById(int id
) throws DataAccessException
{
405 return getSession().get(type
, id
);
409 public T
findByUuid(UUID uuid
) throws DataAccessException
{
410 return this.findByUuid(uuid
, INCLUDE_UNPUBLISHED
);
413 protected T
findByUuid(UUID uuid
, boolean includeUnpublished
) throws DataAccessException
{
414 Session session
= getSession();
415 Criteria crit
= session
.createCriteria(type
);
416 crit
.add(Restrictions
.eq("uuid", uuid
));
417 crit
.addOrder(Order
.desc("created"));
418 if (IPublishable
.class.isAssignableFrom(type
) && !includeUnpublished
) {
419 crit
.add(Restrictions
.eq("publish", Boolean
.TRUE
));
422 @SuppressWarnings("unchecked")
423 List
<T
> results
= crit
.list();
424 Set
<T
> resultSet
= new HashSet
<>();
425 resultSet
.addAll(results
);
426 if (resultSet
.isEmpty()) {
429 if (resultSet
.size() > 1) {
430 logger
.error("findByUuid() delivers more than one result for UUID: " + uuid
);
432 return results
.get(0);
437 public T
findByUuidWithoutFlush(UUID uuid
) throws DataAccessException
{
438 Session session
= getSession();
439 FlushMode currentFlushMode
= session
.getFlushMode();
441 // set flush mode to manual so that the session does not flush
442 // when before performing the query
443 session
.setFlushMode(FlushMode
.MANUAL
);
444 Criteria crit
= session
.createCriteria(type
);
445 crit
.add(Restrictions
.eq("uuid", uuid
));
446 crit
.addOrder(Order
.desc("created"));
447 @SuppressWarnings("unchecked")
448 List
<T
> results
= crit
.list();
449 if (results
.isEmpty()) {
452 if (results
.size() > 1) {
453 logger
.error("findByUuid() delivers more than one result for UUID: " + uuid
);
455 return results
.get(0);
458 // set back the session flush mode
459 if (currentFlushMode
!= null) {
460 session
.setFlushMode(currentFlushMode
);
466 public List
<T
> loadList(Collection
<Integer
> ids
, List
<String
> propertyPaths
) throws DataAccessException
{
469 return new ArrayList
<T
>(0);
472 Criteria criteria
= prepareList(null, ids
, null, null, null, "id");
474 if (logger
.isDebugEnabled()) {
475 logger
.debug(criteria
.toString());
478 @SuppressWarnings("unchecked")
479 List
<T
> result
= criteria
.list();
480 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
485 public List
<T
> list(Collection
<UUID
> uuids
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
486 List
<String
> propertyPaths
) throws DataAccessException
{
488 if (uuids
== null || uuids
.isEmpty()) {
489 return new ArrayList
<>();
492 Criteria criteria
= prepareList(null, uuids
, pageSize
, pageNumber
, orderHints
, "uuid");
493 @SuppressWarnings("unchecked")
494 List
<T
> result
= criteria
.list();
495 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
500 public <S
extends T
> List
<S
> list(Class
<S
> clazz
, Collection
<UUID
> uuids
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
501 List
<String
> propertyPaths
) throws DataAccessException
{
503 if (uuids
== null || uuids
.isEmpty()) {
504 return new ArrayList
<>();
507 Criteria criteria
= prepareList(clazz
, uuids
, pageSize
, pageNumber
, orderHints
, "uuid");
508 @SuppressWarnings("unchecked")
509 List
<S
> result
= criteria
.list();
510 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
518 public <S
extends T
> List
<S
> list(Class
<S
> type
, List
<Restriction
<?
>> restrictions
, Integer limit
, Integer start
,
519 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
521 Criteria criteria
= createCriteria(type
, restrictions
, false);
523 addLimitAndStart(criteria
, limit
, start
);
524 addOrder(criteria
, orderHints
);
526 @SuppressWarnings("unchecked")
527 List
<S
> result
= criteria
.list();
528 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
533 * @param restrictions
536 private void addRestrictions(List
<Restriction
<?
>> restrictions
, DetachedCriteria criteria
) {
538 if(restrictions
== null || restrictions
.isEmpty()){
542 List
<CriterionWithOperator
> perProperty
= new ArrayList
<>(restrictions
.size());
543 Map
<String
, String
> aliases
= new HashMap
<>();
547 for(Restriction
<?
> restriction
: restrictions
){
548 Collection
<?
extends Object
> values
= restriction
.getValues();
549 JoinType jointype
= LEFTOUTER_OPS
.contains(restriction
.getOperator()) ? JoinType
.LEFT_OUTER_JOIN
: JoinType
.INNER_JOIN
;
550 if(values
!= null && !values
.isEmpty()){
552 String propertyPath
= restriction
.getPropertyName();
553 String
[] props
= propertyPath
.split("\\.");
555 if(props
.length
== 1){
556 // direct property of the base type of the criteria
557 propertyName
= propertyPath
;
559 // create aliases if the propertyName is a dot separated property path
560 String aĺiasKey
= jointype
.name() + "_";
561 String aliasedProperty
= null;
563 for(int p
= 0; p
< props
.length
-1; p
++){
564 aĺiasKey
= aĺiasKey
+ (aĺiasKey
.isEmpty() ?
"" : ".") + props
[p
];
565 aliasedProperty
= alias
+ (alias
.isEmpty() ?
"" : ".") + props
[p
];
566 if(!aliases
.containsKey(aliasedProperty
)){
567 alias
= alias
+ (alias
.isEmpty() ?
"" : "_" ) + props
[p
];
568 aliases
.put(aĺiasKey
, alias
);
569 criteria
.createAlias(aliasedProperty
, alias
, jointype
);
570 if(logger
.isDebugEnabled()){
571 logger
.debug("addRestrictions() alias created with aliasKey " + aĺiasKey
+ " => " + aliasedProperty
+ " as " + alias
);
575 propertyName
= alias
+ "." + props
[props
.length
-1];
578 Criterion
[] predicates
= new Criterion
[values
.size()];
580 for(Object v
: values
){
581 Criterion criterion
= createRestriction(propertyName
, v
, restriction
.getMatchMode());
582 if(restriction
.isNot()){
583 if(props
.length
> 1){
584 criterion
= Restrictions
.or(Restrictions
.not(criterion
), Restrictions
.isNull(propertyName
));
586 criterion
= Restrictions
.not(criterion
);
589 predicates
[i
++] = criterion
;
590 if(logger
.isDebugEnabled()){
591 logger
.debug("addRestrictions() predicate with " + propertyName
+ " " + (restriction
.getMatchMode() == null ?
"=" : restriction
.getMatchMode().name()) + " " + v
.toString());
594 if(restriction
.getOperator() == Operator
.AND_NOT
){
595 perProperty
.add(new CriterionWithOperator(restriction
.getOperator(), Restrictions
.and(predicates
)));
597 perProperty
.add(new CriterionWithOperator(restriction
.getOperator(), Restrictions
.or(predicates
)));
599 } // check has values
600 } // loop over restrictions
602 Restriction
.Operator firstOperator
= null;
603 if(!perProperty
.isEmpty()){
604 LogicalExpression logicalExpression
= null;
605 for(CriterionWithOperator cwo
: perProperty
){
606 if(logicalExpression
== null){
607 firstOperator
= cwo
.operator
;
608 logicalExpression
= Restrictions
.and(Restrictions
.sqlRestriction("1=1"), cwo
.criterion
);
610 switch(cwo
.operator
){
613 logicalExpression
= Restrictions
.and(logicalExpression
, cwo
.criterion
);
617 logicalExpression
= Restrictions
.or(logicalExpression
, cwo
.criterion
);
620 throw new RuntimeException("Unsupported Operator");
627 criteria
.add(logicalExpression
);
628 // if(firstOperator == Operator.OR){
632 // criteria.add(Restrictions.and(queryStringCriterion, logicalExpression));
635 if(logger
.isDebugEnabled()){
636 logger
.debug("addRestrictions() final criteria: " + criteria
.toString());
641 * @param propertyName
644 * is only applied if the <code>value</code> is a
645 * <code>String</code> object
649 private Criterion
createRestriction(String propertyName
, Object value
, MatchMode matchMode
) {
651 Criterion restriction
;
653 if (logger
.isDebugEnabled()) {
654 logger
.debug("createRestriction() " + propertyName
+ " is null ");
656 restriction
= Restrictions
.isNull(propertyName
);
657 } else if (matchMode
== null || !(value
instanceof String
)) {
658 if (logger
.isDebugEnabled()) {
659 logger
.debug("createRestriction() " + propertyName
+ " = " + value
.toString());
661 restriction
= Restrictions
.eq(propertyName
, value
);
663 String queryString
= (String
) value
;
664 if (logger
.isDebugEnabled()) {
665 logger
.debug("createRestriction() " + propertyName
+ " " + matchMode
.getMatchOperator() + " "
666 + matchMode
.queryStringFrom(queryString
));
670 restriction
= Restrictions
.ilike(propertyName
, queryString
, org
.hibernate
.criterion
.MatchMode
.START
);
673 restriction
= Restrictions
.ilike(propertyName
, queryString
, org
.hibernate
.criterion
.MatchMode
.END
);
676 restriction
= Restrictions
.ilike(propertyName
, matchMode
.queryStringFrom(queryString
), org
.hibernate
.criterion
.MatchMode
.ANYWHERE
);
679 restriction
= Restrictions
.ilike(propertyName
, queryString
, org
.hibernate
.criterion
.MatchMode
.EXACT
);
682 restriction
= Restrictions
.ilike(propertyName
, queryString
, org
.hibernate
.criterion
.MatchMode
.ANYWHERE
);
685 throw new RuntimeException("Unknown MatchMode: " + matchMode
.name());
695 public long count(Class
<?
extends T
> type
, List
<Restriction
<?
>> restrictions
) {
697 Criteria criteria
= createCriteria(type
, restrictions
, false);
699 criteria
.setProjection(Projections
.projectionList().add(Projections
.rowCount()));
701 return (Long
) criteria
.uniqueResult();
710 * @param propertyName
713 private Criteria
prepareList(Class
<?
extends T
> clazz
, Collection
<?
> uuids
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
714 String propertyName
) {
718 Criteria criteria
= getSession().createCriteria(clazz
);
719 criteria
.add(Restrictions
.in(propertyName
, uuids
));
721 if (pageSize
!= null) {
722 criteria
.setMaxResults(pageSize
);
723 if (pageNumber
!= null) {
724 criteria
.setFirstResult(pageNumber
* pageSize
);
726 criteria
.setFirstResult(0);
730 if (orderHints
== null) {
731 orderHints
= OrderHint
.defaultOrderHintsFor(type
);
733 addOrder(criteria
, orderHints
);
741 private Criteria
criterionForType(Class
<?
extends T
> clazz
) {
742 return getSession().createCriteria(entityType(clazz
));
745 protected Class
<?
extends T
> entityType(Class
<?
extends T
> clazz
){
754 public T
load(UUID uuid
) {
755 T bean
= findByUuid(uuid
);
759 defaultBeanInitializer
.load(bean
);
765 public T
load(int id
, List
<String
> propertyPaths
) {
766 T bean
= findById(id
);
770 defaultBeanInitializer
.initialize(bean
, propertyPaths
);
776 public T
load(UUID uuid
, List
<String
> propertyPaths
) {
777 return this.load(uuid
, INCLUDE_UNPUBLISHED
, propertyPaths
);
780 protected T
load(UUID uuid
, boolean includeUnpublished
, List
<String
> propertyPaths
) {
781 T bean
= findByUuid(uuid
, includeUnpublished
);
785 defaultBeanInitializer
.initialize(bean
, propertyPaths
);
791 public Boolean
exists(UUID uuid
) {
792 if (findByUuid(uuid
) == null) {
799 public long count() {
804 public long count(Class
<?
extends T
> clazz
) {
805 Session session
= getSession();
806 Criteria criteria
= null;
808 criteria
= session
.createCriteria(type
);
810 criteria
= session
.createCriteria(clazz
);
812 criteria
.setProjection(Projections
.projectionList().add(Projections
.rowCount()));
814 // since hibernate 4 (or so) uniqueResult returns Long, not Integer,
816 // to be casted. Think about returning long rather then int!
817 return (long) criteria
.uniqueResult();
821 public List
<T
> list(Integer limit
, Integer start
) {
822 return list(limit
, start
, null);
826 public List
<Object
[]> group(Class
<?
extends T
> clazz
, Integer limit
, Integer start
, List
<Grouping
> groups
,
827 List
<String
> propertyPaths
) {
829 Criteria criteria
= null;
830 criteria
= criterionForType(clazz
);
832 addGroups(criteria
, groups
);
835 criteria
.setFirstResult(start
);
836 criteria
.setMaxResults(limit
);
839 @SuppressWarnings("unchecked")
840 List
<Object
[]> result
= criteria
.list();
842 if (propertyPaths
!= null && !propertyPaths
.isEmpty()) {
843 for (Object
[] objects
: result
) {
844 defaultBeanInitializer
.initialize(objects
[0], propertyPaths
);
851 protected void countGroups(DetachedCriteria criteria
, List
<Grouping
> groups
) {
852 if (groups
!= null) {
854 Map
<String
, String
> aliases
= new HashMap
<String
, String
>();
856 for (Grouping grouping
: groups
) {
857 if (grouping
.getAssociatedObj() != null) {
859 if ((alias
= aliases
.get(grouping
.getAssociatedObj())) == null) {
860 alias
= grouping
.getAssociatedObjectAlias();
861 aliases
.put(grouping
.getAssociatedObj(), alias
);
862 criteria
.createAlias(grouping
.getAssociatedObj(), alias
);
867 ProjectionList projectionList
= Projections
.projectionList();
869 for (Grouping grouping
: groups
) {
870 grouping
.addProjection(projectionList
);
872 criteria
.setProjection(projectionList
);
876 protected void addGroups(Criteria criteria
, List
<Grouping
> groups
) {
877 if (groups
!= null) {
879 Map
<String
, String
> aliases
= new HashMap
<String
, String
>();
881 for (Grouping grouping
: groups
) {
882 if (grouping
.getAssociatedObj() != null) {
884 if ((alias
= aliases
.get(grouping
.getAssociatedObj())) == null) {
885 alias
= grouping
.getAssociatedObjectAlias();
886 aliases
.put(grouping
.getAssociatedObj(), alias
);
887 criteria
.createAlias(grouping
.getAssociatedObj(), alias
);
892 ProjectionList projectionList
= Projections
.projectionList();
894 for (Grouping grouping
: groups
) {
895 grouping
.addProjection(projectionList
);
897 criteria
.setProjection(projectionList
);
899 for (Grouping grouping
: groups
) {
900 grouping
.addOrder(criteria
);
907 public List
<T
> list(Integer limit
, Integer start
, List
<OrderHint
> orderHints
) {
908 return list(limit
, start
, orderHints
, null);
912 public List
<T
> list(Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
913 Criteria criteria
= getSession().createCriteria(type
);
915 criteria
.setFirstResult(start
);
916 criteria
.setMaxResults(limit
);
919 addOrder(criteria
, orderHints
);
920 @SuppressWarnings("unchecked")
921 List
<T
> results
= criteria
.list();
923 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
928 public <S
extends T
> List
<S
> list(Class
<S
> clazz
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
,
929 List
<String
> propertyPaths
) {
930 Criteria criteria
= null;
932 criteria
= getSession().createCriteria(type
);
934 criteria
= getSession().createCriteria(clazz
);
937 addLimitAndStart(criteria
, limit
, start
);
939 addOrder(criteria
, orderHints
);
941 @SuppressWarnings("unchecked")
942 List
<S
> results
= criteria
.list();
944 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
948 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
) {
949 return list(type
, limit
, start
, orderHints
, null);
953 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
) {
954 return list(type
, limit
, start
, null, null);
958 public Class
<T
> getType() {
962 protected void setPagingParameter(Query query
, Integer pageSize
, Integer pageIndex
) {
963 if (pageSize
!= null) {
964 query
.setMaxResults(pageSize
);
965 if (pageIndex
!= null) {
966 query
.setFirstResult(pageIndex
* pageSize
);
968 query
.setFirstResult(0);
973 protected void setPagingParameter(AuditQuery query
, Integer pageSize
, Integer pageIndex
) {
974 if (pageSize
!= null) {
975 query
.setMaxResults(pageSize
);
976 if (pageIndex
!= null) {
977 query
.setFirstResult(pageIndex
* pageSize
);
979 query
.setFirstResult(0);
985 public long count(T example
, Set
<String
> includeProperties
) {
986 Criteria criteria
= getSession().createCriteria(example
.getClass());
987 addExample(criteria
, example
, includeProperties
);
989 criteria
.setProjection(Projections
.rowCount());
990 return (Long
) criteria
.uniqueResult();
993 protected void addExample(Criteria criteria
, T example
, Set
<String
> includeProperties
) {
994 if (includeProperties
!= null && !includeProperties
.isEmpty()) {
995 criteria
.add(Example
.create(example
).setPropertySelector(new PropertySelectorImpl(includeProperties
)));
996 ClassMetadata classMetadata
= getSession().getSessionFactory().getClassMetadata(example
.getClass());
997 for (String property
: includeProperties
) {
998 Type type
= classMetadata
.getPropertyType(property
);
999 if (type
.isEntityType()) {
1001 Field field
= ReflectionUtils
.findField(example
.getClass(), property
);
1002 field
.setAccessible(true);
1003 Object value
= field
.get(example
);
1004 if (value
!= null) {
1005 criteria
.add(Restrictions
.eq(property
, value
));
1007 criteria
.add(Restrictions
.isNull(property
));
1009 } catch (SecurityException se
) {
1010 throw new InvalidDataAccessApiUsageException("Tried to add criteria for property " + property
,
1012 } catch (HibernateException he
) {
1013 throw new InvalidDataAccessApiUsageException("Tried to add criteria for property " + property
,
1015 } catch (IllegalArgumentException iae
) {
1016 throw new InvalidDataAccessApiUsageException("Tried to add criteria for property " + property
,
1018 } catch (IllegalAccessException ie
) {
1019 throw new InvalidDataAccessApiUsageException("Tried to add criteria for property " + property
,
1026 criteria
.add(Example
.create(example
));
1032 * NOTE: We can't reuse
1033 * {@link #list(Class, String, Object, MatchMode, Integer, Integer, List, List)
1034 * here due to different default behavior of the <code>matchmode</code>
1039 * @param queryString
1045 * @param propertyPaths
1049 public <S
extends T
> List
<S
> findByParam(Class
<S
> clazz
, String param
, String queryString
, MatchMode matchmode
,
1050 List
<Criterion
> criterion
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
1051 List
<String
> propertyPaths
) {
1053 Criteria criteria
= criterionForType(clazz
);
1055 if (queryString
!= null) {
1056 if (matchmode
== null) {
1057 criteria
.add(Restrictions
.ilike(param
, queryString
));
1058 } else if (matchmode
== MatchMode
.BEGINNING
) {
1059 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.START
));
1060 } else if (matchmode
== MatchMode
.END
) {
1061 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.END
));
1062 } else if (matchmode
== MatchMode
.EXACT
) {
1063 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.EXACT
));
1065 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.ANYWHERE
));
1069 addCriteria(criteria
, criterion
);
1071 if (pageSize
!= null) {
1072 criteria
.setMaxResults(pageSize
);
1073 if (pageNumber
!= null) {
1074 criteria
.setFirstResult(pageNumber
* pageSize
);
1076 criteria
.setFirstResult(0);
1080 addOrder(criteria
, orderHints
);
1082 @SuppressWarnings("unchecked")
1083 List
<S
> result
= criteria
.list();
1084 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
1092 * @param queryString
1098 public long countByParam(Class
<?
extends T
> clazz
, String param
, String queryString
, MatchMode matchmode
,
1099 List
<Criterion
> criterion
) {
1101 Criteria criteria
= null;
1103 criteria
= criterionForType(clazz
);
1105 if (queryString
!= null) {
1106 if (matchmode
== null) {
1107 criteria
.add(Restrictions
.ilike(param
, queryString
));
1108 } else if (matchmode
== MatchMode
.BEGINNING
) {
1109 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.START
));
1110 } else if (matchmode
== MatchMode
.END
) {
1111 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.END
));
1112 } else if (matchmode
== MatchMode
.EXACT
) {
1113 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.EXACT
));
1115 criteria
.add(Restrictions
.ilike(param
, queryString
, org
.hibernate
.criterion
.MatchMode
.ANYWHERE
));
1119 addCriteria(criteria
, criterion
);
1121 criteria
.setProjection(Projections
.rowCount());
1123 return (Long
) criteria
.uniqueResult();
1127 * Creates a criteria query for the CDM <code>type</code> either for counting or listing matching entities.
1129 * The set of matching entities can be restricted by passing a list of {@link Restriction} objects.
1130 * Restrictions can logically combined:
1133 new Restriction<String>("titleCache", MatchMode.ANYWHERE, "foo"),
1134 new Restriction<String>("institute.name", Operator.OR, MatchMode.BEGINNING, "Bar")
1137 * The first Restriction in the example above by default has the <code>Operator.AND</code> which will be
1138 * ignored since this is the first restriction. The <code>Operator</code> of further restrictions in the
1139 * list are used to combine with the previous restriction.
1142 * @param restrictions
1146 protected Criteria
createCriteria(Class
<?
extends T
> type
, List
<Restriction
<?
>> restrictions
, boolean doCount
) {
1148 DetachedCriteria idsOnlyCriteria
= DetachedCriteria
.forClass(entityType(type
));
1149 idsOnlyCriteria
.setProjection(Projections
.distinct(Projections
.id()));
1151 addRestrictions(restrictions
, idsOnlyCriteria
);
1153 Criteria criteria
= criterionForType(type
);
1154 criteria
.add(Subqueries
.propertyIn("id", idsOnlyCriteria
));
1157 criteria
.setProjection(Projections
.rowCount());
1159 idsOnlyCriteria
.setProjection(Projections
.distinct(Projections
.property("id")));
1167 public <S
extends T
> List
<S
> findByParamWithRestrictions(Class
<S
> clazz
, String param
, String queryString
,
1168 MatchMode matchmode
, List
<Restriction
<?
>> restrictions
, Integer pageSize
, Integer pageNumber
,
1169 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1171 List
<Restriction
<?
>> allRestrictions
= new ArrayList
<>();
1172 allRestrictions
.add(new Restriction
<String
>(param
, matchmode
, queryString
));
1173 if(restrictions
!= null){
1174 allRestrictions
.addAll(restrictions
);
1176 Criteria criteria
= createCriteria(clazz
, allRestrictions
, false);
1178 if (pageSize
!= null) {
1179 criteria
.setMaxResults(pageSize
);
1180 if (pageNumber
!= null) {
1181 criteria
.setFirstResult(pageNumber
* pageSize
);
1183 criteria
.setFirstResult(0);
1187 addOrder(criteria
, orderHints
);
1189 @SuppressWarnings("unchecked")
1190 List
<S
> result
= criteria
.list();
1191 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
1197 public long countByParamWithRestrictions(Class
<?
extends T
> clazz
, String param
, String queryString
,
1198 MatchMode matchmode
, List
<Restriction
<?
>> restrictions
) {
1200 List
<Restriction
<?
>> allRestrictions
= new ArrayList
<>();
1201 allRestrictions
.add(new Restriction
<String
>(param
, matchmode
, queryString
));
1202 if(restrictions
!= null){
1203 allRestrictions
.addAll(restrictions
);
1205 Criteria criteria
= createCriteria(clazz
, allRestrictions
, true);
1207 return (Long
) criteria
.uniqueResult();
1211 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
,
1212 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1213 Criteria criteria
= getSession().createCriteria(example
.getClass());
1214 addExample(criteria
, example
, includeProperties
);
1216 addLimitAndStart(criteria
, limit
, start
);
1218 addOrder(criteria
, orderHints
);
1220 @SuppressWarnings("unchecked")
1221 List
<T
> results
= criteria
.list();
1222 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
1226 private class PropertySelectorImpl
implements PropertySelector
{
1228 private final Set
<String
> includeProperties
;
1232 private static final long serialVersionUID
= -3175311800911570546L;
1234 public PropertySelectorImpl(Set
<String
> includeProperties
) {
1235 this.includeProperties
= includeProperties
;
1239 public boolean include(Object propertyValue
, String propertyName
, Type type
) {
1240 if (includeProperties
.contains(propertyName
)) {
1249 private class CriterionWithOperator
{
1251 Restriction
.Operator operator
;
1252 Criterion criterion
;
1255 public CriterionWithOperator(Operator operator
, Criterion criterion
) {
1257 this.operator
= operator
;
1258 this.criterion
= criterion
;
1265 * Returns a Criteria for the given {@link Class class} or, if
1266 * <code>null</code>, for the base {@link Class class} of this DAO.
1269 * @return the Criteria
1271 protected Criteria
getCriteria(Class
<?
extends CdmBase
> clazz
) {
1272 Criteria criteria
= null;
1273 if (clazz
== null) {
1274 criteria
= getSession().createCriteria(type
);
1276 criteria
= getSession().createCriteria(clazz
);
1286 protected AuditQuery
makeAuditQuery(Class
<?
extends CdmBase
> clazz
, AuditEvent auditEvent
) {
1287 AuditQuery query
= null;
1289 if (clazz
== null) {
1290 query
= getAuditReader().createQuery().forEntitiesAtRevision(type
, auditEvent
.getRevisionNumber());
1292 query
= getAuditReader().createQuery().forEntitiesAtRevision(clazz
, auditEvent
.getRevisionNumber());
1297 protected AuditReader
getAuditReader() {
1298 return AuditReaderFactory
.get(getSession());