HHH-5580 - Using Criteria API in RevisionInfoQueryCreator
This commit is contained in:
parent
b36b095c3c
commit
dd55a99b00
|
@ -307,9 +307,7 @@ public class RevisionInfoConfiguration {
|
||||||
return new RevisionInfoConfigurationResult(
|
return new RevisionInfoConfigurationResult(
|
||||||
revisionInfoGenerator, revisionInfoXmlMapping,
|
revisionInfoGenerator, revisionInfoXmlMapping,
|
||||||
new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdData.getName(),
|
new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdData.getName(),
|
||||||
revisionInfoTimestampData.getName(), isTimestampAsDate(),
|
revisionInfoTimestampData.getName(), isTimestampAsDate()),
|
||||||
globalCfg.isTrackEntitiesChangedInRevisionEnabled() ? modifiedEntityNamesData.getName()
|
|
||||||
: null),
|
|
||||||
generateRevisionInfoRelationMapping(),
|
generateRevisionInfoRelationMapping(),
|
||||||
new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName,
|
new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName,
|
||||||
revisionInfoClass, revisionInfoTimestampData);
|
revisionInfoClass, revisionInfoTimestampData);
|
||||||
|
|
|
@ -27,9 +27,9 @@ import static org.hibernate.envers.tools.ArgumentsTools.checkPositive;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.persistence.NoResultException;
|
import javax.persistence.NoResultException;
|
||||||
|
import org.hibernate.Criteria;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.NonUniqueResultException;
|
import org.hibernate.NonUniqueResultException;
|
||||||
import org.hibernate.Query;
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.engine.SessionImplementor;
|
import org.hibernate.engine.SessionImplementor;
|
||||||
import org.hibernate.envers.ModifiedEntityNames;
|
import org.hibernate.envers.ModifiedEntityNames;
|
||||||
|
@ -153,7 +153,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
||||||
checkPositive(revision, "Entity revision");
|
checkPositive(revision, "Entity revision");
|
||||||
checkSession();
|
checkSession();
|
||||||
|
|
||||||
Query query = verCfg.getRevisionInfoQueryCreator().getRevisionDateQuery(session, revision);
|
Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionDateQuery(session, revision);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Object timestampObject = query.uniqueResult();
|
Object timestampObject = query.uniqueResult();
|
||||||
|
@ -172,7 +172,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
||||||
checkNotNull(date, "Date of revision");
|
checkNotNull(date, "Date of revision");
|
||||||
checkSession();
|
checkSession();
|
||||||
|
|
||||||
Query query = verCfg.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery(session, date);
|
Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery(session, date);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Number res = (Number) query.uniqueResult();
|
Number res = (Number) query.uniqueResult();
|
||||||
|
@ -195,7 +195,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
||||||
|
|
||||||
Set<Number> revisions = new HashSet<Number>(1);
|
Set<Number> revisions = new HashSet<Number>(1);
|
||||||
revisions.add(revision);
|
revisions.add(revision);
|
||||||
Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
T revisionData = (T) query.uniqueResult();
|
T revisionData = (T) query.uniqueResult();
|
||||||
|
@ -221,7 +221,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
||||||
}
|
}
|
||||||
checkSession();
|
checkSession();
|
||||||
|
|
||||||
Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<T> revisionList = query.list();
|
List<T> revisionList = query.list();
|
||||||
|
@ -288,7 +288,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
||||||
}
|
}
|
||||||
Set<Number> revisions = new HashSet<Number>(1);
|
Set<Number> revisions = new HashSet<Number>(1);
|
||||||
revisions.add(revision);
|
revisions.add(revision);
|
||||||
Query query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
Criteria query = verCfg.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
|
||||||
Object revisionInfo = query.uniqueResult();
|
Object revisionInfo = query.uniqueResult();
|
||||||
if (revisionInfo != null) {
|
if (revisionInfo != null) {
|
||||||
// If revision exists
|
// If revision exists
|
||||||
|
|
|
@ -23,8 +23,10 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.envers.revisioninfo;
|
package org.hibernate.envers.revisioninfo;
|
||||||
|
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Criteria;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.criterion.Projections;
|
||||||
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -34,56 +36,30 @@ import java.util.Set;
|
||||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||||
*/
|
*/
|
||||||
public class RevisionInfoQueryCreator {
|
public class RevisionInfoQueryCreator {
|
||||||
private final String revisionDateQuery;
|
private final String revisionInfoEntityName;
|
||||||
private final String revisionNumberForDateQuery;
|
private final String revisionInfoIdName;
|
||||||
private final String revisionsQuery;
|
private final String revisionInfoTimestampName;
|
||||||
private final boolean timestampAsDate;
|
private final boolean timestampAsDate;
|
||||||
|
|
||||||
public RevisionInfoQueryCreator(String revisionInfoEntityName, String revisionInfoIdName,
|
public RevisionInfoQueryCreator(String revisionInfoEntityName, String revisionInfoIdName,
|
||||||
String revisionInfoTimestampName, boolean timestampAsDate,
|
String revisionInfoTimestampName, boolean timestampAsDate) {
|
||||||
String modifiedEntityNamesName) {
|
this.revisionInfoEntityName = revisionInfoEntityName;
|
||||||
|
this.revisionInfoIdName = revisionInfoIdName;
|
||||||
|
this.revisionInfoTimestampName = revisionInfoTimestampName;
|
||||||
this.timestampAsDate = timestampAsDate;
|
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) {
|
public Criteria getRevisionDateQuery(Session session, Number revision) {
|
||||||
return session.createQuery(revisionDateQuery).setParameter("_revision_number", revision);
|
return session.createCriteria(revisionInfoEntityName).setProjection(Projections.property(revisionInfoTimestampName))
|
||||||
|
.add(Restrictions.eq(revisionInfoIdName, revision));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query getRevisionNumberForDateQuery(Session session, Date date) {
|
public Criteria getRevisionNumberForDateQuery(Session session, Date date) {
|
||||||
return session.createQuery(revisionNumberForDateQuery).setParameter("_revision_date", timestampAsDate ? date : date.getTime());
|
return session.createCriteria(revisionInfoEntityName).setProjection(Projections.max(revisionInfoIdName))
|
||||||
|
.add(Restrictions.le(revisionInfoTimestampName, timestampAsDate ? date : date.getTime()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query getRevisionsQuery(Session session, Set<Number> revisions) {
|
public Criteria getRevisionsQuery(Session session, Set<Number> revisions) {
|
||||||
return session.createQuery(revisionsQuery).setParameterList("_revision_numbers", revisions);
|
return session.createCriteria(revisionInfoEntityName).add(Restrictions.in(revisionInfoIdName, revisions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue