Author: adamw
Date: 2008-08-14 06:02:22 -0400 (Thu, 14 Aug 2008)
New Revision: 100
Modified:
trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java
trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java
trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java
trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java
trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java
trunk/src/test/org/jboss/envers/test/working/WorkingMain.java
Log:
ENVERS-36: an additonal parametr for the revisions of entity query, stating if deleted
entities should be selected, and tests
Modified: trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java
===================================================================
--- trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java 2008-08-14 09:36:38 UTC
(rev 99)
+++ trunk/src/main/org/jboss/envers/entity/EntityInstantiator.java 2008-08-14 10:02:22 UTC
(rev 100)
@@ -21,7 +21,6 @@
*/
package org.jboss.envers.entity;
-import org.jboss.envers.RevisionType;
import org.jboss.envers.exception.VersionsException;
import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.mapper.id.IdMapper;
@@ -56,11 +55,6 @@
if (versionsEntity == null) {
return null;
}
-
- if
(RevisionType.DEL.equals(versionsEntity.get(verCfg.getEntitiesCfg().getRevisionTypePropName())))
{
- // This means that at the given revision the entity didn't yet exist.
- return null;
- }
// The $type$ property holds the name of the (versions) entity
String type = verCfg.getEntityNameForVersionsEntityName((String)
versionsEntity.get("$type$"));
Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-08-14 09:36:38
UTC (rev 99)
+++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-08-14 10:02:22
UTC (rev 100)
@@ -59,12 +59,14 @@
* @param c Class of the entities for which to query.
* @param selectEntitiesOnly If true, instead of a list of three-element arrays, a
list of entites will be
* returned as a result of executing this query.
+ * @param selectDeletedEntities If true, also revisions where entities where deleted
will be returned. The
+ * entities will have revision type "delete", and contain no data (all
fields null), except for the id field.
* @return A query for revisions at which instances of the given entity were
modified, to which
* conditions can be added (for example - a specific id of an entity of class
<code>c</code>), and which
* can then be executed. The results of the query will be sorted in ascending order
by the revision number,
* unless an order or projection is added.
*/
- public VersionsQuery forRevisionsOfEntity(Class<?> c, boolean
selectEntitiesOnly) {
- return new RevisionsOfEntityQuery(versionsReaderImplementor, c,
selectEntitiesOnly);
+ public VersionsQuery forRevisionsOfEntity(Class<?> c, boolean
selectEntitiesOnly, boolean selectDeletedEntities) {
+ return new RevisionsOfEntityQuery(versionsReaderImplementor, c,
selectEntitiesOnly,selectDeletedEntities);
}
}
Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-08-14
09:36:38 UTC (rev 99)
+++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-08-14
10:02:22 UTC (rev 100)
@@ -26,6 +26,7 @@
import org.jboss.envers.query.criteria.VersionsCriterion;
import org.jboss.envers.RevisionType;
import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Property;
import java.util.List;
import java.util.ArrayList;
@@ -35,12 +36,15 @@
* @author Adam Warski (adam at warski dot org)
*/
public class RevisionsOfEntityQuery extends AbstractVersionsQuery {
- private boolean selectEntitiesOnly;
+ private final boolean selectEntitiesOnly;
+ private final boolean selectDeletedEntities;
- public RevisionsOfEntityQuery(VersionsReaderImplementor versionsReader,
Class<?> cls, boolean selectEntitiesOnly) {
+ public RevisionsOfEntityQuery(VersionsReaderImplementor versionsReader,
Class<?> cls, boolean selectEntitiesOnly,
+ boolean selectDeletedEntities) {
super(versionsReader, cls);
this.selectEntitiesOnly = selectEntitiesOnly;
+ this.selectDeletedEntities = selectDeletedEntities;
}
@SuppressWarnings({"unchecked"})
@@ -48,11 +52,17 @@
/*
The query that should be executed in the versions table:
SELECT e (unless another projection is specified) FROM ent_ver e WHERE
+ e.revision_type != DEL AND (if selectDeletedEntities == false)
(all specified conditions, transformed, on the "e" entity)
ORDER BY e.revision ASC (unless another order is specified)
*/
String revisionPropertyPath =
versionsReader.getEntitiesCfg().getRevisionPropPath();
+ if (!selectDeletedEntities) {
+ // e.revision_type != DEL AND
+
versionsCriteria.add(Property.forName(versionsReader.getEntitiesCfg().getRevisionTypePropName()).ne(RevisionType.DEL));
+ }
+
// all specified conditions, transformed
for (VersionsCriterion criterion : criterions) {
versionsCriteria.add(criterion.toVersionsCriterion(entityName,
versionsReader));
Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
===================================================================
--- trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-14 09:36:38 UTC
(rev 99)
+++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-14 10:02:22 UTC
(rev 100)
@@ -154,7 +154,7 @@
throw new NotVersionedException(entityName + " is not
versioned!");
}
- return createQuery().forRevisionsOfEntity(cls, false)
+ return createQuery().forRevisionsOfEntity(cls, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.idEq(primaryKey))
.getResultList();
Modified: trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-08-14
09:36:38 UTC (rev 99)
+++ trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-08-14
10:02:22 UTC (rev 100)
@@ -66,4 +66,18 @@
assert (Integer)
getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 2)
.setProjection(Projections.count("originalId.id")).getResultList().get(0) == 1;
}
+
+ @Test
+ public void testRevisionsOfEntityWithoutDelete() {
+ List result = getVersionsReader().createQuery()
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, false)
+ .add(VersionsRestrictions.idEq(id2))
+ .getResultList();
+
+ assert result.size() == 1;
+
+ assert ((Object []) result.get(0))[0].equals(new StrIntTestEntity("b",
11, id2));
+ assert ((Object []) result.get(0))[1].equals(1);
+ assert ((Object []) result.get(0))[2].equals(RevisionType.ADD);
+ }
}
Modified:
trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java 2008-08-14
09:36:38 UTC (rev 99)
+++
trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java 2008-08-14
10:02:22 UTC (rev 100)
@@ -78,7 +78,7 @@
@Test
public void testMaximizeWithIdEq() {
List revs_id1 = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.maximizeProperty("number")
.add(VersionsRestrictions.idEq(id2)))
@@ -90,7 +90,7 @@
@Test
public void testMinimizeWithPropertyEq() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.minimizeProperty("number")
.add(VersionsRestrictions.eq("str1", "a")))
Modified:
trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java 2008-08-14
09:36:38 UTC (rev 99)
+++
trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java 2008-08-14
10:02:22 UTC (rev 100)
@@ -78,7 +78,7 @@
@Test
public void testRevisionsLtQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(VersionsProjections.distinct(RevisionProperty.revisionNumber()))
.add(RevisionProperty.lt(3))
.getResultList();
@@ -89,7 +89,7 @@
@Test
public void testRevisionsGeQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(VersionsProjections.distinct(RevisionProperty.revisionNumber()))
.add(RevisionProperty.ge(2))
.getResultList();
@@ -100,7 +100,7 @@
@Test
public void testRevisionsLeWithPropertyQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(RevisionProperty.le(3))
.add(VersionsRestrictions.eq("str1", "a"))
@@ -112,7 +112,7 @@
@Test
public void testRevisionsGtWithPropertyQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(RevisionProperty.gt(1))
.add(VersionsRestrictions.lt("number", 10))
@@ -124,7 +124,7 @@
@Test
public void testRevisionProjectionQuery() {
Object[] result = (Object[]) getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(
VersionsProjections.projectionList()
.add(RevisionProperty.max())
@@ -141,7 +141,7 @@
@Test
public void testRevisionOrderQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.idEq(id1))
.addOrder(RevisionProperty.desc())
@@ -154,7 +154,7 @@
public void testRevisionCountQuery() {
// The query shouldn't be ordered as always, otherwise - we get an
exception.
Object result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.count())
.add(VersionsRestrictions.idEq(id1))
.getSingleResult();
Modified: trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-08-14
09:36:38 UTC (rev 99)
+++ trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-08-14
10:02:22 UTC (rev 100)
@@ -141,21 +141,21 @@
@Test
public void testRevisionsPropertyEqQuery() {
List revs_id1 = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.le("str1", "a"))
.add(VersionsRestrictions.idEq(id1))
.getResultList();
List revs_id2 = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.le("str1", "a"))
.add(VersionsRestrictions.idEq(id2))
.getResultList();
List revs_id3 = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionProperty.revisionNumber())
.add(VersionsRestrictions.le("str1", "a"))
.add(VersionsRestrictions.idEq(id3))
@@ -169,10 +169,12 @@
@Test
public void testSelectEntitiesQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, true)
+ .forRevisionsOfEntity(StrIntTestEntity.class, true, false)
.add(VersionsRestrictions.idEq(id1))
.getResultList();
+ assert result.size() == 2;
+
assert result.get(0).equals(new StrIntTestEntity("a", 10, id1));
assert result.get(1).equals(new StrIntTestEntity("c", 10, id1));
}
@@ -180,13 +182,15 @@
@Test
public void testSelectEntitiesAndRevisionsQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.add(VersionsRestrictions.idEq(id1))
.getResultList();
+ assert result.size() == 3;
+
assert ((Object []) result.get(0))[0].equals(new StrIntTestEntity("a",
10, id1));
assert ((Object []) result.get(1))[0].equals(new StrIntTestEntity("c",
10, id1));
- assert ((Object []) result.get(2))[0] == null;
+ assert ((Object []) result.get(2))[0].equals(new StrIntTestEntity(null, null,
id1));
assert ((Object []) result.get(0))[1].equals(1);
assert ((Object []) result.get(1))[1].equals(2);
@@ -200,11 +204,13 @@
@Test
public void testSelectRevisionTypeQuery() {
List result = getVersionsReader().createQuery()
- .forRevisionsOfEntity(StrIntTestEntity.class, false)
+ .forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.setProjection(RevisionTypeProperty.revisionType())
.add(VersionsRestrictions.idEq(id1))
.getResultList();
+ assert result.size() == 3;
+
assert result.get(0).equals(RevisionType.ADD);
assert result.get(1).equals(RevisionType.MOD);
assert result.get(2).equals(RevisionType.DEL);
Modified: trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java 2008-08-14 09:36:38
UTC (rev 99)
+++ trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java 2008-08-14 10:02:22
UTC (rev 100)
@@ -5,6 +5,7 @@
import javax.persistence.*;
import java.util.Set;
+import java.util.List;
/**
* @author Adam Warski (adam at warski dot org)
@@ -21,7 +22,7 @@
private String str2;
@Versioned
- @OneToMany(mappedBy = "we2")
+ @OneToMany(mappedBy="we2")
//@ManyToMany
//@Transient
private Set<WorkingEntity1> we1s;
Modified: trunk/src/test/org/jboss/envers/test/working/WorkingMain.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/working/WorkingMain.java 2008-08-14 09:36:38 UTC
(rev 99)
+++ trunk/src/test/org/jboss/envers/test/working/WorkingMain.java 2008-08-14 10:02:22 UTC
(rev 100)
@@ -7,6 +7,7 @@
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
+import org.hibernate.criterion.Projections;
import org.jboss.envers.VersionsReader;
import org.jboss.envers.VersionsReaderFactory;
import org.jboss.envers.test.entities.ids.EmbId;
@@ -16,6 +17,8 @@
import org.jboss.envers.test.integration.inheritance.single.relation.ParentIngEntity;
import org.jboss.envers.test.integration.inheritance.single.relation.ChildIngEntity;
import org.jboss.envers.query.criteria.VersionsCriterion;
+import org.jboss.envers.query.VersionsRestrictions;
+import org.jboss.envers.query.VersionsProjections;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@@ -57,9 +60,43 @@
// Rev 1
entityManager.getTransaction().begin();
+ WorkingEntity2 we2 = new WorkingEntity2("s");
+ entityManager.persist(we2);
+ WorkingEntity1 we1 = new WorkingEntity1("b", "c");
+ we1.setWe2(we2);
+ entityManager.persist(we1);
- entityManager.getTransaction().commit();
+ WorkingEntity1 we1_2 = new WorkingEntity1("b2", "c2");
+ we1_2.setWe2(we2);
+ entityManager.persist(we1_2);
+
+ entityManager.getTransaction().commit();
+
+ // Rev 2
+ entityManager.getTransaction().begin();
+
+ we1 = entityManager.find(WorkingEntity1.class, we1.getId1());
+ entityManager.remove(we1);
+
+ entityManager.getTransaction().commit();
+
+ // Rev 3
+ entityManager.getTransaction().begin();
+
+ VersionsReader vr = VersionsReaderFactory.get(entityManager);
+
+ System.out.println(vr.createQuery().forEntitiesAtRevision(WorkingEntity1.class,
1)
+ .getResultList());
+ System.out.println(vr.createQuery().forEntitiesAtRevision(WorkingEntity1.class,
1)
+
.setProjection(Projections.count("originalId.id1")).getResultList());
+
+ System.out.println(vr.createQuery().forEntitiesAtRevision(WorkingEntity1.class,
2)
+ .getResultList());
+ System.out.println(vr.createQuery().forEntitiesAtRevision(WorkingEntity1.class,
2)
+
.setProjection(Projections.count("originalId.id1")).getResultList());
+
+ entityManager.getTransaction().commit();
}
}