HHH-3598: revision type property can be used as query restriction

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15550 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2008-11-12 11:03:15 +00:00
parent 6d74f28f9e
commit 1013fef216
5 changed files with 114 additions and 8 deletions

View File

@ -39,6 +39,20 @@ import org.hibernate.envers.tools.Triple;
public class RevisionProperty implements AuditProjection {
private RevisionProperty() { }
/**
* Apply a "equal" constraint on the revision number
*/
public static AuditCriterion eq(Integer revision) {
return new RevisionAuditExpression(revision, "=");
}
/**
* Apply a "not equal" constraint on the revision number
*/
public static AuditCriterion ne(Integer revision) {
return new RevisionAuditExpression(revision, "<>");
}
/**
* Apply a "greater than" constraint on the revision number
*/

View File

@ -25,7 +25,10 @@ package org.hibernate.envers.query;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.projection.AuditProjection;
import org.hibernate.envers.query.criteria.AuditCriterion;
import org.hibernate.envers.query.criteria.RevisionTypeAuditExpression;
import org.hibernate.envers.tools.Triple;
import org.hibernate.envers.RevisionType;
/**
* @author Adam Warski (adam at warski dot org)
@ -34,6 +37,20 @@ import org.hibernate.envers.tools.Triple;
public class RevisionTypeProperty implements AuditProjection {
private RevisionTypeProperty() { }
/**
* Apply a "equal" constraint on the revision type
*/
public static AuditCriterion eq(RevisionType type) {
return new RevisionTypeAuditExpression(type, "=");
}
/**
* Apply a "not equal" constraint on the revision type
*/
public static AuditCriterion ne(RevisionType type) {
return new RevisionTypeAuditExpression(type, "<>");
}
/**
* Projection on the revision type
*/

View File

@ -0,0 +1,46 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class RevisionTypeAuditExpression implements AuditCriterion {
private Object value;
private String op;
public RevisionTypeAuditExpression(Object value, String op) {
this.value = value;
this.op = op;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
parameters.addWhereWithParam(verCfg.getAuditEntCfg().getRevisionTypePropName(), op, value);
}
}

View File

@ -24,9 +24,9 @@
package org.hibernate.envers.synchronization;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.envers.revisioninfo.RevisionInfoGenerator;
import org.hibernate.envers.tools.ConcurrentReferenceHashMap;
import org.hibernate.Transaction;
import org.hibernate.event.EventSource;
@ -35,13 +35,11 @@ import org.hibernate.event.EventSource;
* @author Adam Warski (adam at warski dot org)
*/
public class AuditSyncManager {
private final Map<Transaction, AuditSync> versionsSyncs;
private final Map<Transaction, AuditSync> auditSyncs;
private final RevisionInfoGenerator revisionInfoGenerator;
public AuditSyncManager(RevisionInfoGenerator revisionInfoGenerator) {
versionsSyncs = new ConcurrentReferenceHashMap<Transaction, AuditSync>(10,
ConcurrentReferenceHashMap.ReferenceType.WEAK,
ConcurrentReferenceHashMap.ReferenceType.STRONG);
auditSyncs = new ConcurrentHashMap<Transaction, AuditSync>();
this.revisionInfoGenerator = revisionInfoGenerator;
}
@ -49,10 +47,11 @@ public class AuditSyncManager {
public AuditSync get(EventSource session) {
Transaction transaction = session.getTransaction();
AuditSync verSync = versionsSyncs.get(transaction);
AuditSync verSync = auditSyncs.get(transaction);
if (verSync == null) {
// No worries about registering a transaction twice - a transaction is single thread
verSync = new AuditSync(this, session, revisionInfoGenerator);
versionsSyncs.put(transaction, verSync);
auditSyncs.put(transaction, verSync);
transaction.registerSynchronization(verSync);
}
@ -61,6 +60,6 @@ public class AuditSyncManager {
}
public void remove(Transaction transaction) {
versionsSyncs.remove(transaction);
auditSyncs.remove(transaction);
}
}

View File

@ -29,8 +29,10 @@ import javax.persistence.EntityManager;
import org.hibernate.envers.query.RevisionProperty;
import org.hibernate.envers.query.AuditRestrictions;
import org.hibernate.envers.query.RevisionTypeProperty;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.RevisionType;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -184,4 +186,32 @@ public class RevisionConstraintQuery extends AbstractEntityTest {
assert (Long) result == 4;
}
@Test
public void testRevisionTypeEqQuery() {
// The query shouldn't be ordered as always, otherwise - we get an exception.
List results = getAuditReader().createQuery()
.forRevisionsOfEntity(StrIntTestEntity.class, true, true)
.add(AuditRestrictions.idEq(id1))
.add(RevisionTypeProperty.eq(RevisionType.MOD))
.getResultList();
assert results.size() == 3;
assert results.get(0).equals(new StrIntTestEntity("d", 10, id1));
assert results.get(1).equals(new StrIntTestEntity("d", 1, id1));
assert results.get(2).equals(new StrIntTestEntity("d", 5, id1));
}
@Test
public void testRevisionTypeNeQuery() {
// The query shouldn't be ordered as always, otherwise - we get an exception.
List results = getAuditReader().createQuery()
.forRevisionsOfEntity(StrIntTestEntity.class, true, true)
.add(AuditRestrictions.idEq(id1))
.add(RevisionTypeProperty.ne(RevisionType.MOD))
.getResultList();
assert results.size() == 1;
assert results.get(0).equals(new StrIntTestEntity("a", 10, id1));
}
}