Author: adamw
Date: 2008-08-21 07:18:53 -0400 (Thu, 21 Aug 2008)
New Revision: 117
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
trunk/src/test/org/jboss/envers/test/entities/onetomany/unidirectional/
trunk/src/test/org/jboss/envers/test/entities/onetomany/unidirectional/SetRefCollEntity.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/unidirectional/
trunk/src/test/org/jboss/envers/test/integration/onetomany/unidirectional/BasicNotOwnedSet.java
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyIdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java
Modified:
trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/KeepCheckVisitor.java
trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java
trunk/src/test/org/jboss/envers/test/working/WorkingEntity1.java
trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java
Log:
ENVERS-26: refactoring, first test
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,64 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.OneToManyBidirectionalInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyBidirectionalMapper extends AbstractOneToManyMapper implements
PropertyMapper {
- private final String owningReferencePropertyName;
-
- public OneToManyBidirectionalMapper(String owningReferencePropertyName, String
owningEntityName, String propertyName) {
- super(owningEntityName, propertyName);
-
- this.owningReferencePropertyName = owningReferencePropertyName;
- }
-
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- return false;
- }
-
- protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
-
VersionsReaderImplementor versionsReader,
- Class<?>
entityClass, Object primaryKey,
- Number revision,
- Class<T>
collectionClass) {
- return new OneToManyBidirectionalInitializor<T>(versionsReader,
entityClass, owningReferencePropertyName, primaryKey,
- revision, collectionClass);
- }
-
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName, PersistentCollection newColl,
- Serializable
oldColl, Serializable id) {
- return null;
- }
-}
\ No newline at end of file
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,64 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.entities.mapper.relation.lazy.OneToManyBidirectionalInitializor;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.hibernate.collection.PersistentCollection;
+
+import java.util.*;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToManyBidirectionalMapper extends AbstractOneToManyMapper implements
PropertyMapper {
+ private final String owningReferencePropertyName;
+
+ public OneToManyBidirectionalMapper(String owningReferencePropertyName, String
owningEntityName, String propertyName) {
+ super(owningEntityName, propertyName);
+
+ this.owningReferencePropertyName = owningReferencePropertyName;
+ }
+
+ public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
+ return false;
+ }
+
+ protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+
VersionsReaderImplementor versionsReader,
+ Class<?>
entityClass, Object primaryKey,
+ Number revision,
+ Class<T>
collectionClass) {
+ return new OneToManyBidirectionalInitializor<T>(versionsReader,
entityClass, owningReferencePropertyName, primaryKey,
+ revision, collectionClass);
+ }
+
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName, PersistentCollection newColl,
+ Serializable
oldColl, Serializable id) {
+ return null;
+ }
+}
\ No newline at end of file
Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyIdMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyIdMapper.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyIdMapper.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,90 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.OneToManyInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
-import org.jboss.envers.exception.VersionsException;
-import org.jboss.envers.tools.reflection.ReflectionTools;
-import org.hibernate.property.Setter;
-import org.hibernate.property.Getter;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyIdMapper implements PropertyMapper {
- private final String owningReferencePropertyName;
- private final String owningEntityName;
- private final String propertyName;
-
- public OneToManyIdMapper(String owningReferencePropertyName, String owningEntityName,
String propertyName) {
- this.owningReferencePropertyName = owningReferencePropertyName;
- this.owningEntityName = owningEntityName;
- this.propertyName = propertyName;
- }
-
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- return false;
- }
-
- @SuppressWarnings({"unchecked"})
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
- if (obj == null) {
- return;
- }
-
- Class<?> entityClass = ReflectionTools.loadClass(owningEntityName);
-
- Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyName);
- Class collectionClass = getter.getReturnType();
-
- // todo: investigate generics
- Object value;
- if (List.class.isAssignableFrom(collectionClass)) {
- value = new ListProxy(new
OneToManyInitializor<ArrayList>(versionsReader, entityClass,
- owningReferencePropertyName, primaryKey, revision,
ArrayList.class));
- } else if (Set.class.isAssignableFrom(collectionClass) ||
Collection.class.isAssignableFrom(collectionClass)) {
- value = new SetProxy(new OneToManyInitializor<HashSet>(versionsReader,
entityClass,
- owningReferencePropertyName, primaryKey, revision,
HashSet.class));
- } else {
- throw new VersionsException("Unsupported versioned collection type:
" + collectionClass.getName());
- }
-
- Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
- setter.set(obj, value, null);
- }
-
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
-
PersistentCollection newColl,
-
Serializable oldColl,
-
Serializable id) {
- return null;
- }
-}
\ No newline at end of file
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,116 +0,0 @@
-package org.jboss.envers.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
-import
org.jboss.envers.entities.mapper.relation.lazy.OneToManyUnidirectionalInitializor;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.RevisionType;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyUnidirectionalMapper extends AbstractOneToManyMapper implements
PropertyMapper {
- private final VersionsEntitiesConfiguration verEntCfg;
- private final String referencedEntityName;
- private final String collectionReferencingPropertyName;
- private final String versionsReferencedEntityName;
- private final String versionsMiddleEntityName;
- private final IdMapper referencingMiddleIdMapper;
- private final IdMapper referencedMiddleIdMapper;
-
- private final UnidirectionalRelationQueryGenerator queryGenerator;
-
- public OneToManyUnidirectionalMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
- String referencedEntityName, String
collectionReferencingPropertyName,
- String versionsReferencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- super(referencingEntityName, collectionReferencingPropertyName);
-
- this.verEntCfg = verEntCfg;
- this.referencedEntityName = referencedEntityName;
- this.collectionReferencingPropertyName = collectionReferencingPropertyName;
- this.versionsReferencedEntityName = versionsReferencedEntityName;
- this.versionsMiddleEntityName = versionsMiddleEntityName;
- this.referencingMiddleIdMapper = referencingMiddleIdMapper;
- this.referencedMiddleIdMapper = referencedMiddleIdMapper;
-
- queryGenerator = new UnidirectionalRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
- versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
- }
-
- private Collection getOldCollection(Serializable oldColl) {
- if (oldColl instanceof Map) {
- return ((Map) oldColl).keySet();
- } else {
- return (Collection) oldColl;
- }
- }
-
- private void addCollectionChanges(List<PersistentCollectionChangeData>
collectionChanges, Set<Object> changed,
- RevisionType revisionType, Serializable id) {
- for (Object changedEntity : changed) {
- Map<String, Object> entityData = new HashMap<String, Object>();
- Map<String, Object> originalId = new HashMap<String, Object>();
- entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
-
- collectionChanges.add(new
PersistentCollectionChangeData(versionsMiddleEntityName, entityData));
- referencingMiddleIdMapper.mapToMapFromId(originalId, id);
- referencedMiddleIdMapper.mapToMapFromEntity(originalId, changedEntity);
-
- entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
-
- System.out.println("entityData = " + entityData);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
- PersistentCollection
newColl,
- Serializable
oldColl, Serializable id) {
- if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
- return null;
- }
-
- List<PersistentCollectionChangeData> collectionChanges = new
ArrayList<PersistentCollectionChangeData>();
-
- Collection newCollection = (Collection) newColl;
- Collection oldCollection = getOldCollection(oldColl);
-
- Set<Object> added = new HashSet<Object>();
- if (newColl != null) { added.addAll(newCollection); }
- if (oldColl != null) { added.removeAll(oldCollection); }
-
- addCollectionChanges(collectionChanges, added, RevisionType.ADD, id);
-
- Set<Object> deleted = new HashSet<Object>();
- if (oldColl != null) { deleted.addAll(oldCollection); }
- if (newColl != null) { deleted.removeAll(newCollection); }
-
- addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id);
-
- return collectionChanges;
- }
-
- @SuppressWarnings({"unchecked"})
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- // Changes are mapped in the "mapCollectionChanges" method.
- return false;
- }
-
- protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
-
VersionsReaderImplementor versionsReader,
- Class<?>
entityClass, Object primaryKey,
- Number revision,
Class<T> collectionClass) {
- return new OneToManyUnidirectionalInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
- versionsReader, primaryKey, revision, collectionClass);
- }
-}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,112 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import
org.jboss.envers.entities.mapper.relation.lazy.OneToManyUnidirectionalInitializor;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.RevisionType;
+import org.hibernate.collection.PersistentCollection;
+
+import java.util.*;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToManyUnidirectionalMapper extends AbstractOneToManyMapper implements
PropertyMapper {
+ private final VersionsEntitiesConfiguration verEntCfg;
+ private final String referencedEntityName;
+ private final String collectionReferencingPropertyName;
+ private final String versionsMiddleEntityName;
+ private final IdMapper referencingMiddleIdMapper;
+ private final IdMapper referencedMiddleIdMapper;
+
+ private final UnidirectionalRelationQueryGenerator queryGenerator;
+
+ public OneToManyUnidirectionalMapper(VersionsEntitiesConfiguration verEntCfg, String
referencingEntityName,
+ String referencedEntityName, String
collectionReferencingPropertyName,
+ String versionsReferencedEntityName, String
versionsMiddleEntityName,
+ IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
+ IdMapper referencedIdMapper) {
+ super(referencingEntityName, collectionReferencingPropertyName);
+
+ this.verEntCfg = verEntCfg;
+ this.referencedEntityName = referencedEntityName;
+ this.collectionReferencingPropertyName = collectionReferencingPropertyName;
+ this.versionsMiddleEntityName = versionsMiddleEntityName;
+ this.referencingMiddleIdMapper = referencingMiddleIdMapper;
+ this.referencedMiddleIdMapper = referencedMiddleIdMapper;
+
+ queryGenerator = new UnidirectionalRelationQueryGenerator(verEntCfg,
versionsReferencedEntityName,
+ versionsMiddleEntityName, referencingMiddleIdMapper,
referencedMiddleIdMapper, referencedIdMapper);
+ }
+
+ private Collection getOldCollection(Serializable oldColl) {
+ if (oldColl instanceof Map) {
+ return ((Map) oldColl).keySet();
+ } else {
+ return (Collection) oldColl;
+ }
+ }
+
+ private void addCollectionChanges(List<PersistentCollectionChangeData>
collectionChanges, Set<Object> changed,
+ RevisionType revisionType, Serializable id) {
+ for (Object changedEntity : changed) {
+ Map<String, Object> entityData = new HashMap<String, Object>();
+ Map<String, Object> originalId = new HashMap<String, Object>();
+ entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
+
+ collectionChanges.add(new
PersistentCollectionChangeData(versionsMiddleEntityName, entityData));
+ referencingMiddleIdMapper.mapToMapFromId(originalId, id);
+ referencedMiddleIdMapper.mapToMapFromEntity(originalId, changedEntity);
+
+ entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
+ PersistentCollection
newColl,
+ Serializable
oldColl, Serializable id) {
+ if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
+ return null;
+ }
+
+ List<PersistentCollectionChangeData> collectionChanges = new
ArrayList<PersistentCollectionChangeData>();
+
+ Collection newCollection = (Collection) newColl;
+ Collection oldCollection = getOldCollection(oldColl);
+
+ Set<Object> added = new HashSet<Object>();
+ if (newColl != null) { added.addAll(newCollection); }
+ if (oldColl != null) { added.removeAll(oldCollection); }
+
+ addCollectionChanges(collectionChanges, added, RevisionType.ADD, id);
+
+ Set<Object> deleted = new HashSet<Object>();
+ if (oldColl != null) { deleted.addAll(oldCollection); }
+ if (newColl != null) { deleted.removeAll(newCollection); }
+
+ addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id);
+
+ return collectionChanges;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
+ // Changes are mapped in the "mapCollectionChanges" method.
+ return false;
+ }
+
+ protected <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+
VersionsReaderImplementor versionsReader,
+ Class<?>
entityClass, Object primaryKey,
+ Number revision,
Class<T> collectionClass) {
+ return new OneToManyUnidirectionalInitializor<T>(verCfg,
referencedEntityName, queryGenerator,
+ versionsReader, primaryKey, revision, collectionClass);
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,93 +0,0 @@
-package org.jboss.envers.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.id.QueryParameterData;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.RevisionType;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.hibernate.Query;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class UnidirectionalRelationQueryGenerator {
- private final String queryString;
- private final IdMapper referencingMiddleIdMapper;
-
- public UnidirectionalRelationQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
- String versionsReferencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- this.referencingMiddleIdMapper = referencingMiddleIdMapper;
-
- /*
- * The query that we need to create:
- * SELECT e FROM versionsReferencedEntity e, middleEntity ee
- * WHERE
- * (selecting e entities at revision :revision)
- * e.revision = (SELECT max(e2.revision) FROM referencedEntity e2
- * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND
- * (only entities referenced by the association)
- * ee.id1 = e.id1 AND
- * ee.id2 = :id2 AND
- * (the association at revision :revision)
- * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
- * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 =
ee.id1) AND
- * (only non-deleted entities and associations)
- * ee.revision_type != DEL AND
- * e.revision_type != DEL
- */
- String revisionPropertyPath = verEntCfg.getRevisionPropPath();
- String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
-
- StringBuilder querySB = new StringBuilder();
- querySB
- .append("SELECT e FROM
").append(versionsReferencedEntityName).append(" e")
- .append(", ").append(versionsMiddleEntityName).append(" ee
")
- .append("WHERE ")
- // e.revision = (SELECT max(e2.revision) ...
- .append("e.").append(revisionPropertyPath).append(" =
(SELECT max(e2.").append(revisionPropertyPath)
- .append(") FROM
").append(versionsReferencedEntityName).append(" e2 ")
- // e2.revision <= :revision
- .append("WHERE e2.").append(revisionPropertyPath).append("
<= :revision AND ")
- // e2.id1 = e.id1)
- .append(referencedIdMapper.getIdsEqualQuery("e." +
originalIdPropertyName, "e2." + originalIdPropertyName))
- .append(") AND ")
- // ee.id1 = e.id1
- .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName,
- referencedIdMapper, "e." + originalIdPropertyName))
- .append(" AND ")
- // ee.id2 = :id2
- .append(referencingMiddleIdMapper.getIdEqualsQuery("ee." +
originalIdPropertyName, true))
- .append(" AND ")
- // ee.revision = (SELECT max(ee2.revision) ...
- .append("ee.").append(revisionPropertyPath).append(" =
(SELECT max(ee2.").append(revisionPropertyPath)
- .append(") FROM
").append(versionsMiddleEntityName).append(" ee2 ")
- // ee2.revision <= :revision
- .append("WHERE
ee2.").append(revisionPropertyPath).append(" <= :revision AND ")
- // ee2.id2 = :id2)
- .append(referencingMiddleIdMapper.getIdEqualsQuery("ee2." +
originalIdPropertyName, true))
- .append(" AND ")
- // ee2.id1 = ee.id1)
- .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName, "ee2." + originalIdPropertyName))
- .append(") AND ")
- // e.revision_type != DEL AND
-
.append("e.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype")
- .append(" AND ")
- // ee.revision_type != DEL
-
.append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype");
-
- queryString = querySB.toString();
- }
-
- public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
- Query query = versionsReader.getSession().createQuery(queryString);
- query.setParameter("revision", revision);
- query.setParameter("delrevisiontype", RevisionType.DEL);
- for (QueryParameterData paramData:
referencingMiddleIdMapper.mapToQueryParametersFromId(primaryKey)) {
- paramData.setParameterValue(query);
- }
-
- return query;
- }
-}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/UnidirectionalRelationQueryGenerator.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,93 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.id.QueryParameterData;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.RevisionType;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.hibernate.Query;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class UnidirectionalRelationQueryGenerator {
+ private final String queryString;
+ private final IdMapper referencingMiddleIdMapper;
+
+ public UnidirectionalRelationQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
+ String versionsReferencedEntityName, String
versionsMiddleEntityName,
+ IdMapper referencingMiddleIdMapper, IdMapper
referencedMiddleIdMapper,
+ IdMapper referencedIdMapper) {
+ this.referencingMiddleIdMapper = referencingMiddleIdMapper;
+
+ /*
+ * The query that we need to create:
+ * SELECT e FROM versionsReferencedEntity e, middleEntity ee
+ * WHERE
+ * (selecting e entities at revision :revision)
+ * e.revision = (SELECT max(e2.revision) FROM referencedEntity e2
+ * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND
+ * (only entities referenced by the association)
+ * ee.id1 = e.id1 AND
+ * ee.id2 = :id2 AND
+ * (the association at revision :revision)
+ * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
+ * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 =
ee.id1) AND
+ * (only non-deleted entities and associations)
+ * ee.revision_type != DEL AND
+ * e.revision_type != DEL
+ */
+ String revisionPropertyPath = verEntCfg.getRevisionPropPath();
+ String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
+
+ StringBuilder querySB = new StringBuilder();
+ querySB
+ .append("SELECT e FROM
").append(versionsReferencedEntityName).append(" e")
+ .append(", ").append(versionsMiddleEntityName).append(" ee
")
+ .append("WHERE ")
+ // e.revision = (SELECT max(e2.revision) ...
+ .append("e.").append(revisionPropertyPath).append(" =
(SELECT max(e2.").append(revisionPropertyPath)
+ .append(") FROM
").append(versionsReferencedEntityName).append(" e2 ")
+ // e2.revision <= :revision
+ .append("WHERE e2.").append(revisionPropertyPath).append("
<= :revision AND ")
+ // e2.id1 = e.id1)
+ .append(referencedIdMapper.getIdsEqualQuery("e." +
originalIdPropertyName, "e2." + originalIdPropertyName))
+ .append(") AND ")
+ // ee.id1 = e.id1
+ .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName,
+ referencedIdMapper, "e." + originalIdPropertyName))
+ .append(" AND ")
+ // ee.id2 = :id2
+ .append(referencingMiddleIdMapper.getIdEqualsQuery("ee." +
originalIdPropertyName, true))
+ .append(" AND ")
+ // ee.revision = (SELECT max(ee2.revision) ...
+ .append("ee.").append(revisionPropertyPath).append(" =
(SELECT max(ee2.").append(revisionPropertyPath)
+ .append(") FROM
").append(versionsMiddleEntityName).append(" ee2 ")
+ // ee2.revision <= :revision
+ .append("WHERE
ee2.").append(revisionPropertyPath).append(" <= :revision AND ")
+ // ee2.id2 = :id2)
+ .append(referencingMiddleIdMapper.getIdEqualsQuery("ee2." +
originalIdPropertyName, true))
+ .append(" AND ")
+ // ee2.id1 = ee.id1)
+ .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." +
originalIdPropertyName, "ee2." + originalIdPropertyName))
+ .append(") AND ")
+ // e.revision_type != DEL AND
+
.append("e.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype")
+ .append(" AND ")
+ // ee.revision_type != DEL
+
.append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" !=
").append(":delrevisiontype");
+
+ queryString = querySB.toString();
+ }
+
+ public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
+ Query query = versionsReader.getSession().createQuery(queryString);
+ query.setParameter("revision", revision);
+ query.setParameter("delrevisiontype", RevisionType.DEL);
+ for (QueryParameterData paramData:
referencingMiddleIdMapper.mapToQueryParametersFromId(primaryKey)) {
+ paramData.setParameterValue(query);
+ }
+
+ return query;
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,74 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.entities.mapper.relation.lazy;
-
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
-import org.jboss.envers.query.VersionsRestrictions;
-import org.jboss.envers.exception.VersionsException;
-
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyBidirectionalInitializor<T extends Collection> implements
Initializor<T> {
- private VersionsReaderImplementor versionsReader;
- private Class<?> entityClass;
- private String owningReferencePropertyName;
- private Object primaryKey;
- private Number revision;
- private Class<T> collectionClass;
-
- public OneToManyBidirectionalInitializor(VersionsReaderImplementor versionsReader,
Class<?> entityClass,
- String owningReferencePropertyName, Object primaryKey,
Number revision,
- Class<T> collectionClass) {
- this.versionsReader = versionsReader;
- this.entityClass = entityClass;
- this.owningReferencePropertyName = owningReferencePropertyName;
- this.primaryKey = primaryKey;
- this.revision = revision;
- this.collectionClass = collectionClass;
- }
-
- @SuppressWarnings({"unchecked"})
- public T initialize() {
- List queryResult =
versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision)
- .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName,
primaryKey)).getResultList();
-
- if (collectionClass.isAssignableFrom(queryResult.getClass())) {
- return (T) queryResult;
- } else {
- Collection result;
- try {
- result = collectionClass.newInstance();
- } catch (Exception e) {
- throw new VersionsException(e);
- }
-
- result.addAll(queryResult);
-
- return (T) result;
- }
- }
-}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,74 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.entities.mapper.relation.lazy;
+
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.query.VersionsRestrictions;
+import org.jboss.envers.exception.VersionsException;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToManyBidirectionalInitializor<T extends Collection> implements
Initializor<T> {
+ private VersionsReaderImplementor versionsReader;
+ private Class<?> entityClass;
+ private String owningReferencePropertyName;
+ private Object primaryKey;
+ private Number revision;
+ private Class<T> collectionClass;
+
+ public OneToManyBidirectionalInitializor(VersionsReaderImplementor versionsReader,
Class<?> entityClass,
+ String owningReferencePropertyName, Object primaryKey,
Number revision,
+ Class<T> collectionClass) {
+ this.versionsReader = versionsReader;
+ this.entityClass = entityClass;
+ this.owningReferencePropertyName = owningReferencePropertyName;
+ this.primaryKey = primaryKey;
+ this.revision = revision;
+ this.collectionClass = collectionClass;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public T initialize() {
+ List queryResult =
versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision)
+ .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName,
primaryKey)).getResultList();
+
+ if (collectionClass.isAssignableFrom(queryResult.getClass())) {
+ return (T) queryResult;
+ } else {
+ Collection result;
+ try {
+ result = collectionClass.newInstance();
+ } catch (Exception e) {
+ throw new VersionsException(e);
+ }
+
+ result.addAll(queryResult);
+
+ return (T) result;
+ }
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -1,55 +0,0 @@
-package org.jboss.envers.entities.mapper.relation.lazy;
-
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
-import org.jboss.envers.entities.mapper.relation.UnidirectionalRelationQueryGenerator;
-import org.jboss.envers.entities.EntityInstantiator;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.exception.VersionsException;
-import org.jboss.envers.configuration.VersionsConfiguration;
-
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyUnidirectionalInitializor<T extends Collection> implements
Initializor<T> {
- private final VersionsConfiguration verCfg;
- private final String entityName;
- private final UnidirectionalRelationQueryGenerator queryGenerator;
- private final VersionsReaderImplementor versionsReader;
- private final Object primaryKey;
- private final Number revision;
- private final Class<T> collectionClass;
-
- public OneToManyUnidirectionalInitializor(VersionsConfiguration verCfg, String
entityName,
- UnidirectionalRelationQueryGenerator
queryGenerator,
- VersionsReaderImplementor versionsReader,
Object primaryKey,
- Number revision, Class<T>
collectionClass) {
- this.verCfg = verCfg;
- this.entityName = entityName;
- this.queryGenerator = queryGenerator;
- this.versionsReader = versionsReader;
- this.primaryKey = primaryKey;
- this.revision = revision;
- this.collectionClass = collectionClass;
- }
-
- @SuppressWarnings({"unchecked"})
- public T initialize() {
- EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg,
versionsReader);
-
- List queryResult = queryGenerator.getQuery(versionsReader, primaryKey,
revision).list();
-
- T result;
- try {
- result = collectionClass.newInstance();
- } catch (Exception e) {
- throw new VersionsException(e);
- }
-
- entityInstantiator.addInstancesFromVersionsEntities(entityName, result,
queryResult, revision);
-
- return result;
- }
-}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyUnidirectionalInitializor.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,55 @@
+package org.jboss.envers.entities.mapper.relation.lazy;
+
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.relation.UnidirectionalRelationQueryGenerator;
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToManyUnidirectionalInitializor<T extends Collection> implements
Initializor<T> {
+ private final VersionsConfiguration verCfg;
+ private final String entityName;
+ private final UnidirectionalRelationQueryGenerator queryGenerator;
+ private final VersionsReaderImplementor versionsReader;
+ private final Object primaryKey;
+ private final Number revision;
+ private final Class<T> collectionClass;
+
+ public OneToManyUnidirectionalInitializor(VersionsConfiguration verCfg, String
entityName,
+ UnidirectionalRelationQueryGenerator
queryGenerator,
+ VersionsReaderImplementor versionsReader,
Object primaryKey,
+ Number revision, Class<T>
collectionClass) {
+ this.verCfg = verCfg;
+ this.entityName = entityName;
+ this.queryGenerator = queryGenerator;
+ this.versionsReader = versionsReader;
+ this.primaryKey = primaryKey;
+ this.revision = revision;
+ this.collectionClass = collectionClass;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public T initialize() {
+ EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg,
versionsReader);
+
+ List queryResult = queryGenerator.getQuery(versionsReader, primaryKey,
revision).list();
+
+ T result;
+ try {
+ result = collectionClass.newInstance();
+ } catch (Exception e) {
+ throw new VersionsException(e);
+ }
+
+ entityInstantiator.addInstancesFromVersionsEntities(entityName, result,
queryResult, revision);
+
+ return result;
+ }
+}
Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
===================================================================
--- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-08-21 10:19:09
UTC (rev 116)
+++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-08-21 11:18:53
UTC (rev 117)
@@ -44,7 +44,7 @@
PostCollectionRecreateEventListener, Initializable {
private VersionsConfiguration verCfg;
- private void generateCollectionChangeWorkUnits(VersionsSync verSync, EntityPersister
entityPersister,
+ private void generateBidirectionalCollectionChangeWorkUnits(VersionsSync verSync,
EntityPersister entityPersister,
String entityName, Object[] newState,
Object[] oldState) {
// Checking if this is enabled in configuration ...
if (!verCfg.getGlobalCfg().isGenerateRevisionsForCollections()) {
@@ -71,12 +71,12 @@
// (size increases).
if (newValue != null) {
Serializable id = (Serializable)
idMapper.mapToIdFromEntity(newValue);
- verSync.addWorkUnit(new
BidirectionalCollectionChangeWorkUnit(relDesc.getToEntityName(), verCfg, id, newValue));
+ verSync.addWorkUnit(new
CollectionChangeWorkUnit(relDesc.getToEntityName(), verCfg, id, newValue));
}
if (oldValue != null) {
Serializable id = (Serializable)
idMapper.mapToIdFromEntity(oldValue);
- verSync.addWorkUnit(new
BidirectionalCollectionChangeWorkUnit(relDesc.getToEntityName(), verCfg, id, oldValue));
+ verSync.addWorkUnit(new
CollectionChangeWorkUnit(relDesc.getToEntityName(), verCfg, id, oldValue));
}
}
}
@@ -92,7 +92,7 @@
verSync.addWorkUnit(new AddWorkUnit(event.getPersister().getEntityName(),
verCfg, event.getId(),
event.getPersister(), event.getState()));
- generateCollectionChangeWorkUnits(verSync, event.getPersister(), entityName,
event.getState(), null);
+ generateBidirectionalCollectionChangeWorkUnits(verSync, event.getPersister(),
entityName, event.getState(), null);
}
}
@@ -105,7 +105,7 @@
verSync.addWorkUnit(new ModWorkUnit(event.getPersister().getEntityName(),
verCfg, event.getId(),
event.getPersister(), event.getState(), event.getOldState()));
- generateCollectionChangeWorkUnits(verSync, event.getPersister(), entityName,
event.getState(), event.getOldState());
+ generateBidirectionalCollectionChangeWorkUnits(verSync, event.getPersister(),
entityName, event.getState(), event.getOldState());
}
}
@@ -117,7 +117,7 @@
verSync.addWorkUnit(new DelWorkUnit(event.getPersister().getEntityName(),
verCfg, event.getId()));
- generateCollectionChangeWorkUnits(verSync, event.getPersister(), entityName,
null, event.getDeletedState());
+ generateBidirectionalCollectionChangeWorkUnits(verSync, event.getPersister(),
entityName, null, event.getDeletedState());
}
}
@@ -128,8 +128,15 @@
if (verCfg.getEntCfg().isVersioned(entityName)) {
VersionsSync verSync = verCfg.getSyncManager().get(event.getSession());
- verSync.addWorkUnit(new PersistentCollectionChangeWorkUnit(entityName,
verCfg, newColl,
- collectionEntry.getRole(), oldColl,
event.getAffectedOwnerIdOrNull()));
+ VersionsWorkUnit workUnit = new
PersistentCollectionChangeWorkUnit(entityName, verCfg, newColl,
+ collectionEntry.getRole(), oldColl,
event.getAffectedOwnerIdOrNull());
+ verSync.addWorkUnit(workUnit);
+
+ if (workUnit.containsWork()) {
+ // There are some changes: a revision needs also be generated for the
collection owner
+ verSync.addWorkUnit(new
CollectionChangeWorkUnit(event.getAffectedOwnerEntityName(), verCfg,
+ event.getAffectedOwnerIdOrNull(),
event.getAffectedOwnerOrNull()));
+ }
}
}
Modified: trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
+++ trunk/src/main/org/jboss/envers/synchronization/work/AddWorkUnit.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -72,7 +72,7 @@
return KeepCheckResult.NONE;
}
- public KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second) {
+ public KeepCheckResult check(CollectionChangeWorkUnit second) {
return KeepCheckResult.FIRST;
}
Copied: trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
(from rev 115,
trunk/src/main/org/jboss/envers/synchronization/work/BidirectionalCollectionChangeWorkUnit.java)
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,78 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.synchronization.work;
+
+import org.hibernate.Session;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.RevisionType;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class CollectionChangeWorkUnit extends AbstractVersionsWorkUnit implements
VersionsWorkUnit {
+ private final Object entity;
+
+ public CollectionChangeWorkUnit(String entityName, VersionsConfiguration verCfg,
Serializable id, Object entity) {
+ super(entityName, verCfg, id);
+
+ this.entity = entity;
+ }
+
+ public boolean containsWork() {
+ return true;
+ }
+
+ public void perform(Session session, Number revision) {
+ Map<String, Object> data = new HashMap<String, Object>();
+ fillDataWithId(data, revision, RevisionType.MOD);
+
+
verCfg.getEntCfg().get(getEntityName()).getPropertyMapper().mapToMapFromEntity(data,
entity, null);
+
+ session.save(verCfg.getVerEntCfg().getVersionsEntityName(getEntityName()),
data);
+
+ setPerformed(data);
+ }
+
+ public KeepCheckResult check(AddWorkUnit second) {
+ return KeepCheckResult.SECOND;
+ }
+
+ public KeepCheckResult check(ModWorkUnit second) {
+ return KeepCheckResult.SECOND;
+ }
+
+ public KeepCheckResult check(DelWorkUnit second) {
+ return KeepCheckResult.SECOND;
+ }
+
+ public KeepCheckResult check(CollectionChangeWorkUnit second) {
+ return KeepCheckResult.FIRST;
+ }
+
+ public KeepCheckResult dispatch(KeepCheckVisitor first) {
+ return first.check(this);
+ }
+}
Modified: trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
+++ trunk/src/main/org/jboss/envers/synchronization/work/DelWorkUnit.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -62,7 +62,7 @@
return KeepCheckResult.FIRST;
}
- public KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second) {
+ public KeepCheckResult check(CollectionChangeWorkUnit second) {
return KeepCheckResult.FIRST;
}
Modified: trunk/src/main/org/jboss/envers/synchronization/work/KeepCheckVisitor.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/KeepCheckVisitor.java 2008-08-21
10:19:09 UTC (rev 116)
+++ trunk/src/main/org/jboss/envers/synchronization/work/KeepCheckVisitor.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -29,5 +29,5 @@
KeepCheckResult check(AddWorkUnit second);
KeepCheckResult check(ModWorkUnit second);
KeepCheckResult check(DelWorkUnit second);
- KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second);
+ KeepCheckResult check(CollectionChangeWorkUnit second);
}
Modified: trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
+++ trunk/src/main/org/jboss/envers/synchronization/work/ModWorkUnit.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -70,7 +70,7 @@
return KeepCheckResult.SECOND;
}
- public KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second) {
+ public KeepCheckResult check(CollectionChangeWorkUnit second) {
return KeepCheckResult.FIRST;
}
Modified:
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
+++
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -56,7 +56,7 @@
return null;
}
- public KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second) {
+ public KeepCheckResult check(CollectionChangeWorkUnit second) {
return null;
}
Added:
trunk/src/test/org/jboss/envers/test/entities/onetomany/unidirectional/SetRefCollEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/entities/onetomany/unidirectional/SetRefCollEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/entities/onetomany/unidirectional/SetRefCollEntity.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,85 @@
+package org.jboss.envers.test.entities.onetomany.unidirectional;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import java.util.Set;
+
+/**
+ * Set collection of references entity
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class SetRefCollEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @OneToMany
+ private Set<StrTestEntity> collection;
+
+ public SetRefCollEntity() {
+ }
+
+ public SetRefCollEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public SetRefCollEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Set<StrTestEntity> getCollection() {
+ return collection;
+ }
+
+ public void setCollection(Set<StrTestEntity> collection) {
+ this.collection = collection;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof SetRefCollEntity)) return false;
+
+ SetRefCollEntity that = (SetRefCollEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "SetRefEdEntity(id = " + id + ", data = " + data +
")";
+ }
+}
\ No newline at end of file
Added:
trunk/src/test/org/jboss/envers/test/integration/onetomany/unidirectional/BasicNotOwnedSet.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/onetomany/unidirectional/BasicNotOwnedSet.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/onetomany/unidirectional/BasicNotOwnedSet.java 2008-08-21
11:18:53 UTC (rev 117)
@@ -0,0 +1,123 @@
+package org.jboss.envers.test.integration.onetomany.unidirectional;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.unidirectional.SetRefCollEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicNotOwnedSet extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(SetRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<StrTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3,
4).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id));
+
+ assert
Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert
Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @SuppressWarnings({"ManualArrayToCollectionCopy"})
+ private <T> Set<T> makeSet(T... objects) {
+ Set<T> ret = new HashSet<T>();
+ for (T obj : objects) { ret.add(obj); }
+ return ret;
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class,
coll1_id, 1);
+ SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class,
coll1_id, 2);
+ SetRefCollEntity rev3 = getVersionsReader().find(SetRefCollEntity.class,
coll1_id, 3);
+ SetRefCollEntity rev4 = getVersionsReader().find(SetRefCollEntity.class,
coll1_id, 4);
+
+ assert rev1.getCollection().equals(makeSet(str1));
+ assert rev2.getCollection().equals(makeSet(str1, str2));
+ assert rev3.getCollection().equals(makeSet(str2));
+ assert rev4.getCollection().equals(makeSet());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+
+ }
+}
Modified: trunk/src/test/org/jboss/envers/test/working/WorkingEntity1.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/working/WorkingEntity1.java 2008-08-21 10:19:09
UTC (rev 116)
+++ trunk/src/test/org/jboss/envers/test/working/WorkingEntity1.java 2008-08-21 11:18:53
UTC (rev 117)
@@ -15,6 +15,7 @@
public class WorkingEntity1 {
@Id
@GeneratedValue
+ @Column(name = "id1")
private Integer id1;
@Versioned
Modified: trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java 2008-08-21 10:19:09
UTC (rev 116)
+++ trunk/src/test/org/jboss/envers/test/working/WorkingEntity2.java 2008-08-21 11:18:53
UTC (rev 117)
@@ -22,7 +22,7 @@
private String str2;
@Versioned
- @OneToMany //(mappedBy="we2")
+ @OneToMany //(mappedBy="we2")
//@ManyToMany
//@Transient
private Set<WorkingEntity1> we1s;