Author: adamw
Date: 2008-09-23 13:05:36 -0400 (Tue, 23 Sep 2008)
New Revision: 165
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java
trunk/src/test/org/jboss/envers/test/entities/components/
trunk/src/test/org/jboss/envers/test/entities/components/Component1.java
trunk/src/test/org/jboss/envers/test/entities/components/Component2.java
trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java
Modified:
trunk/resources/test/testng.xml
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
trunk/src/test/org/jboss/envers/test/integration/components/Components.java
Log:
ENVERS-25: support for @MapKey
Modified: trunk/resources/test/testng.xml
===================================================================
--- trunk/resources/test/testng.xml 2008-09-22 21:20:21 UTC (rev 164)
+++ trunk/resources/test/testng.xml 2008-09-23 17:05:36 UTC (rev 165)
@@ -4,7 +4,8 @@
<test name="All">
<packages>
<package name="org.jboss.envers.test.integration.basic" />
- <package name="org.jboss.envers.test.integration.collection"
/>
+ <package name="org.jboss.envers.test.integration.collection"
/>
+ <package
name="org.jboss.envers.test.integration.collection.mapkey" />
<package name="org.jboss.envers.test.integration.components"
/>
<package name="org.jboss.envers.test.integration.customtype"
/>
<package name="org.jboss.envers.test.integration.data" />
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -22,6 +22,7 @@
package org.jboss.envers.configuration.metadata;
import javax.persistence.Version;
+import javax.persistence.MapKey;
import org.jboss.envers.configuration.GlobalConfiguration;
import org.jboss.envers.tools.reflection.YClass;
@@ -65,6 +66,13 @@
}
}
+ private void addPropertyMapKey(YProperty property) {
+ MapKey mapKey = property.getAnnotation(MapKey.class);
+ if (mapKey != null) {
+ versioningData.mapKeys.put(property.getName(), mapKey.name());
+ }
+ }
+
private void addPropertyUnversioned(YProperty property) {
// check if a property is declared as unversioned to exclude it
// useful if a class is versioned but some properties should be excluded
@@ -95,6 +103,7 @@
addPropertyVersioned(property);
addPropertyUnversioned(property);
addPropertyJoinTables(property);
+ addPropertyMapKey(property);
}
}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -8,9 +8,7 @@
import org.jboss.envers.entities.mapper.relation.*;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.*;
import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
-import org.jboss.envers.entities.mapper.relation.component.MiddleRelatedComponentMapper;
-import org.jboss.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper;
-import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper;
+import org.jboss.envers.entities.mapper.relation.component.*;
import org.jboss.envers.entities.mapper.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
@@ -39,6 +37,7 @@
private final String referencingEntityName;
private final EntityXmlMappingData xmlMappingData;
private final VersionsJoinTable joinTable;
+ private final String mapKey;
private final EntityConfiguration referencingEntityConfiguration;
/**
@@ -56,11 +55,13 @@
* @param xmlMappingData In case this collection requires a middle table, additional
mapping documents will
* be created using this object.
* @param joinTable User data for the middle (join) table.
<code>null</code> if the user didn't specify it.
+ * @param mapKey The value of the name() property of the MapKey annotation on this
property. Null, if this
+ * property isn't annotated with this annotation.
*/
public CollectionMetadataGenerator(VersionsMetadataGenerator mainGenerator, String
propertyName,
Collection propertyValue, CompositeMapperBuilder
currentMapper,
String referencingEntityName, EntityXmlMappingData
xmlMappingData,
- VersionsJoinTable joinTable) {
+ VersionsJoinTable joinTable, String mapKey) {
this.mainGenerator = mainGenerator;
this.propertyName = propertyName;
this.propertyValue = propertyValue;
@@ -68,6 +69,7 @@
this.referencingEntityName = referencingEntityName;
this.xmlMappingData = xmlMappingData;
this.joinTable = joinTable == null ? getDefaultVersionsJoinTable() : joinTable;
+ this.mapKey = mapKey;
referencingEntityConfiguration =
mainGenerator.getEntitiesConfigurations().get(referencingEntityName);
if (referencingEntityConfiguration == null) {
@@ -226,19 +228,9 @@
queryGeneratorBuilder, referencedPrefix,
joinTable.inverseJoinColumns());
// ******
- // Optionally, generating the index mapping.
+ // Generating the index mapping, if an index exists.
// ******
- MiddleComponentData indexComponentData;
- if (propertyValue instanceof IndexedCollection) {
- IndexedCollection indexedValue = (IndexedCollection) propertyValue;
- indexComponentData = addValueToMiddleTable(indexedValue.getIndex(),
middleEntityXml,
- queryGeneratorBuilder, "mapkey", null);
- // TODO: @MapKey support, @MapKeyManyToMany
- } else {
- // No index - creating a dummy mapper.
- indexComponentData = new MiddleComponentData(new
MiddleDummyComponentMapper(),
- queryGeneratorBuilder.getCurrentIndex());
- }
+ MiddleComponentData indexComponentData = addIndex(middleEntityXml,
queryGeneratorBuilder);
// ******
// Generating the property mapper.
@@ -259,6 +251,34 @@
storeMiddleEntityRelationInformation(mappedBy);
}
+ private MiddleComponentData addIndex(Element middleEntityXml, QueryGeneratorBuilder
queryGeneratorBuilder) {
+ if (propertyValue instanceof IndexedCollection) {
+ IndexedCollection indexedValue = (IndexedCollection) propertyValue;
+ if (mapKey == null) {
+ // This entity doesn't specify a javax.persistence.MapKey. Mapping it
to the middle entity.
+ return addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml,
+ queryGeneratorBuilder, "mapkey", null);
+ } else {
+ IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations()
+ .get(referencedEntityName).getIdMappingData();
+ if ("".equals(mapKey)) {
+ // The key of the map is the id of the entity.
+ return new MiddleComponentData(new
MiddleMapKeyIdComponentMapper(mainGenerator.getVerEntCfg(),
+ referencedIdMapping.getIdMapper()),
queryGeneratorBuilder.getCurrentIndex());
+ } else {
+ // The key of the map is a property of the entity.
+ return new MiddleComponentData(new
MiddleMapKeyPropertyComponentMapper(mapKey),
+ queryGeneratorBuilder.getCurrentIndex());
+ }
+ }
+ // TODO: @MapKeyManyToMany
+ } else {
+ // No index - creating a dummy mapper.
+ return new MiddleComponentData(new MiddleDummyComponentMapper(),
+ queryGeneratorBuilder.getCurrentIndex());
+ }
+ }
+
/**
*
* @param value Value, which should be mapped to the middle-table, either as a
relation to another entity,
@@ -279,8 +299,8 @@
String prefixRelated = prefix + "_";
String referencedEntityName = getReferencedEntityName(value);
- IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations().get(
- referencedEntityName).getIdMappingData();
+ IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations()
+ .get(referencedEntityName).getIdMappingData();
// Adding related-entity (in this case: the referenced entities id) id
mapping to the xml only if the
// relation isn't inverse (so when <code>xmlMapping</code> is
not null).
@@ -305,8 +325,8 @@
return new MiddleComponentData(new
MiddleRelatedComponentMapper(referencedIdData),
queryGeneratorBuilder.getCurrentIndex());
} else {
- boolean mapped =
mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null,
- ModificationStore.FULL, referencingEntityName, true);
+ boolean mapped =
mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(xmlMapping, prefix, value,
null,
+ ModificationStore.FULL, true);
if (mapped) {
// Simple values are always stored in the first item of the array
returned by the query generator.
@@ -314,7 +334,7 @@
} else {
mainGenerator.throwUnsupportedTypeException(type, referencingEntityName,
propertyName);
// Impossible to get here.
- throw new AssertionError();
+ throw new AssertionError();
}
}
}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -40,6 +40,7 @@
secondaryTableDictionary = new HashMap<String, String>();
unversionedProperties = new ArrayList<String>();
versionsJoinTables = new HashMap<String, VersionsJoinTable>();
+ mapKeys = new HashMap<String, String>();
}
public PropertyStoreInfo propertyStoreInfo;
@@ -50,6 +51,11 @@
* A map from property names to custom join tables definitions.
*/
public Map<String, VersionsJoinTable> versionsJoinTables;
+ /**
+ * A map from property names to the value of the related property names in a map key
annotation. An empty string,
+ * if the property name is not specified in the mapkey annotation.
+ */
+ public Map<String, String> mapKeys;
public boolean isVersioned() {
if (propertyStoreInfo.propertyStores.size() > 0) { return true; }
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -119,7 +119,7 @@
@SuppressWarnings({"unchecked"})
void addValue(Element parent, String name, Value value, CompositeMapperBuilder
currentMapper,
ModificationStore store, String entityName, EntityXmlMappingData
xmlMappingData,
- VersionsJoinTable joinTable, boolean firstPass) {
+ VersionsJoinTable joinTable, String mapKey, boolean firstPass) {
Type type = value.getType();
// only first pass
@@ -144,7 +144,7 @@
// only second pass
if (!firstPass) {
CollectionMetadataGenerator collectionMetadataGenerator = new
CollectionMetadataGenerator(this,
- name, (Collection) value, currentMapper, entityName,
xmlMappingData, joinTable);
+ name, (Collection) value, currentMapper, entityName,
xmlMappingData, joinTable, mapKey);
collectionMetadataGenerator.addCollection();
}
} else {
@@ -168,7 +168,8 @@
if (store != null) {
addValue(parent, property.getName(), property.getValue(),
currentMapper, store, entityName,
- xmlMappingData,
versioningData.versionsJoinTables.get(property.getName()), firstPass);
+ xmlMappingData,
versioningData.versionsJoinTables.get(property.getName()),
+ versioningData.mapKeys.get(property.getName()), firstPass);
}
}
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -10,14 +10,16 @@
*/
public interface MiddleComponentMapper {
/**
- * Maps from full object data, contained in the given map, to an object.
+ * Maps from full object data, contained in the given map (or object representation
of the map, if
+ * available), to an object.
* @param entityInstantiator An entity instatiator bound with an open versions
reader.
* @param data Full object data.
+ * @param dataObject An optional object representation of the data.
* @param revision Revision at which the data is read.
* @return An object with data corresponding to the one found in the given map.
*/
Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map<String,
Object> data,
- Number revision);
+ Object dataObject, Number revision);
/**
* Maps from an object to the object's map representation (for an entity - only
its id).
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -9,7 +9,8 @@
* @author Adam Warski (adam at warski dot org)
*/
public final class MiddleDummyComponentMapper implements MiddleComponentMapper {
- public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data, Number revision) {
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Object dataObject, Number revision) {
return null;
}
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java
(from rev 154,
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,37 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.tools.query.Parameters;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+
+import java.util.Map;
+
+/**
+ * A component mapper for the @MapKey mapping: the value of the map's key is the id
of the entity. This
+ * doesn't have an effect on the data stored in the versions tables, so
<code>mapToMapFromObject</code> is
+ * empty.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleMapKeyIdComponentMapper implements MiddleComponentMapper {
+ private final VersionsEntitiesConfiguration verEntCfg;
+ private final IdMapper relatedIdMapper;
+
+ public MiddleMapKeyIdComponentMapper(VersionsEntitiesConfiguration verEntCfg,
IdMapper relatedIdMapper) {
+ this.verEntCfg = verEntCfg;
+ this.relatedIdMapper = relatedIdMapper;
+ }
+
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Object dataObject, Number revision) {
+ return relatedIdMapper.mapToIdFromMap((Map)
data.get(verEntCfg.getOriginalIdPropName()));
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ // Doing nothing.
+ }
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ // Doing nothing.
+ }
+}
\ No newline at end of file
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,35 @@
+package org.jboss.envers.entities.mapper.relation.component;
+
+import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.tools.query.Parameters;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+
+import java.util.Map;
+
+/**
+ * A component mapper for the @MapKey mapping with the name parameter specified: the
value of the map's key
+ * is a property of the entity. This doesn't have an effect on the data stored in the
versions tables,
+ * so <code>mapToMapFromObject</code> is empty.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MiddleMapKeyPropertyComponentMapper implements MiddleComponentMapper
{
+ private final String propertyName;
+
+ public MiddleMapKeyPropertyComponentMapper(String propertyName) {
+ this.propertyName = propertyName;
+ }
+
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Object dataObject, Number revision) {
+ // dataObject is not null, as this mapper can only be used in an index.
+ return ReflectionTools.getGetter(dataObject.getClass(),
propertyName).get(dataObject);
+ }
+
+ public void mapToMapFromObject(Map<String, Object> data, Object obj) {
+ // Doing nothing.
+ }
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
+ // Doing nothing.
+ }
+}
\ No newline at end of file
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -17,7 +17,7 @@
}
public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
- Number revision) {
+ Object dataObject, Number revision) {
return
entityInstantiator.createInstanceFromVersionsEntity(relatedIdData.getEntityName(), data,
revision);
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -19,7 +19,8 @@
}
@SuppressWarnings({"unchecked"})
- public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data, Number revision) {
+ public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
+ Object dataObject, Number revision) {
return ((Map<String, Object>)
data.get(verEntCfg.getOriginalIdPropName())).get(propertyName);
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -35,11 +35,11 @@
protected void addToCollection(Object[] collection, Object collectionRow) {
Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) elementData, revision);
+ (Map<String, Object>) elementData, null, revision);
Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
Object indexObj =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) indexData, revision);
+ (Map<String, Object>) indexData, element, revision);
int index = ((Number) indexObj).intValue();
collection[index] = element;
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -42,7 +42,7 @@
protected void addToCollection(T collection, Object collectionRow) {
Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) elementData, revision);
+ (Map<String, Object>) elementData, null, revision);
collection.add(element);
}
}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -35,11 +35,11 @@
protected void addToCollection(List collection, Object collectionRow) {
Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) elementData, revision);
+ (Map<String, Object>) elementData, null, revision);
Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
Object indexObj =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) indexData, revision);
+ (Map<String, Object>) indexData, element, revision);
int index = ((Number) indexObj).intValue();
collection.set(index, element);
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-22
21:20:21 UTC (rev 164)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -45,11 +45,11 @@
protected void addToCollection(T collection, Object collectionRow) {
Object elementData = ((List)
collectionRow).get(elementComponentData.getComponentIndex());
Object element =
elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) elementData, revision);
+ (Map<String, Object>) elementData, null, revision);
Object indexData = ((List)
collectionRow).get(indexComponentData.getComponentIndex());
Object index =
indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
- (Map<String, Object>) indexData, revision);
+ (Map<String, Object>) indexData, element, revision);
collection.put(index, element);
}
Copied: trunk/src/test/org/jboss/envers/test/entities/components/Component1.java (from rev
148, trunk/src/test/org/jboss/envers/test/integration/components/Component1.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/components/Component1.java
(rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/components/Component1.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,57 @@
+package org.jboss.envers.test.entities.components;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class Component1 {
+ private String str1;
+
+ private String str2;
+
+ public Component1(String str1, String str2) {
+ this.str1 = str1;
+ this.str2 = str2;
+ }
+
+ public Component1() {
+ }
+
+ public String getStr2() {
+ return str2;
+ }
+
+ public void setStr2(String str2) {
+ this.str2 = str2;
+ }
+
+ public String getStr1() {
+ return str1;
+ }
+
+ public void setStr1(String str1) {
+ this.str1 = str1;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Component1)) return false;
+
+ Component1 that = (Component1) o;
+
+ if (str1 != null ? !str1.equals(that.str1) : that.str1 != null) return false;
+ if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (str1 != null ? str1.hashCode() : 0);
+ result = 31 * result + (str2 != null ? str2.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "Comp1(str1 = " + str1 + ", " + str2 + ")";
+ }
+}
Copied: trunk/src/test/org/jboss/envers/test/entities/components/Component2.java (from rev
148, trunk/src/test/org/jboss/envers/test/integration/components/Component2.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/components/Component2.java
(rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/components/Component2.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,57 @@
+package org.jboss.envers.test.entities.components;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class Component2 {
+ private String str5;
+
+ private String str6;
+
+ public Component2(String str5, String str6) {
+ this.str5 = str5;
+ this.str6 = str6;
+ }
+
+ public Component2() {
+ }
+
+ public String getStr5() {
+ return str5;
+ }
+
+ public void setStr5(String str5) {
+ this.str5 = str5;
+ }
+
+ public String getStr6() {
+ return str6;
+ }
+
+ public void setStr6(String str6) {
+ this.str6 = str6;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Component2)) return false;
+
+ Component2 that = (Component2) o;
+
+ if (str5 != null ? !str5.equals(that.str5) : that.str5 != null) return false;
+ if (str6 != null ? !str6.equals(that.str6) : that.str6 != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (str5 != null ? str5.hashCode() : 0);
+ result = 31 * result + (str6 != null ? str6.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "Comp2(str1 = " + str5 + ", " + str6 + ")";
+ }
+}
Copied: trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java
(from rev 148,
trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,89 @@
+package org.jboss.envers.test.entities.components;
+
+import org.jboss.envers.Versioned;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Embedded;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class ComponentTestEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Embedded
+ @Versioned
+ private Component1 comp1;
+
+ @Embedded
+ private Component2 comp2;
+
+ public ComponentTestEntity() {
+ }
+
+ public ComponentTestEntity(Integer id, Component1 comp1, Component2 comp2) {
+ this.id = id;
+ this.comp1 = comp1;
+ this.comp2 = comp2;
+ }
+
+ public ComponentTestEntity(Component1 comp1, Component2 comp2) {
+ this.comp1 = comp1;
+ this.comp2 = comp2;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Component1 getComp1() {
+ return comp1;
+ }
+
+ public void setComp1(Component1 comp1) {
+ this.comp1 = comp1;
+ }
+
+ public Component2 getComp2() {
+ return comp2;
+ }
+
+ public void setComp2(Component2 comp2) {
+ this.comp2 = comp2;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof ComponentTestEntity)) return false;
+
+ ComponentTestEntity that = (ComponentTestEntity) o;
+
+ if (comp1 != null ? !comp1.equals(that.comp1) : that.comp1 != null) return
false;
+ if (comp2 != null ? !comp2.equals(that.comp2) : that.comp2 != 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 + (comp1 != null ? comp1.hashCode() : 0);
+ result = 31 * result + (comp2 != null ? comp2.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "CTE(id = " + id + ", comp1 = " + comp1 + ",
comp2 = " + comp2 + ")";
+ }
+}
+
Added:
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,88 @@
+package org.jboss.envers.test.integration.collection.mapkey;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.components.ComponentTestEntity;
+import org.jboss.envers.test.entities.components.Component1;
+import org.jboss.envers.test.entities.components.Component2;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ComponentMapKey extends AbstractEntityTest {
+ private Integer cmke_id;
+
+ private Integer cte1_id;
+ private Integer cte2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(ComponentMapKeyEntity.class);
+ cfg.addAnnotatedClass(ComponentTestEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ ComponentMapKeyEntity imke = new ComponentMapKeyEntity();
+
+ // Revision 1 (intialy 1 mapping)
+ em.getTransaction().begin();
+
+ ComponentTestEntity cte1 = new ComponentTestEntity(new Component1("x1",
"y2"), new Component2("a1", "b2"));
+ ComponentTestEntity cte2 = new ComponentTestEntity(new Component1("x1",
"y2"), new Component2("a1", "b2"));
+
+ em.persist(cte1);
+ em.persist(cte2);
+
+ imke.getIdmap().put(cte1.getComp1(), cte1);
+
+ em.persist(imke);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (sse1: adding 1 mapping)
+ em.getTransaction().begin();
+
+ cte2 = em.find(ComponentTestEntity.class, cte2.getId());
+ imke = em.find(ComponentMapKeyEntity.class, imke.getId());
+
+ imke.getIdmap().put(cte2.getComp1(), cte2);
+
+ em.getTransaction().commit();
+
+ //
+
+ cmke_id = imke.getId();
+
+ cte1_id = cte1.getId();
+ cte2_id = cte2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(ComponentMapKeyEntity.class, cmke_id));
+ }
+
+ @Test
+ public void testHistoryOfImke() {
+ ComponentTestEntity cte1 = getEntityManager().find(ComponentTestEntity.class,
cte1_id);
+ ComponentTestEntity cte2 = getEntityManager().find(ComponentTestEntity.class,
cte2_id);
+
+ // These fields are unversioned.
+ cte1.setComp2(null);
+ cte2.setComp2(null);
+
+ ComponentMapKeyEntity rev1 =
getVersionsReader().find(ComponentMapKeyEntity.class, cmke_id, 1);
+ ComponentMapKeyEntity rev2 =
getVersionsReader().find(ComponentMapKeyEntity.class, cmke_id, 2);
+
+ assert rev1.getIdmap().equals(TestTools.makeMap(cte1.getComp1(), cte1));
+ assert rev2.getIdmap().equals(TestTools.makeMap(cte1.getComp1(), cte1,
cte2.getComp1(), cte2));
+ }
+}
\ No newline at end of file
Added:
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,63 @@
+package org.jboss.envers.test.integration.collection.mapkey;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.components.Component1;
+import org.jboss.envers.test.entities.components.ComponentTestEntity;
+
+import javax.persistence.*;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class ComponentMapKeyEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @ManyToMany
+ @MapKey(name = "comp1")
+ private Map<Component1, ComponentTestEntity> idmap;
+
+ public ComponentMapKeyEntity() {
+ idmap = new HashMap<Component1, ComponentTestEntity>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<Component1, ComponentTestEntity> getIdmap() {
+ return idmap;
+ }
+
+ public void setIdmap(Map<Component1, ComponentTestEntity> idmap) {
+ this.idmap = idmap;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof ComponentMapKeyEntity)) return false;
+
+ ComponentMapKeyEntity that = (ComponentMapKeyEntity) o;
+
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (id != null ? id.hashCode() : 0);
+ }
+
+ public String toString() {
+ return "CMKE(id = " + id + ", idmap = " + idmap +
")";
+ }
+}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java
(from rev 154,
trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,83 @@
+package org.jboss.envers.test.integration.collection.mapkey;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class IdMapKey extends AbstractEntityTest {
+ private Integer imke_id;
+
+ private Integer ste1_id;
+ private Integer ste2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(IdMapKeyEntity.class);
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ IdMapKeyEntity imke = new IdMapKeyEntity();
+
+ // Revision 1 (intialy 1 mapping)
+ em.getTransaction().begin();
+
+ StrTestEntity ste1 = new StrTestEntity("x");
+ StrTestEntity ste2 = new StrTestEntity("y");
+
+ em.persist(ste1);
+ em.persist(ste2);
+
+ imke.getIdmap().put(ste1.getId(), ste1);
+
+ em.persist(imke);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (sse1: adding 1 mapping)
+ em.getTransaction().begin();
+
+ ste2 = em.find(StrTestEntity.class, ste2.getId());
+ imke = em.find(IdMapKeyEntity.class, imke.getId());
+
+ imke.getIdmap().put(ste2.getId(), ste2);
+
+ em.getTransaction().commit();
+
+ //
+
+ imke_id = imke.getId();
+
+ ste1_id = ste1.getId();
+ ste2_id = ste2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(IdMapKeyEntity.class, imke_id));
+ }
+
+ @Test
+ public void testHistoryOfImke() {
+ StrTestEntity ste1 = getEntityManager().find(StrTestEntity.class, ste1_id);
+ StrTestEntity ste2 = getEntityManager().find(StrTestEntity.class, ste2_id);
+
+ IdMapKeyEntity rev1 = getVersionsReader().find(IdMapKeyEntity.class, imke_id,
1);
+ IdMapKeyEntity rev2 = getVersionsReader().find(IdMapKeyEntity.class, imke_id,
2);
+
+ System.out.println(rev1.getIdmap());
+ assert rev1.getIdmap().equals(TestTools.makeMap(ste1.getId(), ste1));
+ assert rev2.getIdmap().equals(TestTools.makeMap(ste1.getId(), ste1, ste2.getId(),
ste2));
+ }
+}
\ No newline at end of file
Copied:
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java
(from rev 148, trunk/src/test/org/jboss/envers/test/entities/StrIntTestEntity.java)
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -0,0 +1,62 @@
+package org.jboss.envers.test.integration.collection.mapkey;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.*;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class IdMapKeyEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @ManyToMany
+ @MapKey
+ private Map<Integer, StrTestEntity> idmap;
+
+ public IdMapKeyEntity() {
+ idmap = new HashMap<Integer, StrTestEntity>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<Integer, StrTestEntity> getIdmap() {
+ return idmap;
+ }
+
+ public void setIdmap(Map<Integer, StrTestEntity> idmap) {
+ this.idmap = idmap;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof IdMapKeyEntity)) return false;
+
+ IdMapKeyEntity that = (IdMapKeyEntity) o;
+
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (id != null ? id.hashCode() : 0);
+ }
+
+ public String toString() {
+ return "IMKE(id = " + id + ", idmap = " + idmap +
")";
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: trunk/src/test/org/jboss/envers/test/integration/components/Components.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/components/Components.java 2008-09-22
21:20:21 UTC (rev 164)
+++ trunk/src/test/org/jboss/envers/test/integration/components/Components.java 2008-09-23
17:05:36 UTC (rev 165)
@@ -1,6 +1,9 @@
package org.jboss.envers.test.integration.components;
import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.components.Component1;
+import org.jboss.envers.test.entities.components.Component2;
+import org.jboss.envers.test.entities.components.ComponentTestEntity;
import org.hibernate.ejb.Ejb3Configuration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;