Author: adamw
Date: 2008-08-21 06:19:09 -0400 (Thu, 21 Aug 2008)
New Revision: 116
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.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
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyInitializor.java
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/EntityInstantiator.java
trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/SubclassPropertyMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/id/QueryParameterData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneIdMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/ToOneIdMapper.java
trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java
trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java
trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
trunk/src/main/org/jboss/envers/reader/VersionsReaderImplementor.java
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java
trunk/src/main/org/jboss/envers/tools/StringTools.java
trunk/src/test/org/jboss/envers/test/working/WorkingMain.java
Log:
ENVERS-26: retrieving versions of unidirectional one-to-many associations
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -37,7 +37,7 @@
import org.jboss.envers.entities.mapper.id.*;
import org.jboss.envers.entities.mapper.relation.ToOneIdMapper;
import org.jboss.envers.entities.mapper.relation.OneToOneIdMapper;
-import org.jboss.envers.entities.mapper.relation.OneToManyIdMapper;
+import org.jboss.envers.entities.mapper.relation.OneToManyBidirectionalMapper;
import org.jboss.envers.entities.mapper.relation.OneToManyUnidirectionalMapper;
import org.jboss.envers.tools.StringTools;
import org.jboss.envers.tools.Tools;
@@ -315,7 +315,7 @@
owningEntityName, ownedIdMapper);
// Adding mapper for the id
- mapper.addComposite(propertyName, new
OneToManyIdMapper(owningReferencePropertyName, owningEntityName,
+ mapper.addComposite(propertyName, new
OneToManyBidirectionalMapper(owningReferencePropertyName, owningEntityName,
propertyName));
}
@@ -340,7 +340,7 @@
IdMappingData referencingIdMapping = configuration.getIdMappingData();
IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
- String referencingPrefix = entityName + "_";
+ String referencingPrefix = StringTools.getLastComponent(entityName) +
"_";
String referencedPrefix = property.getName() + "_";
// Name of the entity that will be used to store the relation between the two
entities.
@@ -374,10 +374,11 @@
addRevisionType(middleEntity);
// Adding the property mapper
- mapper.addComposite(property.getName(), new
OneToManyUnidirectionalMapper(verEntCfg, property.getName(),
- referencedEntityName, versionsMiddleEntityName,
-
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
-
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix)));
+ mapper.addComposite(property.getName(), new
OneToManyUnidirectionalMapper(verEntCfg, entityName,
+ referencedEntityName, property.getName(),
verEntCfg.getVersionsEntityName(referencedEntityName),
+ versionsMiddleEntityName,
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
+
referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix),
+ entitiesConfigurations.get(referencedEntityName).getIdMapper()));
// TODO: ???
}
Modified: trunk/src/main/org/jboss/envers/entities/EntityInstantiator.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/EntityInstantiator.java 2008-08-20 11:44:45
UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/EntityInstantiator.java 2008-08-21 10:19:09
UTC (rev 116)
@@ -29,6 +29,7 @@
import java.util.Map;
import java.util.List;
+import java.util.Collection;
/**
* @author Adam Warski (adam at warski dot org)
@@ -75,7 +76,7 @@
Object primaryKey = idMapper.mapToIdFromMap(originalId);
- verCfg.getEntCfg().get(entityName).getPropertyMapper().mapToEntityFromMap(ret,
versionsEntity, primaryKey,
+ verCfg.getEntCfg().get(entityName).getPropertyMapper().mapToEntityFromMap(verCfg,
ret, versionsEntity, primaryKey,
versionsReader, revision);
idMapper.mapToEntityFromMap(ret, originalId);
@@ -83,9 +84,9 @@
}
@SuppressWarnings({"unchecked"})
- public void addInstancesFromVersionsEntities(String entityName, List list,
List<Map> versionsEntities, Number revision) {
+ public void addInstancesFromVersionsEntities(String entityName, Collection addTo,
List<Map> versionsEntities, Number revision) {
for (Map versionsEntity : versionsEntities) {
- list.add(createInstanceFromVersionsEntity(entityName, versionsEntity,
revision));
+ addTo.add(createInstanceFromVersionsEntity(entityName, versionsEntity,
revision));
}
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -22,6 +22,7 @@
package org.jboss.envers.entities.mapper;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.exception.VersionsException;
@@ -66,7 +67,7 @@
return delegate.mapToMapFromEntity(newData, newObj, oldObj);
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
if (data == null || obj == null) {
return;
}
@@ -77,17 +78,17 @@
try {
Object subObj =
ReflectHelper.getDefaultConstructor(getter.getReturnType()).newInstance();
setter.set(obj, subObj, null);
- delegate.mapToEntityFromMap(subObj, (Map) data.get(propertyName), primaryKey,
versionsReader, revision);
+ delegate.mapToEntityFromMap(verCfg, subObj, (Map) data.get(propertyName),
primaryKey, versionsReader, revision);
} catch (Exception e) {
throw new VersionsException(e);
}
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
- return delegate.mapCollectionChangesFromCollections(referencingPropertyName,
newColl, oldColl, id);
+ return delegate.mapCollectionChanges(referencingPropertyName, newColl, oldColl,
id);
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -25,6 +25,7 @@
import org.hibernate.MappingException;
import org.hibernate.collection.PersistentCollection;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
@@ -101,19 +102,19 @@
return ret;
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
for (String propertyName : properties.keySet()) {
- properties.get(propertyName).mapToEntityFromMap(obj, data, primaryKey,
versionsReader, revision);
+ properties.get(propertyName).mapToEntityFromMap(verCfg, obj, data,
primaryKey, versionsReader, revision);
}
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
PropertyMapper mapper = properties.get(referencingPropertyName);
if (mapper != null) {
- return mapper.mapCollectionChangesFromCollections(referencingPropertyName,
newColl, oldColl, id);
+ return mapper.mapCollectionChanges(referencingPropertyName, newColl, oldColl,
id);
} else {
return null;
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -22,6 +22,7 @@
package org.jboss.envers.entities.mapper;
import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.collection.PersistentCollection;
import java.util.Map;
@@ -44,14 +45,15 @@
/**
* Maps properties from the given map to the given object.
+ * @param verCfg Versions configuration.
* @param obj Object to map to.
* @param data Data to map from.
* @param primaryKey Primary key of the object to which we map (for relations)
* @param versionsReader VersionsReader for reading relations
* @param revision Revision at which the obejct is read, for reading relations
*/
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader,
- Number revision);
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
+ VersionsReaderImplementor versionsReader, Number
revision);
/**
* Maps collection changes
@@ -61,8 +63,7 @@
* @param id Id of the object owning the collection.
* @return List of changes that need to be performed on the persistent store.
*/
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
-
PersistentCollection newColl,
-
Serializable oldColl,
-
Serializable id);
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
+ PersistentCollection
newColl,
+ Serializable
oldColl, Serializable id);
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -22,6 +22,7 @@
package org.jboss.envers.entities.mapper;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.tools.Tools;
@@ -60,7 +61,7 @@
return !Tools.objectsEqual(newObj, oldObj);
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
if (data == null || obj == null) {
return;
}
@@ -69,7 +70,7 @@
setter.set(obj, data.get(propertyName), null);
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
Modified: trunk/src/main/org/jboss/envers/entities/mapper/SubclassPropertyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/SubclassPropertyMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/SubclassPropertyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -23,6 +23,7 @@
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.collection.PersistentCollection;
import java.util.Map;
@@ -57,20 +58,20 @@
return parentDiffs || mainDiffs;
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
- parentMapper.mapToEntityFromMap(obj, data, primaryKey, versionsReader,
revision);
- main.mapToEntityFromMap(obj, data, primaryKey, versionsReader, revision);
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
+ parentMapper.mapToEntityFromMap(verCfg, obj, data, primaryKey, versionsReader,
revision);
+ main.mapToEntityFromMap(verCfg, obj, data, primaryKey, versionsReader,
revision);
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
- List<PersistentCollectionChangeData> collectionChanges =
parentMapper.mapCollectionChangesFromCollections(
+ List<PersistentCollectionChangeData> collectionChanges =
parentMapper.mapCollectionChanges(
referencingPropertyName, newColl, oldColl, id);
if (collectionChanges == null) {
- return main.mapCollectionChangesFromCollections(referencingPropertyName,
newColl, oldColl, id);
+ return main.mapCollectionChanges(referencingPropertyName, newColl, oldColl,
id);
} else {
return collectionChanges;
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -26,6 +26,7 @@
import org.hibernate.criterion.Conjunction;
import java.util.Set;
+import java.util.Iterator;
/**
* @author Adam Warski (adam at warski dot org)
@@ -50,6 +51,71 @@
}
}
+ public String getIdsEqualQuery(String prefix1, String prefix2) {
+ Set<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+
+ StringBuilder query = new StringBuilder();
+
+ Iterator<QueryParameterData> paramDataIter = paramDatas.iterator();
+
+ while (paramDataIter.hasNext()) {
+ QueryParameterData paramData = paramDataIter.next();
+ query.append(paramData.getProperty(prefix1)).append(" =
").append(paramData.getProperty(prefix2));
+ if (paramDataIter.hasNext()) {
+ query.append(" AND ");
+ }
+ }
+
+ return query.toString();
+ }
+
+ public String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2) {
+ Set<QueryParameterData> paramDatas1 = mapToQueryParametersFromId(null);
+ Set<QueryParameterData> paramDatas2 =
mapper2.mapToQueryParametersFromId(null);
+
+ StringBuilder query = new StringBuilder();
+
+ Iterator<QueryParameterData> paramDataIter1 = paramDatas1.iterator();
+ Iterator<QueryParameterData> paramDataIter2 = paramDatas2.iterator();
+ while (paramDataIter1.hasNext()) {
+ QueryParameterData paramData1 = paramDataIter1.next();
+ QueryParameterData paramData2 = paramDataIter2.next();
+ query.append(paramData1.getProperty(prefix1)).append(" =
").append(paramData2.getProperty(prefix2));
+
+ if (paramDataIter1.hasNext()) {
+ query.append(" AND ");
+ }
+ }
+
+ return query.toString();
+ }
+
+ public String getIdEqualsQuery(String prefix, boolean equals) {
+ Set<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+
+ StringBuilder query = new StringBuilder();
+
+ Iterator<QueryParameterData> paramDataIter = paramDatas.iterator();
+
+ while (paramDataIter.hasNext()) {
+ QueryParameterData paramData = paramDataIter.next();
+
+ query.append(paramData.getProperty(prefix));
+ if (equals) {
+ query.append(" = ");
+ } else {
+ query.append(" != ");
+ }
+ query.append(":").append(paramData.getQueryParameterName());
+
+ if (paramDataIter.hasNext()) {
+ query.append(" AND ");
+ }
+ }
+
+ return query.toString();
+ }
+
public Criterion getIdEqualsCriterion(Object id, String prefix, boolean equals) {
Set<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-08-20 11:44:45
UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-08-21 10:19:09
UTC (rev 116)
@@ -65,6 +65,15 @@
public Criterion getIdsEqualCriterion(String prefix1, String prefix2);
/**
+ * Gets a query string, which contains equalities, which express the property that
the id of the entity
+ * with alias prefix1, is equal to the id of the entity with alias prefix2 (the
entity is the same).
+ * @param prefix1 First alias of the entity.
+ * @param prefix2 Second alias of the entity.
+ * @return A query string expressing the property described above.
+ */
+ public String getIdsEqualQuery(String prefix1, String prefix2);
+
+ /**
* Gets a criteria object, which contains restrictions, which express the property
that the id of the entity
* with alias prefix, is equal to the given object.
* @param id Value of id.
@@ -73,4 +82,26 @@
* @return A criterion object expressing the property described above.
*/
public Criterion getIdEqualsCriterion(Object id, String prefix, boolean equals);
+
+ /**
+ * Gets a query string, which contains equalities, which express the property that
the id of the entity
+ * with alias prefix, is equal to some values (which are left to be specified later,
by specifiying parameter
+ * values)
+ * @param prefix Alias of the entity (may be null).
+ * @param equals Should this query express the "=" relation or the
"<>" relation.
+ * @return A query string expressing the property described above.
+ */
+ public String getIdEqualsQuery(String prefix, boolean equals);
+
+ /**
+ * Gets a query string, which contains equalities, which express the property that
the id of the entity
+ * with alias prefix1, is equal to the id of the entity with alias prefix2, mapped by
the second mapper
+ * (the second mapper must be for the same entity, but it can have, for example,
prefixed properties).
+ * @param prefix1 First alias of the entity.
+ * @param mapper2 Second mapper for the same entity, which will be used to get
properties for the right side
+ * of the equation.
+ * @param prefix2 Second alias of the entity.
+ * @return A query string expressing the property described above.
+ */
+ public String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2);
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/QueryParameterData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/QueryParameterData.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/QueryParameterData.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -21,6 +21,8 @@
*/
package org.jboss.envers.entities.mapper.id;
+import org.hibernate.Query;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
@@ -44,4 +46,28 @@
public Object getValue() {
return value;
}
+
+ public void setParameterValue(Query query) {
+ query.setParameter(flatEntityPropertyName, value);
+ }
+
+ public String getQueryParameterName() {
+ return flatEntityPropertyName;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof QueryParameterData)) return false;
+
+ QueryParameterData that = (QueryParameterData) o;
+
+ if (flatEntityPropertyName != null ?
!flatEntityPropertyName.equals(that.flatEntityPropertyName) : that.flatEntityPropertyName
!= null)
+ return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (flatEntityPropertyName != null ? flatEntityPropertyName.hashCode() : 0);
+ }
}
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -0,0 +1,58 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
+import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor;
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.hibernate.property.Getter;
+import org.hibernate.property.Setter;
+
+import java.util.*;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public abstract class AbstractOneToManyMapper implements PropertyMapper {
+ private final String owningEntityName;
+ private final String propertyName;
+
+ protected AbstractOneToManyMapper(String owningEntityName, String propertyName) {
+ this.owningEntityName = owningEntityName;
+ this.propertyName = propertyName;
+ }
+
+ protected abstract <T extends Collection> Initializor<T>
getInitializator(VersionsConfiguration verCfg,
+
VersionsReaderImplementor versionsReader,
+
Class<?> entityClass, Object primaryKey,
+ Number
revision, Class<T> collectionClass);
+
+ @SuppressWarnings({"unchecked"})
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, 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(getInitializator(verCfg, versionsReader, entityClass,
primaryKey, revision, ArrayList.class));
+ } else if (Set.class.isAssignableFrom(collectionClass) ||
Collection.class.isAssignableFrom(collectionClass)) {
+ value = new SetProxy(getInitializator(verCfg, versionsReader, entityClass,
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);
+ }
+}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyBidirectionalMapper.java
(from rev 115,
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyIdMapper.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
10:19:09 UTC (rev 116)
@@ -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
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyUnidirectionalMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -2,9 +2,12 @@
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;
@@ -14,24 +17,34 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class OneToManyUnidirectionalMapper implements PropertyMapper {
+public class OneToManyUnidirectionalMapper extends AbstractOneToManyMapper implements
PropertyMapper {
private final VersionsEntitiesConfiguration verEntCfg;
+ private final String referencedEntityName;
private final String collectionReferencingPropertyName;
- private final String referencedEntityName;
+ private final String versionsReferencedEntityName;
private final String versionsMiddleEntityName;
private final IdMapper referencingMiddleIdMapper;
private final IdMapper referencedMiddleIdMapper;
- public OneToManyUnidirectionalMapper(VersionsEntitiesConfiguration verEntCfg,
- String collectionReferencingPropertyName,
- String referencedEntityName, String
versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, 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.referencedEntityName = referencedEntityName;
+ 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) {
@@ -60,10 +73,9 @@
}
@SuppressWarnings({"unchecked"})
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
-
PersistentCollection newColl,
-
Serializable oldColl,
-
Serializable id) {
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
+ PersistentCollection
newColl,
+ Serializable
oldColl, Serializable id) {
if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
return null;
}
@@ -90,10 +102,15 @@
@SuppressWarnings({"unchecked"})
public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
+ // Changes are mapped in the "mapCollectionChanges" method.
return false;
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
- System.out.println("XXX");
+ 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);
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneIdMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneIdMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneIdMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -27,6 +27,7 @@
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.query.VersionsRestrictions;
import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.property.Setter;
import org.hibernate.NonUniqueResultException;
import org.hibernate.collection.PersistentCollection;
@@ -54,7 +55,7 @@
return false;
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
VersionsReaderImplementor versionsReader, Number revision) {
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
if (obj == null) {
return;
}
@@ -77,7 +78,7 @@
setter.set(obj, value, null);
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/ToOneIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/ToOneIdMapper.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ToOneIdMapper.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -28,6 +28,7 @@
import org.jboss.envers.tools.reflection.ReflectionTools;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.entities.mapper.relation.lazy.ToOneDelegateSessionImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.property.Setter;
import org.hibernate.collection.PersistentCollection;
@@ -59,7 +60,7 @@
return !Tools.objectsEqual(newObj, oldObj);
}
- public void mapToEntityFromMap(Object obj, Map data, Object primaryKey,
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
VersionsReaderImplementor versionsReader, Number
revision) {
if (obj == null) {
return;
@@ -80,7 +81,7 @@
setter.set(obj, value, null);
}
- public List<PersistentCollectionChangeData>
mapCollectionChangesFromCollections(String referencingPropertyName,
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl,
Serializable id) {
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
10:19:09 UTC (rev 116)
@@ -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;
+ }
+}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyBidirectionalInitializor.java
(from rev 113,
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyInitializor.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
10:19:09 UTC (rev 116)
@@ -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/OneToManyInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyInitializor.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyInitializor.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -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 OneToManyInitializor<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 OneToManyInitializor(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/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
10:19:09 UTC (rev 116)
@@ -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/query/VersionsQueryCreator.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-08-20 11:44:45
UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-08-21 10:19:09
UTC (rev 116)
@@ -26,19 +26,16 @@
import org.jboss.envers.query.impl.RevisionsOfEntityQuery;
import static org.jboss.envers.tools.ArgumentsTools.*;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.hibernate.Session;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class VersionsQueryCreator {
private final VersionsConfiguration verCfg;
- private final Session session;
private final VersionsReaderImplementor versionsReaderImplementor;
- public VersionsQueryCreator(VersionsConfiguration verCfg, Session session,
VersionsReaderImplementor versionsReaderImplementor) {
+ public VersionsQueryCreator(VersionsConfiguration verCfg, VersionsReaderImplementor
versionsReaderImplementor) {
this.verCfg = verCfg;
- this.session = session;
this.versionsReaderImplementor = versionsReaderImplementor;
}
@@ -54,7 +51,7 @@
public VersionsQuery forEntitiesAtRevision(Class<?> c, Number revision) {
checkNotNull(revision, "Entity revision");
checkPositive(revision, "Entity revision");
- return new EntitiesAtRevisionQuery(verCfg, session, versionsReaderImplementor, c,
revision);
+ return new EntitiesAtRevisionQuery(verCfg, versionsReaderImplementor, c,
revision);
}
/**
@@ -73,6 +70,6 @@
* unless an order or projection is added.
*/
public VersionsQuery forRevisionsOfEntity(Class<?> c, boolean
selectEntitiesOnly, boolean selectDeletedEntities) {
- return new RevisionsOfEntityQuery(verCfg, session, versionsReaderImplementor, c,
selectEntitiesOnly,selectDeletedEntities);
+ return new RevisionsOfEntityQuery(verCfg, versionsReaderImplementor, c,
selectEntitiesOnly,selectDeletedEntities);
}
}
Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -55,8 +55,8 @@
protected final VersionsConfiguration verCfg;
- protected AbstractVersionsQuery(VersionsConfiguration verCfg, Session session,
- VersionsReaderImplementor versionsReader,
Class<?> cls) {
+ protected AbstractVersionsQuery(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
+ Class<?> cls) {
this.verCfg = verCfg;
criterions = new ArrayList<VersionsCriterion>();
@@ -64,7 +64,7 @@
entityName = cls.getName();
versionsEntityName = verCfg.getVerEntCfg().getVersionsEntityName(entityName);
- versionsCriteria = session.createCriteria(versionsEntityName, "e");
+ versionsCriteria = versionsReader.getSession().createCriteria(versionsEntityName,
"e");
}
public abstract List list() throws VersionsException;
Modified: trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -27,7 +27,6 @@
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.criterion.*;
-import org.hibernate.Session;
import java.util.List;
import java.util.ArrayList;
@@ -38,10 +37,10 @@
public class EntitiesAtRevisionQuery extends AbstractVersionsQuery {
private final Number revision;
- public EntitiesAtRevisionQuery(VersionsConfiguration verCfg, Session session,
+ public EntitiesAtRevisionQuery(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
Class<?> cls,
Number revision) {
- super(verCfg, session, versionsReader, cls);
+ super(verCfg, versionsReader, cls);
this.revision = revision;
}
Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java
===================================================================
--- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -29,7 +29,6 @@
import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
-import org.hibernate.Session;
import java.util.List;
import java.util.ArrayList;
@@ -42,11 +41,11 @@
private final boolean selectEntitiesOnly;
private final boolean selectDeletedEntities;
- public RevisionsOfEntityQuery(VersionsConfiguration verCfg, Session session,
+ public RevisionsOfEntityQuery(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
Class<?> cls, boolean selectEntitiesOnly,
boolean selectDeletedEntities) {
- super(verCfg, session, versionsReader, cls);
+ super(verCfg, versionsReader, cls);
this.selectEntitiesOnly = selectEntitiesOnly;
this.selectDeletedEntities = selectDeletedEntities;
Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java
===================================================================
--- trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-20 11:44:45 UTC
(rev 115)
+++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-21 10:19:09 UTC
(rev 116)
@@ -64,6 +64,10 @@
return sessionImplementor;
}
+ public Session getSession() {
+ return session;
+ }
+
@SuppressWarnings({"unchecked"})
public <T> T find(Class<T> cls, Object primaryKey, Number revision)
throws
IllegalArgumentException, NotVersionedException, IllegalStateException {
@@ -170,6 +174,6 @@
}
public VersionsQueryCreator createQuery() {
- return new VersionsQueryCreator(verCfg, session, this);
+ return new VersionsQueryCreator(verCfg, this);
}
}
Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImplementor.java
===================================================================
--- trunk/src/main/org/jboss/envers/reader/VersionsReaderImplementor.java 2008-08-20
11:44:45 UTC (rev 115)
+++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImplementor.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -22,6 +22,7 @@
package org.jboss.envers.reader;
import org.hibernate.engine.SessionImplementor;
+import org.hibernate.Session;
import org.jboss.envers.VersionsReader;
/**
@@ -30,4 +31,5 @@
*/
public interface VersionsReaderImplementor extends VersionsReader {
SessionImplementor getSessionImplementor();
+ Session getSession();
}
Deleted:
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/synchronization/work/CollectionChangeWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -1,78 +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.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/PersistentCollectionChangeWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-08-20
11:44:45 UTC (rev 115)
+++
trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-08-21
10:19:09 UTC (rev 116)
@@ -24,7 +24,7 @@
String referencingPropertyName = role.substring(entityName.length() + 1);
collectionChanges = verCfg.getEntCfg().get(getEntityName()).getPropertyMapper()
- .mapCollectionChangesFromCollections(referencingPropertyName, collection,
snapshot, id);
+ .mapCollectionChanges(referencingPropertyName, collection, snapshot,
id);
}
public boolean containsWork() {
Modified: trunk/src/main/org/jboss/envers/tools/StringTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/StringTools.java 2008-08-20 11:44:45 UTC (rev
115)
+++ trunk/src/main/org/jboss/envers/tools/StringTools.java 2008-08-21 10:19:09 UTC (rev
116)
@@ -28,4 +28,17 @@
public static boolean isEmpty(String s) {
return s == null || "".equals(s);
}
+
+ public static String getLastComponent(String s) {
+ if (s == null) {
+ return null;
+ }
+
+ int lastDot = s.lastIndexOf(".");
+ if (lastDot == -1) {
+ return s;
+ } else {
+ return s.substring(lastDot + 1);
+ }
+ }
}
Modified: trunk/src/test/org/jboss/envers/test/working/WorkingMain.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/working/WorkingMain.java 2008-08-20 11:44:45 UTC
(rev 115)
+++ trunk/src/test/org/jboss/envers/test/working/WorkingMain.java 2008-08-21 10:19:09 UTC
(rev 116)
@@ -116,6 +116,12 @@
entityManager.getTransaction().begin();
VersionsReader vr = VersionsReaderFactory.get(entityManager);
+ System.out.println("1 " + vr.find(WorkingEntity2.class, we2.getId2(),
1).getWe1s());
+ System.out.println("2 " + vr.find(WorkingEntity2.class, we2.getId2(),
2).getWe1s());
+ System.out.println("3 " + vr.find(WorkingEntity2.class, we2.getId2(),
3).getWe1s());
+ System.out.println("4 " + vr.find(WorkingEntity2.class, we2.getId2(),
4).getWe1s());
+ System.out.println("5 " + vr.find(WorkingEntity2.class, we2.getId2(),
5).getWe1s());
+ System.out.println("6 " + vr.find(WorkingEntity2.class, we2.getId2(),
6).getWe1s());
entityManager.getTransaction().commit();
}