diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java index 76983d67b3..07d19980a9 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java @@ -307,9 +307,7 @@ public class RevisionInfoConfiguration { return new RevisionInfoConfigurationResult( revisionInfoGenerator, revisionInfoXmlMapping, new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdData.getName(), - revisionInfoTimestampData.getName(), isTimestampAsDate(), - globalCfg.isTrackEntitiesChangedInRevisionEnabled() ? modifiedEntityNamesData.getName() - : null), + revisionInfoTimestampData.getName(), isTimestampAsDate()), generateRevisionInfoRelationMapping(), new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName, revisionInfoClass, revisionInfoTimestampData); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java b/hibernate-envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java index 37a3ac1fcc..9f4bf1c6aa 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java @@ -27,9 +27,9 @@ import static org.hibernate.envers.tools.ArgumentsTools.checkPositive; import java.util.*; import javax.persistence.NoResultException; +import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.NonUniqueResultException; -import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.engine.SessionImplementor; import org.hibernate.envers.ModifiedEntityNames; @@ -153,7 +153,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { checkPositive(revision, "Entity revision"); checkSession(); - Query query = verCfg.getRevisionInfoQueryCreator().getRevisionDateQuery(session, revision); + Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionDateQuery(session, revision); try { Object timestampObject = query.uniqueResult(); @@ -172,7 +172,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { checkNotNull(date, "Date of revision"); checkSession(); - Query query = verCfg.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery(session, date); + Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery(session, date); try { Number res = (Number) query.uniqueResult(); @@ -195,7 +195,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { Set revisions = new HashSet(1); revisions.add(revision); - Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); + Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); try { T revisionData = (T) query.uniqueResult(); @@ -221,7 +221,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { } checkSession(); - Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); + Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); try { List revisionList = query.list(); @@ -288,7 +288,7 @@ public class AuditReaderImpl implements AuditReaderImplementor { } Set revisions = new HashSet(1); revisions.add(revision); - Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); + Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions); Object revisionInfo = query.uniqueResult(); if (revisionInfo != null) { // If revision exists diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoQueryCreator.java b/hibernate-envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoQueryCreator.java index 153092e0a4..bd00d645f9 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoQueryCreator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoQueryCreator.java @@ -23,8 +23,10 @@ */ package org.hibernate.envers.revisioninfo; -import org.hibernate.Query; +import org.hibernate.Criteria; import org.hibernate.Session; +import org.hibernate.criterion.Projections; +import org.hibernate.criterion.Restrictions; import java.util.Date; import java.util.Set; @@ -34,56 +36,30 @@ import java.util.Set; * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class RevisionInfoQueryCreator { - private final String revisionDateQuery; - private final String revisionNumberForDateQuery; - private final String revisionsQuery; + private final String revisionInfoEntityName; + private final String revisionInfoIdName; + private final String revisionInfoTimestampName; private final boolean timestampAsDate; public RevisionInfoQueryCreator(String revisionInfoEntityName, String revisionInfoIdName, - String revisionInfoTimestampName, boolean timestampAsDate, - String modifiedEntityNamesName) { + String revisionInfoTimestampName, boolean timestampAsDate) { + this.revisionInfoEntityName = revisionInfoEntityName; + this.revisionInfoIdName = revisionInfoIdName; + this.revisionInfoTimestampName = revisionInfoTimestampName; this.timestampAsDate = timestampAsDate; - - revisionDateQuery = new StringBuilder() - .append("select rev.").append(revisionInfoTimestampName) - .append(" from ").append(revisionInfoEntityName) - .append(" rev where ").append(revisionInfoIdName).append(" = :_revision_number") - .toString(); - - revisionNumberForDateQuery = new StringBuilder() - .append("select max(rev.").append(revisionInfoIdName) - .append(") from ").append(revisionInfoEntityName) - .append(" rev where ").append(revisionInfoTimestampName).append(" <= :_revision_date") - .toString(); - - if (modifiedEntityNamesName == null) { - // Tracking modified entity classes is not enabled. - revisionsQuery = new StringBuilder() - .append("select rev from ").append(revisionInfoEntityName) - .append(" rev where ").append(revisionInfoIdName) - .append(" in (:_revision_numbers)") - .toString(); - } else { - // Modified entity names enabled. Eagerly loading entity names collection. - // HQL queries do not respect any fetch="join" defined in the mapping document. - revisionsQuery = new StringBuilder() - .append("select rev from ").append(revisionInfoEntityName) - .append(" rev join fetch rev.").append(modifiedEntityNamesName) - .append(" ent where rev.").append(revisionInfoIdName) - .append(" in (:_revision_numbers)") - .toString(); - } } - public Query getRevisionDateQuery(Session session, Number revision) { - return session.createQuery(revisionDateQuery).setParameter("_revision_number", revision); + public Criteria getRevisionDateQuery(Session session, Number revision) { + return session.createCriteria(revisionInfoEntityName).setProjection(Projections.property(revisionInfoTimestampName)) + .add(Restrictions.eq(revisionInfoIdName, revision)); } - public Query getRevisionNumberForDateQuery(Session session, Date date) { - return session.createQuery(revisionNumberForDateQuery).setParameter("_revision_date", timestampAsDate ? date : date.getTime()); + public Criteria getRevisionNumberForDateQuery(Session session, Date date) { + return session.createCriteria(revisionInfoEntityName).setProjection(Projections.max(revisionInfoIdName)) + .add(Restrictions.le(revisionInfoTimestampName, timestampAsDate ? date : date.getTime())); } - public Query getRevisionsQuery(Session session, Set revisions) { - return session.createQuery(revisionsQuery).setParameterList("_revision_numbers", revisions); + public Criteria getRevisionsQuery(Session session, Set revisions) { + return session.createCriteria(revisionInfoEntityName).add(Restrictions.in(revisionInfoIdName, revisions)); } }