JBoss Envers SVN: r162 - in trunk/src: test/org/jboss/envers/test/entities/collection and 1 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 14:07:02 -0400 (Mon, 22 Sep 2008)
New Revision: 162
Added:
trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
Log:
ENVERS-24, ENVERS-42: enum support in collections
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 18:07:02 UTC (rev 162)
@@ -71,7 +71,7 @@
MetadataTools.addColumns(prop_mapping, (Iterator<Column>) value.getColumnIterator());
}
- // A null mapper means that we only want to add xml mappings (while building the id mapping)
+ // A null mapper means that we only want to add xml mappings
if (mapper != null) {
mapper.add(name, store);
}
@@ -81,8 +81,8 @@
private void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper,
ModificationStore store, boolean key) {
if (parent != null) {
- Element prop_mapping = parent.addElement("property");
- prop_mapping.addAttribute("name", name);
+ Element prop_mapping = MetadataTools.addProperty(parent, name,
+ null, key);
CustomType propertyType = (CustomType) value.getType();
@@ -93,14 +93,16 @@
type_param1.addAttribute("name", "enumClass");
type_param1.setText(propertyType.getReturnedClass().getName());
- //Element type_param2 = type_mapping.addElement("param");
- //type_param2.addAttribute("name", "type");
- //type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
+ Element type_param2 = type_mapping.addElement("param");
+ type_param2.addAttribute("name", "type");
+ type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
MetadataTools.addColumns(prop_mapping, (Iterator<Column>) value.getColumnIterator());
}
- mapper.add(name, store);
+ if (mapper != null) {
+ mapper.add(name, store);
+ }
}
private void addComponentClassName(Element any_mapping, Component comp) {
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 18:07:02 UTC (rev 162)
@@ -304,17 +304,18 @@
return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData),
queryGeneratorBuilder.getCurrentIndex());
- } else if (type instanceof ImmutableType || type instanceof MutableType) {
- // TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, ModificationStore.FULL,
- referencingEntityName, true);
+ } else {
+ boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null,
+ ModificationStore.FULL, referencingEntityName, true);
- // Simple values are always stored in the first entity read by the query generator.
- return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
- } else {
- mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
- // Impossible to get here.
- throw new AssertionError();
+ if (mapped) {
+ // Simple values are always stored in the first item of the array returned by the query generator.
+ return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
+ } else {
+ mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
+ // Impossible to get here.
+ throw new AssertionError();
+ }
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 16:45:44 UTC (rev 161)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 18:07:02 UTC (rev 162)
@@ -52,8 +52,12 @@
prop_mapping = parent.addElement("property");
}
- prop_mapping.addAttribute("name", name).addAttribute("type", type);
+ prop_mapping.addAttribute("name", name);
+ if (type != null) {
+ prop_mapping.addAttribute("type", type);
+ }
+
return prop_mapping;
}
Copied: trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java (from rev 153, trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java 2008-09-22 18:07:02 UTC (rev 162)
@@ -0,0 +1,79 @@
+package org.jboss.envers.test.entities.collection;
+
+import org.jboss.envers.Versioned;
+import org.hibernate.annotations.CollectionOfElements;
+
+import javax.persistence.*;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class EnumSetEntity {
+ public static enum E1 { X, Y }
+ public static enum E2 { A, B }
+
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @CollectionOfElements
+ @Enumerated(EnumType.STRING)
+ private Set<E1> enums1;
+
+ @Versioned
+ @CollectionOfElements
+ @Enumerated(EnumType.ORDINAL)
+ private Set<E2> enums2;
+
+ public EnumSetEntity() {
+ enums1 = new HashSet<E1>();
+ enums2 = new HashSet<E2>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Set<E1> getEnums1() {
+ return enums1;
+ }
+
+ public void setEnums1(Set<E1> enums1) {
+ this.enums1 = enums1;
+ }
+
+ public Set<E2> getEnums2() {
+ return enums2;
+ }
+
+ public void setEnums2(Set<E2> enums2) {
+ this.enums2 = enums2;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof EnumSetEntity)) return false;
+
+ EnumSetEntity that = (EnumSetEntity) 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 "ESE(id = " + id + ", enums1 = " + enums1 + ", enums2 = " + enums2 + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java (from rev 154, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java 2008-09-22 18:07:02 UTC (rev 162)
@@ -0,0 +1,86 @@
+package org.jboss.envers.test.integration.collection;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.collection.EnumSetEntity;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.jboss.envers.test.entities.collection.EnumSetEntity.*;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class EnumSet extends AbstractEntityTest {
+ private Integer sse1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(EnumSetEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ EnumSetEntity sse1 = new EnumSetEntity();
+
+ // Revision 1 (sse1: initialy 1 element)
+ em.getTransaction().begin();
+
+ sse1.getEnums1().add(E1.X);
+ sse1.getEnums2().add(E2.A);
+
+ em.persist(sse1);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (sse1: adding 1 element/removing a non-existing element)
+ em.getTransaction().begin();
+
+ sse1 = em.find(EnumSetEntity.class, sse1.getId());
+
+ sse1.getEnums1().add(E1.Y);
+ sse1.getEnums2().remove(E2.B);
+
+ em.getTransaction().commit();
+
+ // Revision 3 (sse1: removing 1 element/adding an exisiting element)
+ em.getTransaction().begin();
+
+ sse1 = em.find(EnumSetEntity.class, sse1.getId());
+
+ sse1.getEnums1().remove(E1.X);
+ sse1.getEnums2().add(E2.A);
+
+ em.getTransaction().commit();
+
+ //
+
+ sse1_id = sse1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(EnumSetEntity.class, sse1_id));
+ }
+
+ @Test
+ public void testHistoryOfSse1() {
+ EnumSetEntity rev1 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 1);
+ EnumSetEntity rev2 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 2);
+ EnumSetEntity rev3 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 3);
+
+ assert rev1.getEnums1().equals(TestTools.makeSet(E1.X));
+ assert rev2.getEnums1().equals(TestTools.makeSet(E1.X, E1.Y));
+ assert rev3.getEnums1().equals(TestTools.makeSet(E1.Y));
+
+ assert rev1.getEnums2().equals(TestTools.makeSet(E2.A));
+ assert rev2.getEnums2().equals(TestTools.makeSet(E2.A));
+ assert rev3.getEnums2().equals(TestTools.makeSet(E2.A));
+ }
+}
\ No newline at end of file
16 years, 3 months
JBoss Envers SVN: r161 - in trunk/src/main/org/jboss/envers: entities/mapper/relation and 1 other directory.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 12:45:44 -0400 (Mon, 22 Sep 2008)
New Revision: 161
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
Log:
ENVERS-44: restructuring the basic mapper
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -5,12 +5,11 @@
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
-import org.hibernate.type.CustomType;
+import org.hibernate.type.*;
import org.hibernate.util.StringHelper;
import org.jboss.envers.entities.mapper.SimpleMapperBuilder;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.ModificationStore;
-import org.jboss.envers.exception.VersionsException;
import java.util.Iterator;
@@ -19,14 +18,52 @@
* @author Adam Warski (adam at warski dot org)
*/
public final class BasicMetadataGenerator {
- private final VersionsMetadataGenerator mainGenerator;
+ boolean addBasic(Element parent, String name, Value value, CompositeMapperBuilder mapper,
+ ModificationStore store, String entityName, boolean key) {
+ Type type = value.getType();
- BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
- mainGenerator = versionsMetadataGenerator;
+ if (type instanceof ComponentType) {
+ addComponent(parent, name, value, mapper, entityName, key);
+ } else if (type instanceof ImmutableType || type instanceof MutableType) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) {
+ addEnumValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
+ ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+ "org.hibernate.type.StringClobType".equals(type.getName()))) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else {
+ return false;
+ }
+
+ return true;
}
+ boolean addBasicNoComponent(Element parent, String name, Value value, SimpleMapperBuilder mapper,
+ ModificationStore store, boolean key) {
+ Type type = value.getType();
+
+ if (type instanceof ImmutableType || type instanceof MutableType) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) {
+ addEnumValue(parent, name, value, mapper, store, key);
+ } else if (type instanceof CustomType &&
+ ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
+ "org.hibernate.type.StringClobType".equals(type.getName()))) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) {
+ addSimpleValue(parent, name, value, mapper, store, key);
+ } else {
+ return false;
+ }
+
+ return true;
+ }
+
@SuppressWarnings({"unchecked"})
- void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper,
+ private void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper,
ModificationStore store, boolean key) {
if (parent != null) {
Element prop_mapping = MetadataTools.addProperty(parent, name,
@@ -41,8 +78,8 @@
}
@SuppressWarnings({"unchecked"})
- void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper,
- ModificationStore store) {
+ private void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper,
+ ModificationStore store, boolean key) {
if (parent != null) {
Element prop_mapping = parent.addElement("property");
prop_mapping.addAttribute("name", name);
@@ -56,9 +93,9 @@
type_param1.addAttribute("name", "enumClass");
type_param1.setText(propertyType.getReturnedClass().getName());
- Element type_param2 = type_mapping.addElement("param");
- type_param2.addAttribute("name", "type");
- type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
+ //Element type_param2 = type_mapping.addElement("param");
+ //type_param2.addAttribute("name", "type");
+ //type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
MetadataTools.addColumns(prop_mapping, (Iterator<Column>) value.getColumnIterator());
}
@@ -73,13 +110,14 @@
}
@SuppressWarnings({"unchecked"})
- void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper,
- String entityName, EntityXmlMappingData xmlMappingData,
- boolean firstPass) {
+ private void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper,
+ String entityName, boolean key) {
Element component_mapping = null;
Component prop_component = (Component) value;
if (parent != null) {
+ /*
+ TODO: investigate relations inside components
if (!firstPass) {
// The required element already exists.
Iterator<Element> iter = parent.elementIterator("component");
@@ -95,11 +133,12 @@
throw new VersionsException("Element for component not found during second pass!");
}
} else {
- component_mapping = parent.addElement("component");
- component_mapping.addAttribute("name", name);
+ */
- addComponentClassName(component_mapping, prop_component);
- }
+ component_mapping = parent.addElement("component");
+ component_mapping.addAttribute("name", name);
+
+ addComponentClassName(component_mapping, prop_component);
}
CompositeMapperBuilder componentMapper = mapper.addComposite(name);
@@ -107,8 +146,8 @@
Iterator<Property> properties = (Iterator<Property>) prop_component.getPropertyIterator();
while (properties.hasNext()) {
Property property = properties.next();
- mainGenerator.addValue(component_mapping, property.getName(), property.getValue(),
- componentMapper, ModificationStore.FULL, entityName, xmlMappingData, null, firstPass);
+ addBasic(component_mapping, property.getName(), property.getValue(), componentMapper,
+ ModificationStore.FULL, entityName, key);
}
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -306,13 +306,15 @@
queryGeneratorBuilder.getCurrentIndex());
} else if (type instanceof ImmutableType || type instanceof MutableType) {
// TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(xmlMapping, prefix, value, null, ModificationStore.FULL, true);
+ mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, ModificationStore.FULL,
+ referencingEntityName, true);
// Simple values are always stored in the first entity read by the query generator.
return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
} else {
- // TODO: throw an exception
- throw new RuntimeException();
+ mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
+ // Impossible to get here.
+ throw new AssertionError();
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -36,7 +36,7 @@
Type propertyType = property.getType();
if (!"_identifierMapper".equals(property.getName())) {
if (propertyType instanceof ImmutableType) {
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(parent, property.getName(),
+ mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(parent, property.getName(),
property.getValue(), mapper, ModificationStore.FULL, key);
} else {
throw new MappingException("Type not supported: " + propertyType.getClass().getName());
@@ -57,12 +57,16 @@
SimpleIdMapperBuilder mapper;
if (id_mapper != null) {
+ // Multiple id
+
mapper = new MultipleIdMapper(((Component) pc.getIdentifier()).getComponentClassName());
addIdProperties(rel_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), mapper, false);
// null mapper - the mapping where already added the first time, now we only want to generate the xml
addIdProperties(orig_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), null, true);
} else if (id_prop.isComposite()) {
+ // Embedded id
+
Component id_component = (Component) id_prop.getValue();
mapper = new EmbeddedIdMapper(id_prop.getName(), id_component.getComponentClassName());
@@ -71,13 +75,15 @@
// null mapper - the mapping where already added the first time, now we only want to generate the xml
addIdProperties(orig_id_mapping, (Iterator<Property>) id_component.getPropertyIterator(), null, true);
} else {
+ // Single id
+
mapper = new SingleIdMapper();
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(rel_id_mapping, id_prop.getName(),
+ mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(rel_id_mapping, id_prop.getName(),
id_prop.getValue(), mapper, ModificationStore.FULL, false);
// null mapper - the mapping where already added the first time, now we only want to generate the xml
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(orig_id_mapping, id_prop.getName(),
+ mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(orig_id_mapping, id_prop.getName(),
id_prop.getValue(), null, ModificationStore.FULL, true);
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -7,7 +7,7 @@
import org.hibernate.MappingException;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.entities.mapper.relation.ToOneIdMapper;
-import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningIdMapper;
+import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningMapper;
import org.jboss.envers.entities.mapper.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
@@ -82,7 +82,7 @@
referencedEntityName, ownedIdMapper);
// Adding mapper for the id
- mapper.addComposite(name, new OneToOneNotOwningIdMapper(owningReferencePropertyName,
+ mapper.addComposite(name, new OneToOneNotOwningMapper(owningReferencePropertyName,
referencedEntityName, name));
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -72,7 +72,7 @@
this.verEntCfg = verEntCfg;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
- this.basicMetadataGenerator = new BasicMetadataGenerator(this);
+ this.basicMetadataGenerator = new BasicMetadataGenerator();
this.idMetadataGenerator = new IdMetadataGenerator(this);
this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);
@@ -122,36 +122,15 @@
VersionsJoinTable joinTable, boolean firstPass) {
Type type = value.getType();
- if (type instanceof ComponentType) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addComponent(parent, name, value, currentMapper, entityName, xmlMappingData, firstPass);
+ // only first pass
+ if (firstPass) {
+ if (basicMetadataGenerator.addBasic(parent, name, value, currentMapper, store, entityName, false)) {
+ // The property was mapped by the basic generator.
+ return;
}
- } else if (type instanceof ImmutableType || type instanceof MutableType) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false);
- }
- } else if (type instanceof CustomType &&
- "org.hibernate.type.EnumType".equals(type.getName())) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper, store);
- }
- } else if (type instanceof CustomType &&
- ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) ||
- "org.hibernate.type.StringClobType".equals(type.getName()))) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false);
- }
- } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
- type.getClass().getName())) {
- // only first pass
- if (firstPass) {
- basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false);
- }
- } else if (type instanceof ManyToOneType) {
+ }
+
+ if (type instanceof ManyToOneType) {
// only second pass
if (!firstPass) {
toOneRelationMetadataGenerator.addToOne(parent, name, value, currentMapper, entityName);
@@ -169,7 +148,11 @@
collectionMetadataGenerator.addCollection();
}
} else {
- throwUnsupportedTypeException(type, entityName, name);
+ if (firstPass) {
+ // If we got here in the first pass, it means the basic mapper didn't map it, and none of the
+ // above branches either.
+ throwUnsupportedTypeException(type, entityName, name);
+ }
}
}
Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java (from rev 148, trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java)
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java (rev 0)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java 2008-09-22 16:45:44 UTC (rev 161)
@@ -0,0 +1,87 @@
+/*
+ * 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.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;
+
+import javax.persistence.NoResultException;
+import java.util.Map;
+import java.util.List;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class OneToOneNotOwningMapper implements PropertyMapper {
+ private String owningReferencePropertyName;
+ private String owningEntityName;
+ private String propertyName;
+
+ public OneToOneNotOwningMapper(String owningReferencePropertyName, String owningEntityName, String propertyName) {
+ this.owningReferencePropertyName = owningReferencePropertyName;
+ this.owningEntityName = owningEntityName;
+ this.propertyName = propertyName;
+ }
+
+ public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj, Object oldObj) {
+ return false;
+ }
+
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) {
+ if (obj == null) {
+ return;
+ }
+
+ Class<?> entityClass = ReflectionTools.loadClass(owningEntityName);
+
+ Object value;
+
+ try {
+ value = versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision)
+ .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName, primaryKey)).getSingleResult();
+ } catch (NoResultException e) {
+ value = null;
+ } catch (NonUniqueResultException e) {
+ throw new VersionsException("Many versions results for one-to-one relationship: (" + owningEntityName +
+ ", " + owningReferencePropertyName + ")");
+ }
+
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
+ setter.set(obj, value, null);
+ }
+
+ public List<PersistentCollectionChangeData> mapCollectionChanges(String referencingPropertyName,
+ PersistentCollection newColl,
+ Serializable oldColl,
+ Serializable id) {
+ return null;
+ }
+}
16 years, 3 months
JBoss Envers SVN: r160 - in trunk/src/main/org/jboss/envers: entities/mapper/relation and 1 other directory.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 11:33:44 -0400 (Mon, 22 Sep 2008)
New Revision: 160
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
Log:
ENVERS-44: restructuring the collections mapper, making it statefull
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
@@ -28,113 +28,104 @@
import java.lang.annotation.Annotation;
/**
- * Generates metadata for collection-valued properties.
+ * Generates metadata for a collection-valued property.
* @author Adam Warski (adam at warski dot org)
*/
public final class CollectionMetadataGenerator {
private final VersionsMetadataGenerator mainGenerator;
+ private final String propertyName;
+ private final Collection propertyValue;
+ private final CompositeMapperBuilder currentMapper;
+ private final String referencingEntityName;
+ private final EntityXmlMappingData xmlMappingData;
+ private final VersionsJoinTable joinTable;
- CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) {
- mainGenerator = versionsMetadataGenerator;
- }
+ private final EntityConfiguration referencingEntityConfiguration;
+ /**
+ * Null if this collection isn't a relation to another entity.
+ */
+ private final String referencedEntityName;
- void addCollection(String name, Collection value, CompositeMapperBuilder currentMapper,
- String entityName, EntityXmlMappingData xmlMappingData,
- VersionsJoinTable joinTable) {
- Type type = value.getType();
+ /**
+ * @param mainGenerator Main generator, giving access to configuration and the basic mapper.
+ * @param propertyName Name of the property that references the collection in the referencing entity.
+ * @param propertyValue Value of the collection, as mapped by Hibernate.
+ * @param currentMapper Mapper, to which the appropriate {@link org.jboss.envers.entities.mapper.PropertyMapper}
+ * will be added.
+ * @param referencingEntityName Name of the entity that owns this collection.
+ * @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.
+ */
+ public CollectionMetadataGenerator(VersionsMetadataGenerator mainGenerator, String propertyName,
+ Collection propertyValue, CompositeMapperBuilder currentMapper,
+ String referencingEntityName, EntityXmlMappingData xmlMappingData,
+ VersionsJoinTable joinTable) {
+ this.mainGenerator = mainGenerator;
+ this.propertyName = propertyName;
+ this.propertyValue = propertyValue;
+ this.currentMapper = currentMapper;
+ this.referencingEntityName = referencingEntityName;
+ this.xmlMappingData = xmlMappingData;
+ this.joinTable = joinTable == null ? getDefaultVersionsJoinTable() : joinTable;
- if ((type instanceof BagType || type instanceof SetType) &&
- (value.getElement() instanceof OneToMany) && (value.isInverse())) {
- addOneToManyAttached(name, value, currentMapper, entityName);
- } else {
- addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData,
- joinTable == null ? getDefaultVersionsJoinTable() : joinTable);
+ referencingEntityConfiguration = mainGenerator.getEntitiesConfigurations().get(referencingEntityName);
+ if (referencingEntityConfiguration == null) {
+ throw new MappingException("Unable to read versioning configuration for " + referencingEntityName + "!");
}
- }
- private VersionsJoinTable getDefaultVersionsJoinTable() {
- return new VersionsJoinTable() {
- public String name() { return ""; }
- public String schema() { return ""; }
- public String catalog() { return ""; }
- public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; }
- public Class<? extends Annotation> annotationType() { return this.getClass(); }
- };
+ referencedEntityName = getReferencedEntityName(propertyValue.getElement());
}
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Collection collectionValue) {
- Iterator<Property> assocClassProps =
- ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
-
- while (assocClassProps.hasNext()) {
- Property property = assocClassProps.next();
-
- if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
- collectionValue.getKey().getColumnIterator())) {
- return property.getName();
- }
+ private String getReferencedEntityName(Value value) {
+ if (value instanceof ToOne) {
+ return ((ToOne) value).getReferencedEntityName();
+ } else if (value instanceof OneToMany) {
+ return ((OneToMany) value).getReferencedEntityName();
+ } else {
+ return null;
}
-
- return null;
}
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
- Iterator<Property> properties = referencedClass.getPropertyIterator();
- while (properties.hasNext()) {
- Property property = properties.next();
- if (property.getValue() instanceof Collection) {
- // The equality is intentional. We want to find a collection property with the same collection table.
- //noinspection ObjectEquality
- if (((Collection) property.getValue()).getCollectionTable() == collectionTable) {
- return property.getName();
- }
- }
+ void addCollection() {
+ Type type = propertyValue.getType();
+
+ if ((type instanceof BagType || type instanceof SetType) &&
+ (propertyValue.getElement() instanceof OneToMany) && (propertyValue.isInverse())) {
+ // A one-to-many relation mapped using @ManyToOne and @OneToMany(mappedBy="...")
+ addOneToManyAttached();
+ } else {
+ // All other kinds of relations require a middle (join) table).
+ addWithMiddleTable();
}
-
- return null;
}
@SuppressWarnings({"unchecked"})
- private void addOneToManyAttached(String name, Collection value, CompositeMapperBuilder mapper, String entityName) {
- String owningReferencePropertyName = getMappedBy(value);
- if (owningReferencePropertyName == null) {
- throw new MappingException("Unable to read the mapped by attribute for " + name + " in " + entityName + "!");
- }
+ private void addOneToManyAttached() {
+ String owningReferencePropertyName = getMappedBy(propertyValue);
- EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName);
- if (configuration == null) {
- throw new MappingException("Unable to read versioning configuration for " + entityName + "!");
- }
-
- IdMappingData referencingIdMapping = configuration.getIdMappingData();
-
- String owningEntityName = ((OneToMany) value.getElement()).getReferencedEntityName();
+ IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
String lastPropertyPrefix = owningReferencePropertyName + "_";
// Generating the id mapper for the relation
IdMapper ownedIdMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
// Storing information about this relation
- mainGenerator.getEntitiesConfigurations().get(entityName).addToManyNotOwningRelation(name, owningReferencePropertyName,
- owningEntityName, ownedIdMapper);
+ referencingEntityConfiguration.addToManyNotOwningRelation(propertyName, owningReferencePropertyName,
+ referencedEntityName, ownedIdMapper);
// Adding mapper for the id
- mapper.addComposite(name, new OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName,
- name));
+ currentMapper.addComposite(propertyName, new OneToManyAttachedMapper(referencedEntityName, propertyName,
+ owningReferencePropertyName));
}
- private String getReferencedEntityName(Value value) {
- if (value instanceof ToOne) {
- return ((ToOne) value).getReferencedEntityName();
- } else if (value instanceof OneToMany) {
- return ((OneToMany) value).getReferencedEntityName();
- } else {
- return null;
- }
- }
-
+ /**
+ * Adds mapping of the id of a related entity to the given xml mapping, prefixing the id with the given prefix.
+ * @param xmlMapping Mapping, to which to add the xml.
+ * @param prefix Prefix for the names of properties which will be prepended to properties that form the id.
+ * @param columnNameIterator Iterator over the column names that will be used for properties that form the id.
+ * @param relatedIdMapping Id mapping data of the related entity.
+ */
@SuppressWarnings({"unchecked"})
private void addRelatedToXmlMapping(Element xmlMapping, String prefix,
MetadataTools.ColumnNameIterator columnNameIterator,
@@ -159,17 +150,15 @@
}
@SuppressWarnings({"unchecked"})
- private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper,
- String entityName, EntityXmlMappingData xmlMappingData, VersionsJoinTable joinTable) {
+ private void addWithMiddleTable() {
// Generating the name of the middle table
-
String versionsMiddleTableName;
String versionsMiddleEntityName;
if (!StringTools.isEmpty(joinTable.name())) {
versionsMiddleTableName = joinTable.name();
versionsMiddleEntityName = joinTable.name();
} else {
- String middleTableName = getMiddleTableName(value, entityName);
+ String middleTableName = getMiddleTableName(propertyValue, referencingEntityName);
versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName);
}
@@ -177,32 +166,18 @@
// Generating the XML mapping for the middle entity, only if the relation isn't inverse.
// If the relation is inverse, will be later checked by comparing middleEntityXml with null.
Element middleEntityXml;
- Element middleEntityXmlId;
- if (!value.isInverse()) {
- String schema = StringTools.isEmpty(joinTable.schema()) ? value.getCollectionTable().getSchema() : joinTable.schema();
- String catalog = StringTools.isEmpty(joinTable.catalog()) ? value.getCollectionTable().getCatalog() : joinTable.catalog();
-
- middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(),
- versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null);
- middleEntityXmlId = middleEntityXml.addElement("composite-id");
+ if (!propertyValue.isInverse()) {
+ middleEntityXml = createMiddleEntityXml(versionsMiddleTableName, versionsMiddleEntityName);
} else {
middleEntityXml = null;
- middleEntityXmlId = null;
}
// ******
// Generating the mapping for the referencing entity (it must be an entity).
// ******
- EntityConfiguration referencingConfiguration = mainGenerator.getEntitiesConfigurations().get(entityName);
- if (referencingConfiguration == null) {
- throw new MappingException("Unable to read versioning configuration for " + entityName + "!");
- }
-
// Getting the id-mapping data of the referencing entity (the entity that "owns" this collection).
- IdMappingData referencingIdMapping = referencingConfiguration.getIdMappingData();
+ IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
- // Null if this collection doesn't reference an entity.
- String referencedEntityName = getReferencedEntityName(value.getElement());
// Only valid for an inverse relation; null otherwise.
String mappedBy;
@@ -210,28 +185,25 @@
String referencingPrefixRelated;
String referencedPrefix;
- if (value.isInverse()) {
+ if (propertyValue.isInverse()) {
// If the relation is inverse, then referencedEntityName is not null.
- mappedBy = getMappedBy(value.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName));
- if (mappedBy == null) {
- throw new MappingException("Unable to read the mapped by attribute for " + name);
- }
+ mappedBy = getMappedBy(propertyValue.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName));
referencingPrefixRelated = mappedBy + "_";
referencedPrefix = referencedEntityName == null ? "element" : StringTools.getLastComponent(referencedEntityName);
} else {
mappedBy = null;
- referencingPrefixRelated = StringTools.getLastComponent(entityName) + "_";
- referencedPrefix = referencedEntityName == null ? "element" : name;
+ referencingPrefixRelated = StringTools.getLastComponent(referencingEntityName) + "_";
+ referencedPrefix = referencedEntityName == null ? "element" : propertyName;
}
// Storing the id data of the referencing entity: original mapper, prefixed mapper and entity name.
MiddleIdData referencingIdData = new MiddleIdData(
referencingIdMapping.getIdMapper(),
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefixRelated),
- entityName,
- mainGenerator.getVerEntCfg().getVersionsEntityName(entityName));
+ referencingEntityName,
+ mainGenerator.getVerEntCfg().getVersionsEntityName(referencingEntityName));
// Creating a query generator builder, to which additional id data will be added, in case this collection
// references some entities (either from the element or index). At the end, this will be used to build
@@ -241,34 +213,25 @@
// Adding the XML mapping for the referencing entity, if the relation isn't inverse.
if (middleEntityXml != null) {
- middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
-
// Adding related-entity (in this case: the referencing's entity id) id mapping to the xml.
- addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated,
- MetadataTools.getColumnNameIterator(value.getKey().getColumnIterator()),
+ addRelatedToXmlMapping(middleEntityXml, referencingPrefixRelated,
+ MetadataTools.getColumnNameIterator(propertyValue.getKey().getColumnIterator()),
referencingIdMapping);
-
- // Adding the revision number as a foreign key to the revision info entity to the composite id of the
- // middle table.
- mainGenerator.addRevisionInfoRelation(middleEntityXmlId);
-
- // Adding the revision type property to the entity xml.
- mainGenerator.addRevisionType(middleEntityXml);
}
// ******
// Generating the element mapping.
// ******
- MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId,
+ MiddleComponentData elementComponentData = addValueToMiddleTable(propertyValue.getElement(), middleEntityXml,
queryGeneratorBuilder, referencedPrefix, joinTable.inverseJoinColumns());
// ******
// Optionally, generating the index mapping.
// ******
MiddleComponentData indexComponentData;
- if (value instanceof IndexedCollection) {
- IndexedCollection indexedValue = (IndexedCollection) value;
- indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId,
+ if (propertyValue instanceof IndexedCollection) {
+ IndexedCollection indexedValue = (IndexedCollection) propertyValue;
+ indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml,
queryGeneratorBuilder, "mapkey", null);
// TODO: @MapKey support, @MapKeyManyToMany
} else {
@@ -280,58 +243,27 @@
// ******
// Generating the property mapper.
// ******
-
// Building the query generator.
MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(elementComponentData, indexComponentData);
// Creating common data
CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(
- mainGenerator.getVerEntCfg(), versionsMiddleEntityName, name, referencingIdData, queryGenerator);
+ mainGenerator.getVerEntCfg(), versionsMiddleEntityName, propertyName, referencingIdData, queryGenerator);
// Checking the type of the collection and adding an appropriate mapper.
- Type type = value.getType();
- if (type instanceof SortedSetType) {
- currentMapper.addComposite(name, new BasicCollectionMapper<Set>(commonCollectionMapperData,
- TreeSet.class, SortedSetProxy.class, elementComponentData));
- } else if (type instanceof SetType) {
- currentMapper.addComposite(name, new BasicCollectionMapper<Set>(commonCollectionMapperData,
- HashSet.class, SetProxy.class, elementComponentData));
- } else if (type instanceof SortedMapType) {
- // Indexed collection, so <code>indexComponentData</code> is not null.
- currentMapper.addComposite(name, new MapCollectionMapper<Map>(commonCollectionMapperData,
- TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData));
- } else if (type instanceof MapType) {
- // Indexed collection, so <code>indexComponentData</code> is not null.
- currentMapper.addComposite(name, new MapCollectionMapper<Map>(commonCollectionMapperData,
- HashMap.class, MapProxy.class, elementComponentData, indexComponentData));
- } else if (type instanceof BagType) {
- currentMapper.addComposite(name, new BasicCollectionMapper<List>(commonCollectionMapperData,
- ArrayList.class, ListProxy.class, elementComponentData));
- } else {
- throw new RuntimeException();
- }
+ addMapper(commonCollectionMapperData, elementComponentData, indexComponentData);
// ******
// Storing information about this relation.
// ******
-
- // Only if this is a relation (when there is a referenced entity).
- if (referencedEntityName != null) {
- if (value.isInverse()) {
- mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy,
- referencedEntityName);
- } else {
- mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name,
- referencedEntityName);
- }
- }
+ storeMiddleEntityRelationInformation(mappedBy);
}
/**
*
* @param value Value, which should be mapped to the middle-table, either as a relation to another entity,
* or as a simple value.
- * @param middleEntityXml If not <code>null</code>, xml mapping for this value is added to this element.
+ * @param xmlMapping If not <code>null</code>, xml mapping for this value is added to this element.
* @param queryGeneratorBuilder In case <code>value</code> is a relation to another entity, information about it
* should be added to the given.
* @param prefix Prefix for proeprty names of related entities identifiers.
@@ -339,7 +271,7 @@
* @return Data for mapping this component.
*/
@SuppressWarnings({"unchecked"})
- private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml,
+ private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMapping,
QueryGeneratorBuilder queryGeneratorBuilder,
String prefix, JoinColumn[] joinColumns) {
Type type = value.getType();
@@ -351,9 +283,9 @@
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>middleEntityXml</code> is not null).
- if (middleEntityXml != null) {
- addRelatedToXmlMapping(middleEntityXml, prefixRelated,
+ // relation isn't inverse (so when <code>xmlMapping</code> is not null).
+ if (xmlMapping != null) {
+ addRelatedToXmlMapping(xmlMapping, prefixRelated,
joinColumns != null && joinColumns.length > 0
? MetadataTools.getColumnNameIterator(joinColumns)
: MetadataTools.getColumnNameIterator(value.getColumnIterator()),
@@ -374,7 +306,7 @@
queryGeneratorBuilder.getCurrentIndex());
} else if (type instanceof ImmutableType || type instanceof MutableType) {
// TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true);
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(xmlMapping, prefix, value, null, ModificationStore.FULL, true);
// Simple values are always stored in the first entity read by the query generator.
return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
@@ -383,4 +315,107 @@
throw new RuntimeException();
}
}
+
+ private void addMapper(CommonCollectionMapperData commonCollectionMapperData, MiddleComponentData elementComponentData,
+ MiddleComponentData indexComponentData) {
+ Type type = propertyValue.getType();
+ if (type instanceof SortedSetType) {
+ currentMapper.addComposite(propertyName, new BasicCollectionMapper<Set>(commonCollectionMapperData,
+ TreeSet.class, SortedSetProxy.class, elementComponentData));
+ } else if (type instanceof SetType) {
+ currentMapper.addComposite(propertyName, new BasicCollectionMapper<Set>(commonCollectionMapperData,
+ HashSet.class, SetProxy.class, elementComponentData));
+ } else if (type instanceof SortedMapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not null.
+ currentMapper.addComposite(propertyName, new MapCollectionMapper<Map>(commonCollectionMapperData,
+ TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData));
+ } else if (type instanceof MapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not null.
+ currentMapper.addComposite(propertyName, new MapCollectionMapper<Map>(commonCollectionMapperData,
+ HashMap.class, MapProxy.class, elementComponentData, indexComponentData));
+ } else if (type instanceof BagType) {
+ currentMapper.addComposite(propertyName, new BasicCollectionMapper<List>(commonCollectionMapperData,
+ ArrayList.class, ListProxy.class, elementComponentData));
+ } else {
+ mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
+ }
+ }
+
+ private void storeMiddleEntityRelationInformation(String mappedBy) {
+ // Only if this is a relation (when there is a referenced entity).
+ if (referencedEntityName != null) {
+ if (propertyValue.isInverse()) {
+ referencingEntityConfiguration.addToManyMiddleNotOwningRelation(propertyName, mappedBy, referencedEntityName);
+ } else {
+ referencingEntityConfiguration.addToManyMiddleRelation(propertyName, referencedEntityName);
+ }
+ }
+ }
+
+ private Element createMiddleEntityXml(String versionsMiddleTableName, String versionsMiddleEntityName) {
+ String schema = StringTools.isEmpty(joinTable.schema()) ? propertyValue.getCollectionTable().getSchema() : joinTable.schema();
+ String catalog = StringTools.isEmpty(joinTable.catalog()) ? propertyValue.getCollectionTable().getCatalog() : joinTable.catalog();
+
+ Element middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(),
+ versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null);
+ Element middleEntityXmlId = middleEntityXml.addElement("composite-id");
+
+ middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
+
+ // Adding the revision number as a foreign key to the revision info entity to the composite id of the
+ // middle table.
+ mainGenerator.addRevisionInfoRelation(middleEntityXmlId);
+
+ // Adding the revision type property to the entity xml.
+ mainGenerator.addRevisionType(middleEntityXml);
+
+ // All other properties should also be part of the primary key of the middle entity.
+ return middleEntityXmlId;
+ }
+
+ private VersionsJoinTable getDefaultVersionsJoinTable() {
+ return new VersionsJoinTable() {
+ public String name() { return ""; }
+ public String schema() { return ""; }
+ public String catalog() { return ""; }
+ public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; }
+ public Class<? extends Annotation> annotationType() { return this.getClass(); }
+ };
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private String getMappedBy(Collection collectionValue) {
+ Iterator<Property> assocClassProps =
+ ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
+
+ while (assocClassProps.hasNext()) {
+ Property property = assocClassProps.next();
+
+ if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
+ collectionValue.getKey().getColumnIterator())) {
+ return property.getName();
+ }
+ }
+
+ throw new MappingException("Unable to read the mapped by attribute for " + propertyName + " in "
+ + referencingEntityName + "!");
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private String getMappedBy(Table collectionTable, PersistentClass referencedClass) {
+ Iterator<Property> properties = referencedClass.getPropertyIterator();
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ if (property.getValue() instanceof Collection) {
+ // The equality is intentional. We want to find a collection property with the same collection table.
+ //noinspection ObjectEquality
+ if (((Collection) property.getValue()).getCollectionTable() == collectionTable) {
+ return property.getName();
+ }
+ }
+ }
+
+ throw new MappingException("Unable to read the mapped by attribute for " + propertyName + " in "
+ + referencingEntityName + "!");
+ }
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160)
@@ -55,7 +55,6 @@
private final BasicMetadataGenerator basicMetadataGenerator;
private final IdMetadataGenerator idMetadataGenerator;
- private final CollectionMetadataGenerator collectionMetadataGenerator;
private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator;
private final Map<String, EntityConfiguration> entitiesConfigurations;
@@ -75,7 +74,6 @@
this.basicMetadataGenerator = new BasicMetadataGenerator(this);
this.idMetadataGenerator = new IdMetadataGenerator(this);
- this.collectionMetadataGenerator = new CollectionMetadataGenerator(this);
this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this);
entitiesConfigurations = new HashMap<String, EntityConfiguration>();
@@ -166,17 +164,12 @@
} else if (type instanceof CollectionType) {
// only second pass
if (!firstPass) {
- collectionMetadataGenerator.addCollection(name, (Collection) value, currentMapper, entityName,
- xmlMappingData, joinTable);
+ CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this,
+ name, (Collection) value, currentMapper, entityName, xmlMappingData, joinTable);
+ collectionMetadataGenerator.addCollection();
}
} else {
- String message = "Type not supported for versioning: " + type.getClass().getName() +
- ", on entity " + entityName + ", property '" + name + "'.";
- if (globalCfg.isWarnOnUnsupportedTypes()) {
- log.warn(message);
- } else {
- throw new MappingException(message);
- }
+ throwUnsupportedTypeException(type, entityName, name);
}
}
@@ -386,4 +379,14 @@
VersionsEntitiesConfiguration getVerEntCfg() {
return verEntCfg;
}
+
+ void throwUnsupportedTypeException(Type type, String entityName, String propertyName) {
+ String message = "Type not supported for versioning: " + type.getClass().getName() +
+ ", on entity " + entityName + ", property '" + propertyName + "'.";
+ if (globalCfg.isWarnOnUnsupportedTypes()) {
+ log.warn(message);
+ } else {
+ throw new MappingException(message);
+ }
+ }
}
Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-22 11:26:24 UTC (rev 159)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-22 15:33:44 UTC (rev 160)
@@ -1,84 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.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.initializor.Initializor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy;
-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> 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 if (Map.class.isAssignableFrom(collectionClass)) {
- value = new MapProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, HashMap.class));
- } else {
- throw new VersionsException("Unsupported versioned collection type: " + collectionClass.getName());
- }
-
- Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
- setter.set(obj, value, null);
- }
-}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-22 11:26:24 UTC (rev 159)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-22 15:33:44 UTC (rev 160)
@@ -25,9 +25,16 @@
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.entities.mapper.relation.lazy.OneToManyAttachedInitializor;
+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.MapProxy;
import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+import org.jboss.envers.exception.VersionsException;
import org.hibernate.collection.PersistentCollection;
+import org.hibernate.property.Getter;
+import org.hibernate.property.Setter;
import java.util.*;
import java.io.Serializable;
@@ -35,24 +42,54 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public class OneToManyAttachedMapper extends AbstractOneToManyMapper implements PropertyMapper {
+public class OneToManyAttachedMapper implements PropertyMapper {
+ private final String owningEntityName;
+ private final String propertyName;
private final String owningReferencePropertyName;
- public OneToManyAttachedMapper(String owningReferencePropertyName, String owningEntityName, String propertyName) {
- super(owningEntityName, propertyName);
-
+ public OneToManyAttachedMapper(String owningEntityName, String propertyName, String owningReferencePropertyName) {
+ this.owningEntityName = owningEntityName;
+ this.propertyName = propertyName;
this.owningReferencePropertyName = owningReferencePropertyName;
}
+ @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
+ // todo: add support for @MapKey, sorted collections
+ Object value;
+ if (List.class.isAssignableFrom(collectionClass)) {
+ value = new ListProxy(getInitializator(versionsReader, entityClass, primaryKey, revision, ArrayList.class));
+ } else if (Set.class.isAssignableFrom(collectionClass) || Collection.class.isAssignableFrom(collectionClass)) {
+ value = new SetProxy(getInitializator(versionsReader, entityClass, primaryKey, revision, HashSet.class));
+ } else if (Map.class.isAssignableFrom(collectionClass)) {
+ value = new MapProxy(getInitializator(versionsReader, entityClass, primaryKey, revision, HashMap.class));
+ } else {
+ throw new VersionsException("Unsupported versioned collection type: " + collectionClass.getName());
+ }
+
+ Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
+ setter.set(obj, value, null);
+ }
+
public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj, Object oldObj) {
return false;
}
- protected <T> Initializor<T> getInitializator(VersionsConfiguration verCfg,
- VersionsReaderImplementor versionsReader,
- Class<?> entityClass, Object primaryKey,
- Number revision,
- Class<T> collectionClass) {
+ protected <T> Initializor<T> getInitializator(VersionsReaderImplementor versionsReader,
+ Class<?> entityClass, Object primaryKey,
+ Number revision,
+ Class<T> collectionClass) {
return new OneToManyAttachedInitializor<T>(versionsReader, entityClass, owningReferencePropertyName, primaryKey,
revision, collectionClass);
}
16 years, 3 months
JBoss Envers SVN: r159 - in trunk/src: main/org/jboss/envers/configuration and 4 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 07:26:24 -0400 (Mon, 22 Sep 2008)
New Revision: 159
Added:
trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java
trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java
trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java
Modified:
trunk/src/main/org/jboss/envers/VersionsJoinTable.java
trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java
Log:
ENVERS-26: support for @VersionsJoinTable, and unidirectional one-to-many associations mapped with @OneToMany+@JoinColumn
Modified: trunk/src/main/org/jboss/envers/VersionsJoinTable.java
===================================================================
--- trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -50,8 +50,8 @@
String catalog() default "";
/**
- * @return The foreign key columns of the join table which reference the primary table of the entity owning the
- * association (i.e. the owning side of the association).
+ * @return The foreign key columns of the join table which reference the primary table of the entity that does not
+ * own the association (i.e. the inverse side of the association).
*/
- JoinColumn[] joinColumns() default {};
+ JoinColumn[] inverseJoinColumns() default {};
}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -28,6 +28,7 @@
import org.jboss.envers.configuration.metadata.EntityXmlMappingData;
import org.jboss.envers.tools.graph.GraphTopologicalSort;
import org.jboss.envers.tools.reflection.YReflectionManager;
+import org.jboss.envers.tools.StringTools;
import org.dom4j.io.DOMWriter;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
@@ -75,8 +76,8 @@
if (versioningData.isVersioned()) {
pcDatas.put(pc, versioningData);
- if (versioningData.versionsTableName != null) {
- verEntCfg.addCustomVersionsTableName(pc.getEntityName(), versioningData.versionsTableName);
+ if (!StringTools.isEmpty(versioningData.versionsTable.value())) {
+ verEntCfg.addCustomVersionsTableName(pc.getEntityName(), versioningData.versionsTable.value());
}
EntityXmlMappingData xmlMappingData = new EntityXmlMappingData();
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -31,6 +31,8 @@
import org.hibernate.mapping.PersistentClass;
import org.hibernate.MappingException;
+import java.lang.annotation.Annotation;
+
/**
* A helper class to read versioning meta-data from annotations on a persistent class.
* @author Adam Warski (adam at warski dot org)
@@ -117,9 +119,9 @@
private void addVersionsTable(YClass clazz) {
VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class);
if (versionsTable != null) {
- versioningData.versionsTableName = versionsTable.value();
- versioningData.schema = versionsTable.schema();
- versioningData.catalog = versionsTable.catalog();
+ versioningData.versionsTable = versionsTable;
+ } else {
+ versioningData.versionsTable = getDefaultVersionsTable();
}
}
@@ -158,4 +160,13 @@
return versioningData;
}
+
+ private VersionsTable getDefaultVersionsTable() {
+ return new VersionsTable() {
+ public String value() { return ""; }
+ public String schema() { return ""; }
+ public String catalog() { return ""; }
+ public Class<? extends Annotation> annotationType() { return this.getClass(); }
+ };
+ }
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -108,7 +108,7 @@
while (properties.hasNext()) {
Property property = properties.next();
mainGenerator.addValue(component_mapping, property.getName(), property.getValue(),
- componentMapper, ModificationStore.FULL, entityName, xmlMappingData, firstPass);
+ componentMapper, ModificationStore.FULL, entityName, xmlMappingData, null, firstPass);
}
}
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -17,12 +17,15 @@
import org.jboss.envers.tools.Tools;
import org.jboss.envers.tools.StringTools;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.VersionsJoinTable;
import org.dom4j.Element;
+import javax.persistence.JoinColumn;
import java.util.*;
import java.util.Set;
import java.util.List;
import java.util.Map;
+import java.lang.annotation.Annotation;
/**
* Generates metadata for collection-valued properties.
@@ -36,17 +39,29 @@
}
void addCollection(String name, Collection value, CompositeMapperBuilder currentMapper,
- String entityName, EntityXmlMappingData xmlMappingData) {
+ String entityName, EntityXmlMappingData xmlMappingData,
+ VersionsJoinTable joinTable) {
Type type = value.getType();
if ((type instanceof BagType || type instanceof SetType) &&
(value.getElement() instanceof OneToMany) && (value.isInverse())) {
addOneToManyAttached(name, value, currentMapper, entityName);
} else {
- addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData);
+ addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData,
+ joinTable == null ? getDefaultVersionsJoinTable() : joinTable);
}
}
+ private VersionsJoinTable getDefaultVersionsJoinTable() {
+ return new VersionsJoinTable() {
+ public String name() { return ""; }
+ public String schema() { return ""; }
+ public String catalog() { return ""; }
+ public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; }
+ public Class<? extends Annotation> annotationType() { return this.getClass(); }
+ };
+ }
+
@SuppressWarnings({"unchecked"})
private String getMappedBy(Collection collectionValue) {
Iterator<Property> assocClassProps =
@@ -113,7 +128,7 @@
private String getReferencedEntityName(Value value) {
if (value instanceof ToOne) {
return ((ToOne) value).getReferencedEntityName();
- } if (value instanceof OneToMany) {
+ } else if (value instanceof OneToMany) {
return ((OneToMany) value).getReferencedEntityName();
} else {
return null;
@@ -121,9 +136,11 @@
}
@SuppressWarnings({"unchecked"})
- private void addRelatedToXmlMapping(Element xmlMapping, String prefix, Value relatedValue, IdMappingData relatedIdMapping) {
+ private void addRelatedToXmlMapping(Element xmlMapping, String prefix,
+ MetadataTools.ColumnNameIterator columnNameIterator,
+ IdMappingData relatedIdMapping) {
Element properties = (Element) relatedIdMapping.getXmlRelationMapping().clone();
- MetadataTools.prefixNamesInPropertyElement(properties, prefix, relatedValue.getColumnIterator(), true);
+ MetadataTools.prefixNamesInPropertyElement(properties, prefix, columnNameIterator, true);
for (Element idProperty : (java.util.List<Element>) properties.elements()) {
xmlMapping.add((Element) idProperty.clone());
}
@@ -131,10 +148,9 @@
private String getMiddleTableName(Collection value, String entityName) {
// We check how Hibernate maps the collection.
- if (value.getKey().getTable().equals(value.getCollectionTable())) {
- // The table of the element is the same as the collection table - this must be a @JoinColumn+@OneToMany
- // mapping. Generating the table name ourselves, as Hibernate doesn't use a middle table for mapping
- // this relation.
+ if (value.getElement() instanceof OneToMany && !value.isInverse()) {
+ // This must be a @JoinColumn+@OneToMany mapping. Generating the table name, as Hibernate doesn't use a
+ // middle table for mapping this relation.
return StringTools.getLastComponent(entityName) + "_" + StringTools.getLastComponent(getReferencedEntityName(value.getElement()));
} else {
// Hibernate uses a middle table for mapping this relation, so we get it's name directly.
@@ -144,21 +160,28 @@
@SuppressWarnings({"unchecked"})
private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper,
- String entityName, EntityXmlMappingData xmlMappingData) {
- // Generating the name of the middle table, its schema and catalog
- // TODO: add support for @VersionsJoinTable
- String middleTableName = getMiddleTableName(value, entityName);//value.getCollectionTable().getName();
- String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
- String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName);
+ String entityName, EntityXmlMappingData xmlMappingData, VersionsJoinTable joinTable) {
+ // Generating the name of the middle table
- String schema = value.getCollectionTable().getSchema();
- String catalog = value.getCollectionTable().getCatalog();
+ String versionsMiddleTableName;
+ String versionsMiddleEntityName;
+ if (!StringTools.isEmpty(joinTable.name())) {
+ versionsMiddleTableName = joinTable.name();
+ versionsMiddleEntityName = joinTable.name();
+ } else {
+ String middleTableName = getMiddleTableName(value, entityName);
+ versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
+ versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName);
+ }
// Generating the XML mapping for the middle entity, only if the relation isn't inverse.
// If the relation is inverse, will be later checked by comparing middleEntityXml with null.
Element middleEntityXml;
Element middleEntityXmlId;
if (!value.isInverse()) {
+ String schema = StringTools.isEmpty(joinTable.schema()) ? value.getCollectionTable().getSchema() : joinTable.schema();
+ String catalog = StringTools.isEmpty(joinTable.catalog()) ? value.getCollectionTable().getCatalog() : joinTable.catalog();
+
middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(),
versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null);
middleEntityXmlId = middleEntityXml.addElement("composite-id");
@@ -221,7 +244,9 @@
middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
// Adding related-entity (in this case: the referencing's entity id) id mapping to the xml.
- addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, value.getKey(), referencingIdMapping);
+ addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated,
+ MetadataTools.getColumnNameIterator(value.getKey().getColumnIterator()),
+ referencingIdMapping);
// Adding the revision number as a foreign key to the revision info entity to the composite id of the
// middle table.
@@ -235,7 +260,7 @@
// Generating the element mapping.
// ******
MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId,
- queryGeneratorBuilder, referencedPrefix);
+ queryGeneratorBuilder, referencedPrefix, joinTable.inverseJoinColumns());
// ******
// Optionally, generating the index mapping.
@@ -244,8 +269,8 @@
if (value instanceof IndexedCollection) {
IndexedCollection indexedValue = (IndexedCollection) value;
indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId,
- queryGeneratorBuilder, "mapkey");
- // TODO
+ queryGeneratorBuilder, "mapkey", null);
+ // TODO: @MapKey support, @MapKeyManyToMany
} else {
// No index - creating a dummy mapper.
indexComponentData = new MiddleComponentData(new MiddleDummyComponentMapper(),
@@ -289,7 +314,7 @@
// ******
// Storing information about this relation.
// ******
-
+
// Only if this is a relation (when there is a referenced entity).
if (referencedEntityName != null) {
if (value.isInverse()) {
@@ -310,11 +335,13 @@
* @param queryGeneratorBuilder In case <code>value</code> is a relation to another entity, information about it
* should be added to the given.
* @param prefix Prefix for proeprty names of related entities identifiers.
+ * @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements.
* @return Data for mapping this component.
*/
+ @SuppressWarnings({"unchecked"})
private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml,
QueryGeneratorBuilder queryGeneratorBuilder,
- String prefix) {
+ String prefix, JoinColumn[] joinColumns) {
Type type = value.getType();
if (type instanceof ManyToOneType) {
String prefixRelated = prefix + "_";
@@ -326,7 +353,11 @@
// 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>middleEntityXml</code> is not null).
if (middleEntityXml != null) {
- addRelatedToXmlMapping(middleEntityXml, prefixRelated, value, referencedIdMapping);
+ addRelatedToXmlMapping(middleEntityXml, prefixRelated,
+ joinColumns != null && joinColumns.length > 0
+ ? MetadataTools.getColumnNameIterator(joinColumns)
+ : MetadataTools.getColumnNameIterator(value.getColumnIterator()),
+ referencedIdMapping);
}
// Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -27,6 +27,7 @@
import org.jboss.envers.tools.StringTools;
import org.hibernate.mapping.Column;
+import javax.persistence.JoinColumn;
import java.util.Iterator;
/**
@@ -104,8 +105,8 @@
}
public static Element createSubclassEntity(Document document, String entityName, String tableName,
- String schema, String catalog, String extendsEntityName,
- String discriminatorValue) {
+ String schema, String catalog, String extendsEntityName,
+ String discriminatorValue) {
Element class_mapping = createEntityCommon(document, "subclass", entityName, tableName, schema, catalog,
discriminatorValue);
@@ -137,9 +138,9 @@
addColumn(any_mapping, column.getName(), column.getLength());
}
}
-
+
@SuppressWarnings({"unchecked"})
- private static void changeNamesInColumnElement(Element element, Iterator<Column> columnIterator) {
+ private static void changeNamesInColumnElement(Element element, ColumnNameIterator columnNameIterator) {
Iterator<Element> properties = element.elementIterator();
while (properties.hasNext()) {
Element property = properties.next();
@@ -147,15 +148,15 @@
if ("column".equals(property.getName())) {
Attribute nameAttr = property.attribute("name");
if (nameAttr != null) {
- nameAttr.setText(columnIterator.next().getName());
+ nameAttr.setText(columnNameIterator.next());
}
}
}
}
@SuppressWarnings({"unchecked"})
- public static void prefixNamesInPropertyElement(Element element, String prefix, Iterator<Column> columnIterator,
- boolean changeToKey) {
+ public static void prefixNamesInPropertyElement(Element element, String prefix, ColumnNameIterator columnNameIterator,
+ boolean changeToKey) {
Iterator<Element> properties = element.elementIterator();
while (properties.hasNext()) {
Element property = properties.next();
@@ -166,7 +167,7 @@
nameAttr.setText(prefix + nameAttr.getText());
}
- changeNamesInColumnElement(property, columnIterator);
+ changeNamesInColumnElement(property, columnNameIterator);
if (changeToKey) {
property.setName("key-property");
@@ -174,4 +175,26 @@
}
}
}
+
+ /**
+ * An iterator over column names.
+ */
+ public static abstract class ColumnNameIterator implements Iterator<String> { }
+
+ public static ColumnNameIterator getColumnNameIterator(final Iterator<Column> columnIterator) {
+ return new ColumnNameIterator() {
+ public boolean hasNext() { return columnIterator.hasNext(); }
+ public String next() { return columnIterator.next().getName(); }
+ public void remove() { columnIterator.remove(); }
+ };
+ }
+
+ public static ColumnNameIterator getColumnNameIterator(final JoinColumn[] joinColumns) {
+ return new ColumnNameIterator() {
+ int counter = 0;
+ public boolean hasNext() { return counter < joinColumns.length; }
+ public String next() { return joinColumns[counter++].name(); }
+ public void remove() { throw new UnsupportedOperationException(); }
+ };
+ }
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -23,6 +23,7 @@
import org.jboss.envers.ModificationStore;
import org.jboss.envers.VersionsJoinTable;
+import org.jboss.envers.VersionsTable;
import java.util.HashMap;
import java.util.Map;
@@ -42,9 +43,7 @@
}
public PropertyStoreInfo propertyStoreInfo;
- public String versionsTableName;
- public String schema;
- public String catalog;
+ public VersionsTable versionsTable;
public Map<String, String> secondaryTableDictionary;
public List<String> unversionedProperties;
/**
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -46,7 +46,8 @@
Element properties = (Element) idMapping.getXmlRelationMapping().clone();
properties.addAttribute("name", name);
- MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, value.getColumnIterator(), false);
+ MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix,
+ MetadataTools.getColumnNameIterator(value.getColumnIterator()), false);
parent.add(properties);
// Adding mapper for the id
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -35,6 +35,7 @@
import org.jboss.envers.tools.StringTools;
import org.jboss.envers.tools.HibernateVersion;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.VersionsJoinTable;
import org.jboss.envers.tools.log.YLog;
import org.jboss.envers.tools.log.YLogManager;
@@ -120,7 +121,7 @@
@SuppressWarnings({"unchecked"})
void addValue(Element parent, String name, Value value, CompositeMapperBuilder currentMapper,
ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData,
- boolean firstPass) {
+ VersionsJoinTable joinTable, boolean firstPass) {
Type type = value.getType();
if (type instanceof ComponentType) {
@@ -166,7 +167,7 @@
// only second pass
if (!firstPass) {
collectionMetadataGenerator.addCollection(name, (Collection) value, currentMapper, entityName,
- xmlMappingData);
+ xmlMappingData, joinTable);
}
} else {
String message = "Type not supported for versioning: " + type.getClass().getName() +
@@ -181,16 +182,17 @@
@SuppressWarnings({"unchecked"})
private void addProperties(Element parent, Iterator<Property> properties, CompositeMapperBuilder currentMapper,
- PropertyStoreInfo propertyStoreInfo, String entityName, EntityXmlMappingData xmlMappingData,
- List<String> unversionedProperties, boolean firstPass) {
+ PersistentClassVersioningData versioningData, String entityName, EntityXmlMappingData xmlMappingData,
+ boolean firstPass) {
while (properties.hasNext()) {
Property property = properties.next();
if (!"_identifierMapper".equals(property.getName())) {
- ModificationStore store = getStoreForProperty(property, propertyStoreInfo, unversionedProperties);
+ ModificationStore store = getStoreForProperty(property, versioningData.propertyStoreInfo,
+ versioningData.unversionedProperties);
if (store != null) {
addValue(parent, property.getName(), property.getValue(), currentMapper, store, entityName,
- xmlMappingData, firstPass);
+ xmlMappingData, versioningData.versionsJoinTables.get(property.getName()), firstPass);
}
}
}
@@ -214,12 +216,12 @@
versionedTableName = verEntCfg.getVersionsEntityName(originalTableName);
}
- String schema = versioningData.schema;
+ String schema = versioningData.versionsTable.schema();
if (StringTools.isEmpty(schema)) {
schema = join.getTable().getSchema();
}
- String catalog = versioningData.catalog;
+ String catalog = versioningData.versionsTable.catalog();
if (StringTools.isEmpty(catalog)) {
catalog = join.getTable().getCatalog();
}
@@ -234,17 +236,16 @@
}
@SuppressWarnings({"unchecked"})
- private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, PropertyStoreInfo propertyStoreInfo,
- String entityName, EntityXmlMappingData xmlMappingData, List<String> unversionedProperties,
- boolean firstPass) {
+ private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, PersistentClassVersioningData versioningData,
+ String entityName, EntityXmlMappingData xmlMappingData,boolean firstPass) {
Iterator<Join> joins = pc.getJoinIterator();
while (joins.hasNext()) {
Join join = joins.next();
Element joinElement = entitiesJoins.get(entityName).get(join);
- addProperties(joinElement, join.getPropertyIterator(), currentMapper, propertyStoreInfo, entityName,
- xmlMappingData, unversionedProperties, firstPass);
+ addProperties(joinElement, join.getPropertyIterator(), currentMapper, versioningData, entityName,
+ xmlMappingData, firstPass);
}
}
@@ -264,12 +265,12 @@
@SuppressWarnings({"unchecked"})
public void generateFirstPass(PersistentClass pc, PersistentClassVersioningData versioningData,
EntityXmlMappingData xmlMappingData) {
- String schema = versioningData.schema;
+ String schema = versioningData.versionsTable.schema();
if (StringTools.isEmpty(schema)) {
schema = pc.getTable().getSchema();
}
- String catalog = versioningData.catalog;
+ String catalog = versioningData.versionsTable.catalog();
if (StringTools.isEmpty(catalog)) {
catalog = pc.getTable().getCatalog();
}
@@ -335,13 +336,12 @@
// Mapping unjoined properties
addProperties(class_mapping, (Iterator<Property>) pc.getUnjoinedPropertyIterator(), propertyMapper,
- versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData, versioningData.unversionedProperties,
+ versioningData, pc.getEntityName(), xmlMappingData,
true);
// Creating and mapping joins (first pass)
createJoins(pc, class_mapping, versioningData);
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData,
- versioningData.unversionedProperties, true);
+ addJoins(pc, propertyMapper, versioningData, pc.getEntityName(), xmlMappingData, true);
// Storing the generated configuration
EntityConfiguration entityCfg = new EntityConfiguration(entityName, versionsEntityName, idMapper,
@@ -363,12 +363,10 @@
}
addProperties(parent, (Iterator<Property>) pc.getUnjoinedPropertyIterator(),
- propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData,
- versioningData.unversionedProperties, false);
+ propertyMapper, versioningData, entityName, xmlMappingData, false);
// Mapping joins (second pass)
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData,
- versioningData.unversionedProperties, false);
+ addJoins(pc, propertyMapper, versioningData, entityName, xmlMappingData, false);
}
public Map<String, EntityConfiguration> getEntitiesConfigurations() {
Copied: trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetRefCollEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -0,0 +1,87 @@
+package org.jboss.envers.test.entities.onetomany.detached;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.JoinColumn;
+import java.util.Set;
+
+/**
+ * A detached relation to another entity, with a @OneToMany+@JoinColumn mapping.
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class SetJoinColumnRefCollEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @OneToMany
+ @JoinColumn(name = "SJCR_ID")
+ private Set<StrTestEntity> collection;
+
+ public SetJoinColumnRefCollEntity() {
+ }
+
+ public SetJoinColumnRefCollEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public SetJoinColumnRefCollEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Set<StrTestEntity> getCollection() {
+ return collection;
+ }
+
+ public void setCollection(Set<StrTestEntity> collection) {
+ this.collection = collection;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof SetJoinColumnRefCollEntity)) return false;
+
+ SetJoinColumnRefCollEntity that = (SetJoinColumnRefCollEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "SetJoinColumnRefCollEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-22 09:48:53 UTC (rev 158)
+++ trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -75,11 +75,10 @@
assert "data1".equals(rev2.getData());
}
- private final static String MIDDLE_VERSIONS_ENTITY_NAME =
- "UNI_NAMING_TEST_versions";
+ private final static String MIDDLE_VERSIONS_ENTITY_NAME = "UNI_NAMING_TEST_versions";
@Test
public void testTableName() {
- assert "UNI_NAMING_TEST_versions".equals(
+ assert MIDDLE_VERSIONS_ENTITY_NAME.equals(
getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getName());
}
Copied: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java (from rev 152, trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -0,0 +1,108 @@
+package org.jboss.envers.test.integration.naming;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.mapping.Column;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class VersionsJoinTableNaming extends AbstractEntityTest {
+ private Integer uni1_id;
+ private Integer str1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(VersionsJoinTableTestEntity.class);
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ VersionsJoinTableTestEntity uni1 = new VersionsJoinTableTestEntity(1, "data1");
+ StrTestEntity str1 = new StrTestEntity("str1");
+
+ // Revision 1
+ EntityManager em = getEntityManager();
+ em.getTransaction().begin();
+
+ uni1.setCollection(new HashSet<StrTestEntity>());
+ em.persist(uni1);
+ em.persist(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ uni1 = em.find(VersionsJoinTableTestEntity.class, uni1.getId());
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ uni1.getCollection().add(str1);
+
+ em.getTransaction().commit();
+
+ //
+
+ uni1_id = uni1.getId();
+ str1_id = str1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(VersionsJoinTableTestEntity.class, uni1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ }
+
+ @Test
+ public void testHistoryOfUniId1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+
+ VersionsJoinTableTestEntity rev1 = getVersionsReader().find(VersionsJoinTableTestEntity.class, uni1_id, 1);
+ VersionsJoinTableTestEntity rev2 = getVersionsReader().find(VersionsJoinTableTestEntity.class, uni1_id, 2);
+
+ assert rev1.getCollection().equals(TestTools.makeSet());
+ assert rev2.getCollection().equals(TestTools.makeSet(str1));
+
+ assert "data1".equals(rev1.getData());
+ assert "data1".equals(rev2.getData());
+ }
+
+ private final static String MIDDLE_VERSIONS_ENTITY_NAME = "VERSIONS_JOIN_TABLE_TEST";
+
+ @Test
+ public void testTableName() {
+ assert MIDDLE_VERSIONS_ENTITY_NAME.equals(
+ getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getName());
+ }
+
+ @SuppressWarnings({"unchecked"})
+ @Test
+ public void testJoinColumnName() {
+ Iterator<Column> columns =
+ getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getColumnIterator();
+
+ boolean id1Found = false;
+ boolean id2Found = false;
+
+ while (columns.hasNext()) {
+ Column column = columns.next();
+ if ("VJT_ID".equals(column.getName())) {
+ id1Found = true;
+ }
+
+ if ("STR_ID".equals(column.getName())) {
+ id2Found = true;
+ }
+ }
+
+ assert id1Found && id2Found;
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/naming/DetachedNamingTestEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -0,0 +1,85 @@
+package org.jboss.envers.test.integration.naming;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.VersionsJoinTable;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.*;
+import java.util.Set;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class VersionsJoinTableTestEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @OneToMany
+ @JoinColumn(name = "VJT_ID")
+ @VersionsJoinTable(name = "VERSIONS_JOIN_TABLE_TEST", inverseJoinColumns = @JoinColumn(name = "STR_ID"))
+ private Set<StrTestEntity> collection;
+
+ public VersionsJoinTableTestEntity() {
+ }
+
+ public VersionsJoinTableTestEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public VersionsJoinTableTestEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Set<StrTestEntity> getCollection() {
+ return collection;
+ }
+
+ public void setCollection(Set<StrTestEntity> collection) {
+ this.collection = collection;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof VersionsJoinTableTestEntity)) return false;
+
+ VersionsJoinTableTestEntity that = (VersionsJoinTableTestEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "VersionsJoinTableTestEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java (from rev 156, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java 2008-09-22 11:26:24 UTC (rev 159)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.entities.onetomany.detached.SetJoinColumnRefCollEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicJoinColumnSet extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(SetJoinColumnRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ SetJoinColumnRefCollEntity coll1 = new SetJoinColumnRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<StrTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetJoinColumnRefCollEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ SetJoinColumnRefCollEntity rev1 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 1);
+ SetJoinColumnRefCollEntity rev2 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 2);
+ SetJoinColumnRefCollEntity rev3 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 3);
+ SetJoinColumnRefCollEntity rev4 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 4);
+
+ assert rev1.getCollection().equals(TestTools.makeSet(str1));
+ assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
+ assert rev3.getCollection().equals(TestTools.makeSet(str2));
+ assert rev4.getCollection().equals(TestTools.makeSet());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java
___________________________________________________________________
Name: svn:mergeinfo
+
16 years, 3 months
JBoss Envers SVN: r158 - in trunk/src/main/org/jboss/envers: configuration and 1 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 05:48:53 -0400 (Mon, 22 Sep 2008)
New Revision: 158
Added:
trunk/src/main/org/jboss/envers/VersionsJoinTable.java
Modified:
trunk/src/main/org/jboss/envers/VersionsTable.java
trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
Log:
ENVERS-26: adding reading of the VersionsJoinTable annotation, restructuring AnnotationMetadataReader from stateless to stateful
Copied: trunk/src/main/org/jboss/envers/VersionsJoinTable.java (from rev 148, trunk/src/main/org/jboss/envers/SecondaryVersionsTable.java)
===================================================================
--- trunk/src/main/org/jboss/envers/VersionsJoinTable.java (rev 0)
+++ trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 09:48:53 UTC (rev 158)
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+import javax.persistence.JoinColumn;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target({ElementType.FIELD, ElementType.METHOD})
+public @interface VersionsJoinTable {
+ /**
+ * @return Name of the join table. Defaults to a concatenation of the names of the primary table of the entity
+ * owning the association and of the primary table of the entity referenced by the association.
+ */
+ String name() default "";
+
+ /**
+ * @return The schema of the join table. Defaults to the schema of the entity owning the association.
+ */
+ String schema() default "";
+
+ /**
+ * @return The catalog of the join table. Defaults to the catalog of the entity owning the association.
+ */
+ String catalog() default "";
+
+ /**
+ * @return The foreign key columns of the join table which reference the primary table of the entity owning the
+ * association (i.e. the owning side of the association).
+ */
+ JoinColumn[] joinColumns() default {};
+}
\ No newline at end of file
Property changes on: trunk/src/main/org/jboss/envers/VersionsJoinTable.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: trunk/src/main/org/jboss/envers/VersionsTable.java
===================================================================
--- trunk/src/main/org/jboss/envers/VersionsTable.java 2008-09-22 09:12:36 UTC (rev 157)
+++ trunk/src/main/org/jboss/envers/VersionsTable.java 2008-09-22 09:48:53 UTC (rev 158)
@@ -29,19 +29,18 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-@SuppressWarnings({"JavaDoc"})
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface VersionsTable {
String value();
/**
- * The schema of the table. Defaults to the schema of the annotated entity.
+ * @return The schema of the table. Defaults to the schema of the annotated entity.
*/
String schema() default "";
/**
- * The catalog of the table. Defaults to the catalog of the annotated entity.
+ * @return The catalog of the table. Defaults to the catalog of the annotated entity.
*/
String catalog() default "";
}
Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:12:36 UTC (rev 157)
+++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:48:53 UTC (rev 158)
@@ -65,12 +65,12 @@
Map<PersistentClass, EntityXmlMappingData> xmlMappings = new HashMap<PersistentClass, EntityXmlMappingData>();
// First pass
- AnnotationsMetadataReader annotationsMetadataReader = new AnnotationsMetadataReader();
while (classes.hasNext()) {
PersistentClass pc = classes.next();
// Collecting information from annotations on the persistent class pc
- PersistentClassVersioningData versioningData =
- annotationsMetadataReader.getVersioningData(pc, reflectionManager, globalCfg);
+ AnnotationsMetadataReader annotationsMetadataReader =
+ new AnnotationsMetadataReader(globalCfg, reflectionManager, pc);
+ PersistentClassVersioningData versioningData = annotationsMetadataReader.getVersioningData();
if (versioningData.isVersioned()) {
pcDatas.put(pc, versioningData);
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:12:36 UTC (rev 157)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:48:53 UTC (rev 158)
@@ -32,87 +32,126 @@
import org.hibernate.MappingException;
/**
- * A helper class to read versioning meta-data from annotations on persistent classes.
+ * A helper class to read versioning meta-data from annotations on a persistent class.
* @author Adam Warski (adam at warski dot org)
* @author Sebastian Komander
*/
-public class AnnotationsMetadataReader {
- private void addFromProperties(Iterable<YProperty> properties, PersistentClassVersioningData versioningData,
- GlobalConfiguration globalCfg) {
- for (YProperty property : properties) {
- Versioned ver = property.getAnnotation(Versioned.class);
- if (ver != null) {
- versioningData.propertyStoreInfo.propertyStores.put(property.getName(), ver.modStore());
- }
+public final class AnnotationsMetadataReader {
+ private final GlobalConfiguration globalCfg;
+ private final YReflectionManager reflectionManager;
+ private final PersistentClass pc;
- // check if a property is declared as unversioned to exclude it
- // useful if a class is versioned but some properties should be excluded
- Unversioned unVer = property.getAnnotation(Unversioned.class);
- if (unVer != null) {
- versioningData.unversionedProperties.add(property.getName());
- } else {
- // if the optimistic locking field has to be unversioned and the current property
- // is the optimistic locking field, add it to the unversioned properties list
- if (globalCfg.isUnversionedOptimisticLockingField()) {
- Version jpaVer = property.getAnnotation(Version.class);
- if (jpaVer != null) {
- versioningData.unversionedProperties.add(property.getName());
- }
- }
+ /**
+ * This object is filled with information read from annotations and returned by the <code>getVersioningData</code>
+ * method.
+ */
+ private final PersistentClassVersioningData versioningData;
+
+ public AnnotationsMetadataReader(GlobalConfiguration globalCfg, YReflectionManager reflectionManager,
+ PersistentClass pc) {
+ this.globalCfg = globalCfg;
+ this.reflectionManager = reflectionManager;
+ this.pc = pc;
+
+ versioningData = new PersistentClassVersioningData();
+ }
+
+ private void addPropertyVersioned(YProperty property) {
+ Versioned ver = property.getAnnotation(Versioned.class);
+ if (ver != null) {
+ versioningData.propertyStoreInfo.propertyStores.put(property.getName(), ver.modStore());
+ }
+ }
+
+ 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
+ Unversioned unVer = property.getAnnotation(Unversioned.class);
+ if (unVer != null) {
+ versioningData.unversionedProperties.add(property.getName());
+ } else {
+ // if the optimistic locking field has to be unversioned and the current property
+ // is the optimistic locking field, add it to the unversioned properties list
+ if (globalCfg.isUnversionedOptimisticLockingField()) {
+ Version jpaVer = property.getAnnotation(Version.class);
+ if (jpaVer != null) {
+ versioningData.unversionedProperties.add(property.getName());
+ }
}
}
}
- private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData versioningData,
- GlobalConfiguration globalCfg) {
+ private void addPropertyJoinTables(YProperty property) {
+ VersionsJoinTable joinTable = property.getAnnotation(VersionsJoinTable.class);
+ if (joinTable != null) {
+ versioningData.versionsJoinTables.put(property.getName(), joinTable);
+ }
+ }
+
+ private void addFromProperties(Iterable<YProperty> properties) {
+ for (YProperty property : properties) {
+ addPropertyVersioned(property);
+ addPropertyUnversioned(property);
+ addPropertyJoinTables(property);
+ }
+ }
+
+ private void addPropertiesFromClass(YClass clazz) {
YClass superclazz = clazz.getSuperclass();
if (!"java.lang.Object".equals(superclazz.getName())) {
- addPropertiesFromClass(superclazz, versioningData, globalCfg);
+ addPropertiesFromClass(superclazz);
}
- addFromProperties(clazz.getDeclaredProperties("field"), versioningData, globalCfg);
- addFromProperties(clazz.getDeclaredProperties("property"), versioningData, globalCfg);
+ addFromProperties(clazz.getDeclaredProperties("field"));
+ addFromProperties(clazz.getDeclaredProperties("property"));
}
- public PersistentClassVersioningData getVersioningData(PersistentClass pc, YReflectionManager reflectionManager,
- GlobalConfiguration globalCfg) {
- PersistentClassVersioningData versioningData = new PersistentClassVersioningData();
+ private void addDefaultVersioned(YClass clazz) {
+ Versioned defaultVersioned = clazz.getAnnotation(Versioned.class);
+ if (defaultVersioned != null) {
+ versioningData.propertyStoreInfo.defaultStore = defaultVersioned.modStore();
+ }
+ }
+
+ private void addVersionsTable(YClass clazz) {
+ VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class);
+ if (versionsTable != null) {
+ versioningData.versionsTableName = versionsTable.value();
+ versioningData.schema = versionsTable.schema();
+ versioningData.catalog = versionsTable.catalog();
+ }
+ }
+
+ private void addVersionsSecondaryTables(YClass clazz) {
+ // Getting information on secondary tables
+ SecondaryVersionsTable secondaryVersionsTable1 = clazz.getAnnotation(SecondaryVersionsTable.class);
+ if (secondaryVersionsTable1 != null) {
+ versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(),
+ secondaryVersionsTable1.secondaryVersionsTableName());
+ }
+
+ SecondaryVersionsTables secondaryVersionsTables = clazz.getAnnotation(SecondaryVersionsTables.class);
+ if (secondaryVersionsTables != null) {
+ for (SecondaryVersionsTable secondaryVersionsTable2 : secondaryVersionsTables.value()) {
+ versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(),
+ secondaryVersionsTable2.secondaryVersionsTableName());
+ }
+ }
+ }
+
+ public PersistentClassVersioningData getVersioningData() {
if (pc.getClassName() == null) {
return versioningData;
}
try {
YClass clazz = reflectionManager.classForName(pc.getClassName(), this.getClass());
- Versioned defaultVersioned = clazz.getAnnotation(Versioned.class);
- if (defaultVersioned != null) {
- versioningData.propertyStoreInfo.defaultStore = defaultVersioned.modStore();
- }
-
- addPropertiesFromClass(clazz, versioningData, globalCfg);
-
- VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class);
- if (versionsTable != null) {
- versioningData.versionsTableName = versionsTable.value();
- versioningData.schema = versionsTable.schema();
- versioningData.catalog = versionsTable.catalog();
- }
-
- // Getting information on secondary tables
- SecondaryVersionsTable secondaryVersionsTable1 = clazz.getAnnotation(SecondaryVersionsTable.class);
- if (secondaryVersionsTable1 != null) {
- versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(),
- secondaryVersionsTable1.secondaryVersionsTableName());
- }
-
- SecondaryVersionsTables secondaryVersionsTables = clazz.getAnnotation(SecondaryVersionsTables.class);
- if (secondaryVersionsTables != null) {
- for (SecondaryVersionsTable secondaryVersionsTable2 : secondaryVersionsTables.value()) {
- versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(),
- secondaryVersionsTable2.secondaryVersionsTableName());
- }
- }
+ addDefaultVersioned(clazz);
+ addPropertiesFromClass(clazz);
+ addVersionsTable(clazz);
+ addVersionsSecondaryTables(clazz);
} catch (ClassNotFoundException e) {
throw new MappingException(e);
}
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:12:36 UTC (rev 157)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:48:53 UTC (rev 158)
@@ -22,6 +22,7 @@
package org.jboss.envers.configuration.metadata;
import org.jboss.envers.ModificationStore;
+import org.jboss.envers.VersionsJoinTable;
import java.util.HashMap;
import java.util.Map;
@@ -37,6 +38,7 @@
propertyStoreInfo = new PropertyStoreInfo(new HashMap<String, ModificationStore>());
secondaryTableDictionary = new HashMap<String, String>();
unversionedProperties = new ArrayList<String>();
+ versionsJoinTables = new HashMap<String, VersionsJoinTable>();
}
public PropertyStoreInfo propertyStoreInfo;
@@ -45,6 +47,10 @@
public String catalog;
public Map<String, String> secondaryTableDictionary;
public List<String> unversionedProperties;
+ /**
+ * A map from property names to custom join tables definitions.
+ */
+ public Map<String, VersionsJoinTable> versionsJoinTables;
public boolean isVersioned() {
if (propertyStoreInfo.propertyStores.size() > 0) { return true; }
16 years, 3 months
JBoss Envers SVN: r157 - in trunk/src: main/org/jboss/envers/entities/mapper/relation and 3 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-22 05:12:36 -0400 (Mon, 22 Sep 2008)
New Revision: 157
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java
Log:
ENVERS-26: intial support for @OneToMany+@JoinTable
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 22:00:50 UTC (rev 156)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -40,7 +40,7 @@
Type type = value.getType();
if ((type instanceof BagType || type instanceof SetType) &&
- (value.getElement() instanceof OneToMany)) {
+ (value.getElement() instanceof OneToMany) && (value.isInverse())) {
addOneToManyAttached(name, value, currentMapper, entityName);
} else {
addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData);
@@ -110,9 +110,11 @@
name));
}
- private String getReferencedEntityName(Collection value) {
- if (value.getElement() instanceof ToOne) {
- return ((ToOne) value.getElement()).getReferencedEntityName();
+ private String getReferencedEntityName(Value value) {
+ if (value instanceof ToOne) {
+ return ((ToOne) value).getReferencedEntityName();
+ } if (value instanceof OneToMany) {
+ return ((OneToMany) value).getReferencedEntityName();
} else {
return null;
}
@@ -127,15 +129,27 @@
}
}
+ private String getMiddleTableName(Collection value, String entityName) {
+ // We check how Hibernate maps the collection.
+ if (value.getKey().getTable().equals(value.getCollectionTable())) {
+ // The table of the element is the same as the collection table - this must be a @JoinColumn+@OneToMany
+ // mapping. Generating the table name ourselves, as Hibernate doesn't use a middle table for mapping
+ // this relation.
+ return StringTools.getLastComponent(entityName) + "_" + StringTools.getLastComponent(getReferencedEntityName(value.getElement()));
+ } else {
+ // Hibernate uses a middle table for mapping this relation, so we get it's name directly.
+ return value.getCollectionTable().getName();
+ }
+ }
+
@SuppressWarnings({"unchecked"})
private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper,
String entityName, EntityXmlMappingData xmlMappingData) {
// Generating the name of the middle table, its schema and catalog
// TODO: add support for @VersionsJoinTable
- String middleTableName = value.getCollectionTable().getName();
- String middleEntityName = middleTableName;
+ String middleTableName = getMiddleTableName(value, entityName);//value.getCollectionTable().getName();
String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName);
- String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName);
+ String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName);
String schema = value.getCollectionTable().getSchema();
String catalog = value.getCollectionTable().getCatalog();
@@ -165,7 +179,7 @@
IdMappingData referencingIdMapping = referencingConfiguration.getIdMappingData();
// Null if this collection doesn't reference an entity.
- String referencedEntityName = getReferencedEntityName(value);
+ String referencedEntityName = getReferencedEntityName(value.getElement());
// Only valid for an inverse relation; null otherwise.
String mappedBy;
@@ -305,10 +319,9 @@
if (type instanceof ManyToOneType) {
String prefixRelated = prefix + "_";
- ToOne toOneValue = (ToOne) value;
- String referencedEntityName = toOneValue.getReferencedEntityName();
+ String referencedEntityName = getReferencedEntityName(value);
IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get(
- toOneValue.getReferencedEntityName()).getIdMappingData();
+ 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>middleEntityXml</code> is not null).
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-21 22:00:50 UTC (rev 156)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -8,9 +8,21 @@
* @author Adam Warski (adam at warski dot org)
*/
public final class MiddleIdData {
+ /**
+ * Original id mapper of the related entity.
+ */
private final IdMapper originalMapper;
+ /**
+ * Prefixed id mapper (with the names for the id fields that are used in the middle table) of the related entity.
+ */
private final IdMapper prefixedMapper;
+ /**
+ * Name of the related entity.
+ */
private final String entityName;
+ /**
+ * Versions name of the related entity.
+ */
private final String versionsEntityName;
public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String entityName, String versionsEntityName) {
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-21 22:00:50 UTC (rev 156)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -20,7 +20,7 @@
Number revision);
/**
- * Maps from an object to the object's representation (for an entity - only its id).
+ * Maps from an object to the object's map representation (for an entity - only its id).
* @param data Map to which data should be added.
* @param obj Object to map from.
*/
Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-21 22:00:50 UTC (rev 156)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -1,78 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.entities.mapper.relation.lazy.initializor.Initializor;
-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 DetachedRelationInitializor<T> implements Initializor<T> {
- private final VersionsConfiguration verCfg;
- private final String entityName;
- //private final DetachedRelationQueryGenerator queryGenerator;
- private final VersionsReaderImplementor versionsReader;
- private final Object primaryKey;
- private final Number revision;
- private final Class<T> collectionClass;
-
- public DetachedRelationInitializor(VersionsConfiguration verCfg, String entityName,
- //DetachedRelationQueryGenerator 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 = null;//queryGenerator.getQuery(versionsReader, primaryKey, revision).list();
-
- T result;
- try {
- result = collectionClass.newInstance();
- } catch (Exception e) {
- throw new VersionsException(e);
- }
-
- // TODO
- entityInstantiator.addInstancesFromVersionsEntities(entityName, (Collection) result, queryResult, revision);
-
- return result;
- }
-}
Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -1,81 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.*;
-import java.util.List;
-
-/**
- * Entity owning the many-to-many relation
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class ListOwningUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- @Versioned
- @ManyToMany
- private List<ListOwnedUniEntity> references;
-
- public ListOwningUniEntity() { }
-
- public ListOwningUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public ListOwningUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public List<ListOwnedUniEntity> getReferences() {
- return references;
- }
-
- public void setReferences(List<ListOwnedUniEntity> references) {
- this.references = references;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof ListOwningUniEntity)) return false;
-
- ListOwningUniEntity that = (ListOwningUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwningEntity(id = " + id + ", data = " + data + ")";
- }
-}
\ No newline at end of file
Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-22 09:12:36 UTC (rev 157)
@@ -1,81 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.*;
-import java.util.Set;
-
-/**
- * Entity owning the many-to-many relation
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class SetOwningUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- @Versioned
- @ManyToMany
- private Set<SetOwnedUniEntity> references;
-
- public SetOwningUniEntity() { }
-
- public SetOwningUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public SetOwningUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public Set<SetOwnedUniEntity> getReferences() {
- return references;
- }
-
- public void setReferences(Set<SetOwnedUniEntity> references) {
- this.references = references;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SetOwningUniEntity)) return false;
-
- SetOwningUniEntity that = (SetOwningUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwningUniEntity(id = " + id + ", data = " + data + ")";
- }
-}
\ No newline at end of file
16 years, 3 months
JBoss Envers SVN: r156 - in trunk/src: main/org/jboss/envers/entities/mapper and 8 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-21 18:00:50 -0400 (Sun, 21 Sep 2008)
New Revision: 156
Added:
trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java
trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java
trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java
Removed:
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java
trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java
trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java
trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java
Log:
ENVERS-25: support for many-to-many relations with map-valued properties; mapping from a simple value. Tests name refactorings to clarify meaning.
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -332,8 +332,8 @@
// TODO: add support for enums, components, custom types
mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true);
- return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix),
- queryGeneratorBuilder.getCurrentIndex());
+ // Simple values are always stored in the first entity read by the query generator.
+ return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
} else {
// TODO: throw an exception
throw new RuntimeException();
Modified: trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -33,12 +33,12 @@
public class PersistentCollectionChangeData {
private final String entityName;
private final Map<String, Object> data;
- private final Object obj;
+ private final Object changedElement;
- public PersistentCollectionChangeData(String entityName, Map<String, Object> data, Object obj) {
+ public PersistentCollectionChangeData(String entityName, Map<String, Object> data, Object changedElement) {
this.entityName = entityName;
this.data = data;
- this.obj = obj;
+ this.changedElement = changedElement;
}
/**
@@ -54,10 +54,10 @@
}
/**
- *
- * @return The affected object, which was changed (added, removed, modified) in the collection.
+ * For use by bi-directional associations.
+ * @return The affected element, which was changed (added, removed, modified) in the collection.
*/
- public Object getObj() {
- return obj;
+ public Object getChangedElement() {
+ return changedElement;
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -39,6 +39,7 @@
protected abstract Collection getNewCollectionContent(PersistentCollection newCollection);
protected abstract Collection getOldCollectionContent(Serializable oldCollection);
+ protected abstract Object getElement(Object changedObject);
/**
* Maps the changed collection element to the given map.
@@ -55,7 +56,7 @@
entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(), originalId);
collectionChanges.add(new PersistentCollectionChangeData(
- commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, changedObj));
+ commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, getElement(changedObj)));
// Mapping the collection owner's id.
commonCollectionMapperData.getReferencingIdData().getPrefixedMapper().mapToMapFromId(originalId, id);
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -47,4 +47,8 @@
protected void mapToMapFromObject(Map<String, Object> data, Object changed) {
elementComponentData.getComponentMapper().mapToMapFromObject(data, changed);
}
+
+ protected Object getElement(Object changedObject) {
+ return changedObject;
+ }
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -52,4 +52,8 @@
elementComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getValue());
indexComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getKey());
}
+
+ protected Object getElement(Object changedObject) {
+ return ((Map.Entry) changedObject).getValue();
+ }
}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java
===================================================================
--- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -140,7 +140,7 @@
IdMapper relatedIdMapper = verCfg.getEntCfg().get(relatedEntityName).getIdMapper();
for (PersistentCollectionChangeData changeData : workUnit.getCollectionChanges()) {
- Object relatedObj = changeData.getObj();
+ Object relatedObj = changeData.getChangedElement();
Serializable relatedId = (Serializable) relatedIdMapper.mapToIdFromEntity(relatedObj);
verSync.addWorkUnit(new CollectionChangeWorkUnit(relatedEntityName, verCfg, relatedId, relatedObj));
Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/SetOwnedEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,84 @@
+package org.jboss.envers.test.entities.manytomany;
+
+import org.jboss.envers.Versioned;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import java.util.Set;
+
+/**
+ * Many-to-many not-owning entity
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class MapOwnedEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @ManyToMany(mappedBy="references")
+ private Set<MapOwningEntity> referencing;
+
+ public MapOwnedEntity() {
+ }
+
+ public MapOwnedEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public MapOwnedEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Set<MapOwningEntity> getReferencing() {
+ return referencing;
+ }
+
+ public void setReferencing(Set<MapOwningEntity> referencing) {
+ this.referencing = referencing;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof MapOwnedEntity)) return false;
+
+ MapOwnedEntity that = (MapOwnedEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "MapOwnedEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/ListOwningEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,82 @@
+package org.jboss.envers.test.entities.manytomany;
+
+import org.jboss.envers.Versioned;
+
+import javax.persistence.*;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Entity owning the many-to-many relation
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class MapOwningEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @ManyToMany
+ private Map<String, MapOwnedEntity> references = new HashMap<String, MapOwnedEntity>();
+
+ public MapOwningEntity() { }
+
+ public MapOwningEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public MapOwningEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Map<String, MapOwnedEntity> getReferences() {
+ return references;
+ }
+
+ public void setReferences(Map<String, MapOwnedEntity> references) {
+ this.references = references;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof MapOwningEntity)) return false;
+
+ MapOwningEntity that = (MapOwningEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "MapOwningEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,70 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.Entity;
-import javax.persistence.Id;
-
-/**
- * Many-to-many not-owning entity
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class ListOwnedUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- public ListOwnedUniEntity() {
- }
-
- public ListOwnedUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public ListOwnedUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof ListOwnedUniEntity)) return false;
-
- ListOwnedUniEntity that = (ListOwnedUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwnedEntity(id = " + id + ", data = " + data + ")";
- }
-}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,82 @@
+package org.jboss.envers.test.entities.manytomany.unidirectional;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.*;
+import java.util.List;
+
+/**
+ * Entity owning the many-to-many relation
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class ListUniEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @ManyToMany
+ private List<StrTestEntity> references;
+
+ public ListUniEntity() { }
+
+ public ListUniEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public ListUniEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public List<StrTestEntity> getReferences() {
+ return references;
+ }
+
+ public void setReferences(List<StrTestEntity> references) {
+ this.references = references;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof ListUniEntity)) return false;
+
+ ListUniEntity that = (ListUniEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "ListUniEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetRefCollEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,85 @@
+package org.jboss.envers.test.entities.manytomany.unidirectional;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import java.util.Map;
+
+/**
+ * Entity with a map from a string to an entity
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class MapUniEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @ManyToMany
+ private Map<String, StrTestEntity> map;
+
+ public MapUniEntity() {
+ }
+
+ public MapUniEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public MapUniEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Map<String, StrTestEntity> getMap() {
+ return map;
+ }
+
+ public void setMap(Map<String, StrTestEntity> map) {
+ this.map = map;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof MapUniEntity)) return false;
+
+ MapUniEntity that = (MapUniEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "MapUniEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,70 +0,0 @@
-package org.jboss.envers.test.entities.manytomany.unidirectional;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.Entity;
-import javax.persistence.Id;
-
-/**
- * Many-to-many not-owning entity
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class SetOwnedUniEntity {
- @Id
- private Integer id;
-
- @Versioned
- private String data;
-
- public SetOwnedUniEntity() {
- }
-
- public SetOwnedUniEntity(Integer id, String data) {
- this.id = id;
- this.data = data;
- }
-
- public SetOwnedUniEntity(String data) {
- this.data = data;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SetOwnedUniEntity)) return false;
-
- SetOwnedUniEntity that = (SetOwnedUniEntity) o;
-
- if (data != null ? !data.equals(that.data) : that.data != null) return false;
- if (id != null ? !id.equals(that.id) : that.id != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (id != null ? id.hashCode() : 0);
- result = 31 * result + (data != null ? data.hashCode() : 0);
- return result;
- }
-
- public String toString() {
- return "SetOwnedUniEntity(id = " + id + ", data = " + data + ")";
- }
-}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,82 @@
+package org.jboss.envers.test.entities.manytomany.unidirectional;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.test.entities.StrTestEntity;
+
+import javax.persistence.*;
+import java.util.Set;
+
+/**
+ * Entity owning the many-to-many relation
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class SetUniEntity {
+ @Id
+ private Integer id;
+
+ @Versioned
+ private String data;
+
+ @Versioned
+ @ManyToMany
+ private Set<StrTestEntity> references;
+
+ public SetUniEntity() { }
+
+ public SetUniEntity(Integer id, String data) {
+ this.id = id;
+ this.data = data;
+ }
+
+ public SetUniEntity(String data) {
+ this.data = data;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ public Set<StrTestEntity> getReferences() {
+ return references;
+ }
+
+ public void setReferences(Set<StrTestEntity> references) {
+ this.references = references;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof SetUniEntity)) return false;
+
+ SetUniEntity that = (SetUniEntity) o;
+
+ if (data != null ? !data.equals(that.data) : that.data != null) return false;
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = (id != null ? id.hashCode() : 0);
+ result = 31 * result + (data != null ? data.hashCode() : 0);
+ return result;
+ }
+
+ public String toString() {
+ return "SetUniEntity(id = " + id + ", data = " + data + ")";
+ }
+}
\ No newline at end of file
Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -61,7 +61,7 @@
em.getTransaction().commit();
- // Revision 4 (sme1: removing a non-existing mapping, sme2: replacing with the same value)
+ // No revision (sme1: removing a non-existing mapping, sme2: replacing with the same value)
em.getTransaction().begin();
sme1 = em.find(StringMapEntity.class, sme1.getId());
Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,163 @@
+package org.jboss.envers.test.integration.manytomany;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.manytomany.MapOwningEntity;
+import org.jboss.envers.test.entities.manytomany.MapOwnedEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicMap extends AbstractEntityTest {
+ private Integer ed1_id;
+ private Integer ed2_id;
+
+ private Integer ing1_id;
+ private Integer ing2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(MapOwningEntity.class);
+ cfg.addAnnotatedClass(MapOwnedEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ MapOwnedEntity ed1 = new MapOwnedEntity(1, "data_ed_1");
+ MapOwnedEntity ed2 = new MapOwnedEntity(2, "data_ed_2");
+
+ MapOwningEntity ing1 = new MapOwningEntity(3, "data_ing_1");
+ MapOwningEntity ing2 = new MapOwningEntity(4, "data_ing_2");
+
+ // Revision 1 (ing1: initialy empty, ing2: one mapping)
+ em.getTransaction().begin();
+
+ ing2.getReferences().put("2", ed2);
+
+ em.persist(ed1);
+ em.persist(ed2);
+ em.persist(ing1);
+ em.persist(ing2);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (ing1: adding two mappings, ing2: replacing an existing mapping)
+
+ em.getTransaction().begin();
+
+ ing1 = em.find(MapOwningEntity.class, ing1.getId());
+ ing2 = em.find(MapOwningEntity.class, ing2.getId());
+ ed1 = em.find(MapOwnedEntity.class, ed1.getId());
+ ed2 = em.find(MapOwnedEntity.class, ed2.getId());
+
+ ing1.getReferences().put("1", ed1);
+ ing1.getReferences().put("2", ed1);
+
+ ing2.getReferences().put("2", ed1);
+
+ em.getTransaction().commit();
+
+ // No revision (ing1: adding an existing mapping, ing2: removing a non existing mapping)
+ em.getTransaction().begin();
+
+ ing1 = em.find(MapOwningEntity.class, ing1.getId());
+ ing2 = em.find(MapOwningEntity.class, ing2.getId());
+
+ ing1.getReferences().put("1", ed1);
+
+ ing2.getReferences().remove("3");
+
+ em.getTransaction().commit();
+
+ // Revision 3 (ing1: clearing, ing2: replacing with a new map)
+ em.getTransaction().begin();
+
+ ing1 = em.find(MapOwningEntity.class, ing1.getId());
+ ed1 = em.find(MapOwnedEntity.class, ed1.getId());
+
+ ing1.getReferences().clear();
+ ing2.setReferences(new HashMap<String, MapOwnedEntity>());
+ ing2.getReferences().put("1", ed2);
+
+ em.getTransaction().commit();
+ //
+
+ ed1_id = ed1.getId();
+ ed2_id = ed2.getId();
+
+ ing1_id = ing1.getId();
+ ing2_id = ing2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwnedEntity.class, ed1_id));
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwnedEntity.class, ed2_id));
+
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwningEntity.class, ing1_id));
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwningEntity.class, ing2_id));
+ }
+
+ @Test
+ public void testHistoryOfEdId1() {
+ MapOwningEntity ing1 = getEntityManager().find(MapOwningEntity.class, ing1_id);
+ MapOwningEntity ing2 = getEntityManager().find(MapOwningEntity.class, ing2_id);
+
+ MapOwnedEntity rev1 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 1);
+ MapOwnedEntity rev2 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 2);
+ MapOwnedEntity rev3 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 3);
+
+ assert rev1.getReferencing().equals(Collections.EMPTY_SET);
+ assert rev2.getReferencing().equals(TestTools.makeSet(ing1, ing2));
+ assert rev3.getReferencing().equals(Collections.EMPTY_SET);
+ }
+
+ @Test
+ public void testHistoryOfEdId2() {
+ MapOwningEntity ing2 = getEntityManager().find(MapOwningEntity.class, ing2_id);
+
+ MapOwnedEntity rev1 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 1);
+ MapOwnedEntity rev2 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 2);
+ MapOwnedEntity rev3 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 3);
+
+ assert rev1.getReferencing().equals(TestTools.makeSet(ing2));
+ assert rev2.getReferencing().equals(Collections.EMPTY_SET);
+ assert rev3.getReferencing().equals(TestTools.makeSet(ing2));
+ }
+
+ @Test
+ public void testHistoryOfEdIng1() {
+ MapOwnedEntity ed1 = getEntityManager().find(MapOwnedEntity.class, ed1_id);
+
+ MapOwningEntity rev1 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 1);
+ MapOwningEntity rev2 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 2);
+ MapOwningEntity rev3 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 3);
+
+ assert rev1.getReferences().equals(Collections.EMPTY_MAP);
+ assert rev2.getReferences().equals(TestTools.makeMap("1", ed1, "2", ed1));
+ assert rev3.getReferences().equals(Collections.EMPTY_MAP);
+ }
+
+ @Test
+ public void testHistoryOfEdIng2() {
+ MapOwnedEntity ed1 = getEntityManager().find(MapOwnedEntity.class, ed1_id);
+ MapOwnedEntity ed2 = getEntityManager().find(MapOwnedEntity.class, ed2_id);
+
+ MapOwningEntity rev1 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 1);
+ MapOwningEntity rev2 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 2);
+ MapOwningEntity rev3 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 3);
+
+ assert rev1.getReferences().equals(TestTools.makeMap("2", ed2));
+ assert rev2.getReferences().equals(TestTools.makeMap("2", ed1));
+ assert rev3.getReferences().equals(TestTools.makeMap("1", ed2));
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,8 +1,8 @@
package org.jboss.envers.test.integration.manytomany.unidirectional;
import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.manytomany.unidirectional.ListOwnedUniEntity;
-import org.jboss.envers.test.entities.manytomany.unidirectional.ListOwningUniEntity;
+import org.jboss.envers.test.entities.manytomany.unidirectional.ListUniEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
import org.jboss.envers.test.tools.TestTools;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -24,19 +24,19 @@
private Integer ing2_id;
public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(ListOwningUniEntity.class);
- cfg.addAnnotatedClass(ListOwnedUniEntity.class);
+ cfg.addAnnotatedClass(ListUniEntity.class);
+ cfg.addAnnotatedClass(StrTestEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
- ListOwnedUniEntity ed1 = new ListOwnedUniEntity(1, "data_ed_1");
- ListOwnedUniEntity ed2 = new ListOwnedUniEntity(2, "data_ed_2");
+ StrTestEntity ed1 = new StrTestEntity("data_ed_1");
+ StrTestEntity ed2 = new StrTestEntity("data_ed_2");
- ListOwningUniEntity ing1 = new ListOwningUniEntity(3, "data_ing_1");
- ListOwningUniEntity ing2 = new ListOwningUniEntity(4, "data_ing_2");
+ ListUniEntity ing1 = new ListUniEntity(3, "data_ing_1");
+ ListUniEntity ing2 = new ListUniEntity(4, "data_ing_2");
// Revision 1
em.getTransaction().begin();
@@ -52,15 +52,15 @@
em.getTransaction().begin();
- ing1 = em.find(ListOwningUniEntity.class, ing1.getId());
- ing2 = em.find(ListOwningUniEntity.class, ing2.getId());
- ed1 = em.find(ListOwnedUniEntity.class, ed1.getId());
- ed2 = em.find(ListOwnedUniEntity.class, ed2.getId());
+ ing1 = em.find(ListUniEntity.class, ing1.getId());
+ ing2 = em.find(ListUniEntity.class, ing2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
- ing1.setReferences(new ArrayList<ListOwnedUniEntity>());
+ ing1.setReferences(new ArrayList<StrTestEntity>());
ing1.getReferences().add(ed1);
- ing2.setReferences(new ArrayList<ListOwnedUniEntity>());
+ ing2.setReferences(new ArrayList<StrTestEntity>());
ing2.getReferences().add(ed1);
ing2.getReferences().add(ed2);
@@ -69,9 +69,9 @@
// Revision 3
em.getTransaction().begin();
- ing1 = em.find(ListOwningUniEntity.class, ing1.getId());
- ed2 = em.find(ListOwnedUniEntity.class, ed2.getId());
- ed1 = em.find(ListOwnedUniEntity.class, ed1.getId());
+ ing1 = em.find(ListUniEntity.class, ing1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
ing1.getReferences().add(ed2);
@@ -80,9 +80,9 @@
// Revision 4
em.getTransaction().begin();
- ing1 = em.find(ListOwningUniEntity.class, ing1.getId());
- ed2 = em.find(ListOwnedUniEntity.class, ed2.getId());
- ed1 = em.find(ListOwnedUniEntity.class, ed1.getId());
+ ing1 = em.find(ListUniEntity.class, ing1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
ing1.getReferences().remove(ed1);
@@ -91,7 +91,7 @@
// Revision 5
em.getTransaction().begin();
- ing1 = em.find(ListOwningUniEntity.class, ing1.getId());
+ ing1 = em.find(ListUniEntity.class, ing1.getId());
ing1.setReferences(null);
@@ -108,23 +108,23 @@
@Test
public void testRevisionsCounts() {
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(ListOwnedUniEntity.class, ed1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(ListOwnedUniEntity.class, ed2_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed2_id));
- assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(ListOwningUniEntity.class, ing1_id));
- assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ListOwningUniEntity.class, ing2_id));
+ assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(ListUniEntity.class, ing1_id));
+ assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ListUniEntity.class, ing2_id));
}
@Test
public void testHistoryOfEdIng1() {
- ListOwnedUniEntity ed1 = getEntityManager().find(ListOwnedUniEntity.class, ed1_id);
- ListOwnedUniEntity ed2 = getEntityManager().find(ListOwnedUniEntity.class, ed2_id);
+ StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id);
+ StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id);
- ListOwningUniEntity rev1 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 1);
- ListOwningUniEntity rev2 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 2);
- ListOwningUniEntity rev3 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 3);
- ListOwningUniEntity rev4 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 4);
- ListOwningUniEntity rev5 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 5);
+ ListUniEntity rev1 = getVersionsReader().find(ListUniEntity.class, ing1_id, 1);
+ ListUniEntity rev2 = getVersionsReader().find(ListUniEntity.class, ing1_id, 2);
+ ListUniEntity rev3 = getVersionsReader().find(ListUniEntity.class, ing1_id, 3);
+ ListUniEntity rev4 = getVersionsReader().find(ListUniEntity.class, ing1_id, 4);
+ ListUniEntity rev5 = getVersionsReader().find(ListUniEntity.class, ing1_id, 5);
assert rev1.getReferences().equals(Collections.EMPTY_LIST);
assert TestTools.checkList(rev2.getReferences(), ed1);
@@ -135,14 +135,14 @@
@Test
public void testHistoryOfEdIng2() {
- ListOwnedUniEntity ed1 = getEntityManager().find(ListOwnedUniEntity.class, ed1_id);
- ListOwnedUniEntity ed2 = getEntityManager().find(ListOwnedUniEntity.class, ed2_id);
+ StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id);
+ StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id);
- ListOwningUniEntity rev1 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 1);
- ListOwningUniEntity rev2 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 2);
- ListOwningUniEntity rev3 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 3);
- ListOwningUniEntity rev4 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 4);
- ListOwningUniEntity rev5 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 5);
+ ListUniEntity rev1 = getVersionsReader().find(ListUniEntity.class, ing2_id, 1);
+ ListUniEntity rev2 = getVersionsReader().find(ListUniEntity.class, ing2_id, 2);
+ ListUniEntity rev3 = getVersionsReader().find(ListUniEntity.class, ing2_id, 3);
+ ListUniEntity rev4 = getVersionsReader().find(ListUniEntity.class, ing2_id, 4);
+ ListUniEntity rev5 = getVersionsReader().find(ListUniEntity.class, ing2_id, 5);
assert rev1.getReferences().equals(Collections.EMPTY_LIST);
assert TestTools.checkList(rev2.getReferences(), ed1, ed2);
Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.manytomany.unidirectional;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.manytomany.unidirectional.MapUniEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicUniMap extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(MapUniEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ MapUniEntity coll1 = new MapUniEntity(3, "coll1");
+
+ // Revision 1 (coll1: initialy one mapping)
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setMap(new HashMap<String, StrTestEntity>());
+ coll1.getMap().put("1", str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (coll1: adding one mapping)
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(MapUniEntity.class, coll1.getId());
+
+ coll1.getMap().put("2", str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3 (coll1: replacing one mapping)
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(MapUniEntity.class, coll1.getId());
+
+ coll1.getMap().put("2", str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4 (coll1: removing one mapping)
+ em.getTransaction().begin();
+
+ coll1 = em.find(MapUniEntity.class, coll1.getId());
+
+ coll1.getMap().remove("1");
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(MapUniEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ MapUniEntity rev1 = getVersionsReader().find(MapUniEntity.class, coll1_id, 1);
+ MapUniEntity rev2 = getVersionsReader().find(MapUniEntity.class, coll1_id, 2);
+ MapUniEntity rev3 = getVersionsReader().find(MapUniEntity.class, coll1_id, 3);
+ MapUniEntity rev4 = getVersionsReader().find(MapUniEntity.class, coll1_id, 4);
+
+ assert rev1.getMap().equals(TestTools.makeMap("1", str1));
+ assert rev2.getMap().equals(TestTools.makeMap("1", str1, "2", str2));
+ assert rev3.getMap().equals(TestTools.makeMap("1", str1, "2", str1));
+ assert rev4.getMap().equals(TestTools.makeMap("2", str1));
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,8 +1,8 @@
package org.jboss.envers.test.integration.manytomany.unidirectional;
import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.manytomany.unidirectional.SetOwnedUniEntity;
-import org.jboss.envers.test.entities.manytomany.unidirectional.SetOwningUniEntity;
+import org.jboss.envers.test.entities.manytomany.unidirectional.SetUniEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
import org.jboss.envers.test.tools.TestTools;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -24,19 +24,19 @@
private Integer ing2_id;
public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(SetOwningUniEntity.class);
- cfg.addAnnotatedClass(SetOwnedUniEntity.class);
+ cfg.addAnnotatedClass(SetUniEntity.class);
+ cfg.addAnnotatedClass(StrTestEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
- SetOwnedUniEntity ed1 = new SetOwnedUniEntity(1, "data_ed_1");
- SetOwnedUniEntity ed2 = new SetOwnedUniEntity(2, "data_ed_2");
+ StrTestEntity ed1 = new StrTestEntity("data_ed_1");
+ StrTestEntity ed2 = new StrTestEntity("data_ed_2");
- SetOwningUniEntity ing1 = new SetOwningUniEntity(3, "data_ing_1");
- SetOwningUniEntity ing2 = new SetOwningUniEntity(4, "data_ing_2");
+ SetUniEntity ing1 = new SetUniEntity(3, "data_ing_1");
+ SetUniEntity ing2 = new SetUniEntity(4, "data_ing_2");
// Revision 1
em.getTransaction().begin();
@@ -52,15 +52,15 @@
em.getTransaction().begin();
- ing1 = em.find(SetOwningUniEntity.class, ing1.getId());
- ing2 = em.find(SetOwningUniEntity.class, ing2.getId());
- ed1 = em.find(SetOwnedUniEntity.class, ed1.getId());
- ed2 = em.find(SetOwnedUniEntity.class, ed2.getId());
+ ing1 = em.find(SetUniEntity.class, ing1.getId());
+ ing2 = em.find(SetUniEntity.class, ing2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
- ing1.setReferences(new HashSet<SetOwnedUniEntity>());
+ ing1.setReferences(new HashSet<StrTestEntity>());
ing1.getReferences().add(ed1);
- ing2.setReferences(new HashSet<SetOwnedUniEntity>());
+ ing2.setReferences(new HashSet<StrTestEntity>());
ing2.getReferences().add(ed1);
ing2.getReferences().add(ed2);
@@ -69,9 +69,9 @@
// Revision 3
em.getTransaction().begin();
- ing1 = em.find(SetOwningUniEntity.class, ing1.getId());
- ed2 = em.find(SetOwnedUniEntity.class, ed2.getId());
- ed1 = em.find(SetOwnedUniEntity.class, ed1.getId());
+ ing1 = em.find(SetUniEntity.class, ing1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
ing1.getReferences().add(ed2);
@@ -80,9 +80,9 @@
// Revision 4
em.getTransaction().begin();
- ing1 = em.find(SetOwningUniEntity.class, ing1.getId());
- ed2 = em.find(SetOwnedUniEntity.class, ed2.getId());
- ed1 = em.find(SetOwnedUniEntity.class, ed1.getId());
+ ing1 = em.find(SetUniEntity.class, ing1.getId());
+ ed2 = em.find(StrTestEntity.class, ed2.getId());
+ ed1 = em.find(StrTestEntity.class, ed1.getId());
ing1.getReferences().remove(ed1);
@@ -91,7 +91,7 @@
// Revision 5
em.getTransaction().begin();
- ing1 = em.find(SetOwningUniEntity.class, ing1.getId());
+ ing1 = em.find(SetUniEntity.class, ing1.getId());
ing1.setReferences(null);
@@ -108,23 +108,23 @@
@Test
public void testRevisionsCounts() {
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(SetOwnedUniEntity.class, ed1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(SetOwnedUniEntity.class, ed2_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed2_id));
- assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(SetOwningUniEntity.class, ing1_id));
- assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetOwningUniEntity.class, ing2_id));
+ assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(SetUniEntity.class, ing1_id));
+ assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetUniEntity.class, ing2_id));
}
@Test
public void testHistoryOfEdIng1() {
- SetOwnedUniEntity ed1 = getEntityManager().find(SetOwnedUniEntity.class, ed1_id);
- SetOwnedUniEntity ed2 = getEntityManager().find(SetOwnedUniEntity.class, ed2_id);
+ StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id);
+ StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id);
- SetOwningUniEntity rev1 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 1);
- SetOwningUniEntity rev2 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 2);
- SetOwningUniEntity rev3 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 3);
- SetOwningUniEntity rev4 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 4);
- SetOwningUniEntity rev5 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 5);
+ SetUniEntity rev1 = getVersionsReader().find(SetUniEntity.class, ing1_id, 1);
+ SetUniEntity rev2 = getVersionsReader().find(SetUniEntity.class, ing1_id, 2);
+ SetUniEntity rev3 = getVersionsReader().find(SetUniEntity.class, ing1_id, 3);
+ SetUniEntity rev4 = getVersionsReader().find(SetUniEntity.class, ing1_id, 4);
+ SetUniEntity rev5 = getVersionsReader().find(SetUniEntity.class, ing1_id, 5);
assert rev1.getReferences().equals(Collections.EMPTY_SET);
assert rev2.getReferences().equals(TestTools.makeSet(ed1));
@@ -135,14 +135,14 @@
@Test
public void testHistoryOfEdIng2() {
- SetOwnedUniEntity ed1 = getEntityManager().find(SetOwnedUniEntity.class, ed1_id);
- SetOwnedUniEntity ed2 = getEntityManager().find(SetOwnedUniEntity.class, ed2_id);
+ StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id);
+ StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id);
- SetOwningUniEntity rev1 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 1);
- SetOwningUniEntity rev2 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 2);
- SetOwningUniEntity rev3 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 3);
- SetOwningUniEntity rev4 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 4);
- SetOwningUniEntity rev5 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 5);
+ SetUniEntity rev1 = getVersionsReader().find(SetUniEntity.class, ing2_id, 1);
+ SetUniEntity rev2 = getVersionsReader().find(SetUniEntity.class, ing2_id, 2);
+ SetUniEntity rev3 = getVersionsReader().find(SetUniEntity.class, ing2_id, 3);
+ SetUniEntity rev4 = getVersionsReader().find(SetUniEntity.class, ing2_id, 4);
+ SetUniEntity rev5 = getVersionsReader().find(SetUniEntity.class, ing2_id, 5);
assert rev1.getReferences().equals(Collections.EMPTY_SET);
assert rev2.getReferences().equals(TestTools.makeSet(ed1, ed2));
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.ListRefCollEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.ArrayList;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicDetachedList extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(ListRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ ListRefCollEntity coll1 = new ListRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new ArrayList<StrTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(ListRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(ListRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(ListRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(ListRefCollEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ ListRefCollEntity rev1 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 1);
+ ListRefCollEntity rev2 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 2);
+ ListRefCollEntity rev3 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 3);
+ ListRefCollEntity rev4 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 4);
+
+ assert TestTools.checkList(rev1.getCollection(), str1);
+ assert TestTools.checkList(rev2.getCollection(), str1, str2);
+ assert TestTools.checkList(rev3.getCollection(), str2);
+ assert TestTools.checkList(rev4.getCollection());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicDetachedSet extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(SetRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<StrTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1);
+ SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2);
+ SetRefCollEntity rev3 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 3);
+ SetRefCollEntity rev4 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 4);
+
+ assert rev1.getCollection().equals(TestTools.makeSet(str1));
+ assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
+ assert rev3.getCollection().equals(TestTools.makeSet(str2));
+ assert rev4.getCollection().equals(TestTools.makeSet());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,114 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityEmbId;
+import org.jboss.envers.test.entities.ids.EmbIdTestEntity;
+import org.jboss.envers.test.entities.ids.EmbId;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicDetachedSetWithEmbId extends AbstractEntityTest {
+ private EmbId str1_id;
+ private EmbId str2_id;
+
+ private EmbId coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(EmbIdTestEntity.class);
+ cfg.addAnnotatedClass(SetRefCollEntityEmbId.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ str1_id = new EmbId(1, 2);
+ str2_id = new EmbId(3, 4);
+
+ coll1_id = new EmbId(5, 6);
+
+ EmbIdTestEntity str1 = new EmbIdTestEntity(str1_id, "str1");
+ EmbIdTestEntity str2 = new EmbIdTestEntity(str2_id, "str2");
+
+ SetRefCollEntityEmbId coll1 = new SetRefCollEntityEmbId(coll1_id, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<EmbIdTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(EmbIdTestEntity.class, str2.getId());
+ coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(EmbIdTestEntity.class, str1.getId());
+ coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityEmbId.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ EmbIdTestEntity str1 = getEntityManager().find(EmbIdTestEntity.class, str1_id);
+ EmbIdTestEntity str2 = getEntityManager().find(EmbIdTestEntity.class, str2_id);
+
+ SetRefCollEntityEmbId rev1 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 1);
+ SetRefCollEntityEmbId rev2 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 2);
+ SetRefCollEntityEmbId rev3 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 3);
+ SetRefCollEntityEmbId rev4 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 4);
+
+ assert rev1.getCollection().equals(TestTools.makeSet(str1));
+ assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
+ assert rev3.getCollection().equals(TestTools.makeSet(str2));
+ assert rev4.getCollection().equals(TestTools.makeSet());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,114 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityMulId;
+import org.jboss.envers.test.entities.ids.MulIdTestEntity;
+import org.jboss.envers.test.entities.ids.MulId;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BasicDetachedSetWithMulId extends AbstractEntityTest {
+ private MulId str1_id;
+ private MulId str2_id;
+
+ private MulId coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(MulIdTestEntity.class);
+ cfg.addAnnotatedClass(SetRefCollEntityMulId.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ str1_id = new MulId(1, 2);
+ str2_id = new MulId(3, 4);
+
+ coll1_id = new MulId(5, 6);
+
+ MulIdTestEntity str1 = new MulIdTestEntity(str1_id.getId1(), str1_id.getId2(), "str1");
+ MulIdTestEntity str2 = new MulIdTestEntity(str2_id.getId1(), str2_id.getId2(), "str2");
+
+ SetRefCollEntityMulId coll1 = new SetRefCollEntityMulId(coll1_id.getId1(), coll1_id.getId2(), "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<MulIdTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(MulIdTestEntity.class, str2_id);
+ coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(MulIdTestEntity.class, str1_id);
+ coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
+
+ coll1.getCollection().remove(str1);
+
+ em.getTransaction().commit();
+
+ // Revision 4
+ em.getTransaction().begin();
+
+ coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
+
+ coll1.getCollection().clear();
+
+ em.getTransaction().commit();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityMulId.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ MulIdTestEntity str1 = getEntityManager().find(MulIdTestEntity.class, str1_id);
+ MulIdTestEntity str2 = getEntityManager().find(MulIdTestEntity.class, str2_id);
+
+ SetRefCollEntityMulId rev1 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 1);
+ SetRefCollEntityMulId rev2 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 2);
+ SetRefCollEntityMulId rev3 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 3);
+ SetRefCollEntityMulId rev4 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 4);
+
+ assert rev1.getCollection().equals(TestTools.makeSet(str1));
+ assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
+ assert rev3.getCollection().equals(TestTools.makeSet(str2));
+ assert rev4.getCollection().equals(TestTools.makeSet());
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll1".equals(rev2.getData());
+ assert "coll1".equals(rev3.getData());
+ assert "coll1".equals(rev4.getData());
+ }
+}
\ No newline at end of file
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,115 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.ListRefCollEntity;
-import org.jboss.envers.test.entities.StrTestEntity;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.ArrayList;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class BasicNotOwnedList extends AbstractEntityTest {
- private Integer str1_id;
- private Integer str2_id;
-
- private Integer coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(StrTestEntity.class);
- cfg.addAnnotatedClass(ListRefCollEntity.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- StrTestEntity str1 = new StrTestEntity("str1");
- StrTestEntity str2 = new StrTestEntity("str2");
-
- ListRefCollEntity coll1 = new ListRefCollEntity(3, "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
- em.persist(str2);
-
- coll1.setCollection(new ArrayList<StrTestEntity>());
- coll1.getCollection().add(str1);
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str2 = em.find(StrTestEntity.class, str2.getId());
- coll1 = em.find(ListRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().add(str2);
-
- em.getTransaction().commit();
-
- // Revision 3
- em.getTransaction().begin();
-
- str1 = em.find(StrTestEntity.class, str1.getId());
- coll1 = em.find(ListRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().remove(str1);
-
- em.getTransaction().commit();
-
- // Revision 4
- em.getTransaction().begin();
-
- coll1 = em.find(ListRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().clear();
-
- em.getTransaction().commit();
-
- //
-
- str1_id = str1.getId();
- str2_id = str2.getId();
-
- coll1_id = coll1.getId();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(ListRefCollEntity.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
- StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
-
- ListRefCollEntity rev1 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 1);
- ListRefCollEntity rev2 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 2);
- ListRefCollEntity rev3 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 3);
- ListRefCollEntity rev4 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 4);
-
- assert TestTools.checkList(rev1.getCollection(), str1);
- assert TestTools.checkList(rev2.getCollection(), str1, str2);
- assert TestTools.checkList(rev3.getCollection(), str2);
- assert TestTools.checkList(rev4.getCollection());
-
- assert "coll1".equals(rev1.getData());
- assert "coll1".equals(rev2.getData());
- assert "coll1".equals(rev3.getData());
- assert "coll1".equals(rev4.getData());
- }
-}
\ No newline at end of file
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,115 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity;
-import org.jboss.envers.test.entities.StrTestEntity;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class BasicNotOwnedSet extends AbstractEntityTest {
- private Integer str1_id;
- private Integer str2_id;
-
- private Integer coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(StrTestEntity.class);
- cfg.addAnnotatedClass(SetRefCollEntity.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- StrTestEntity str1 = new StrTestEntity("str1");
- StrTestEntity str2 = new StrTestEntity("str2");
-
- SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
- em.persist(str2);
-
- coll1.setCollection(new HashSet<StrTestEntity>());
- coll1.getCollection().add(str1);
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str2 = em.find(StrTestEntity.class, str2.getId());
- coll1 = em.find(SetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().add(str2);
-
- em.getTransaction().commit();
-
- // Revision 3
- em.getTransaction().begin();
-
- str1 = em.find(StrTestEntity.class, str1.getId());
- coll1 = em.find(SetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().remove(str1);
-
- em.getTransaction().commit();
-
- // Revision 4
- em.getTransaction().begin();
-
- coll1 = em.find(SetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().clear();
-
- em.getTransaction().commit();
-
- //
-
- str1_id = str1.getId();
- str2_id = str2.getId();
-
- coll1_id = coll1.getId();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
- StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
-
- SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1);
- SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2);
- SetRefCollEntity rev3 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 3);
- SetRefCollEntity rev4 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 4);
-
- assert rev1.getCollection().equals(TestTools.makeSet(str1));
- assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
- assert rev3.getCollection().equals(TestTools.makeSet(str2));
- assert rev4.getCollection().equals(TestTools.makeSet());
-
- assert "coll1".equals(rev1.getData());
- assert "coll1".equals(rev2.getData());
- assert "coll1".equals(rev3.getData());
- assert "coll1".equals(rev4.getData());
- }
-}
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,114 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityEmbId;
-import org.jboss.envers.test.entities.ids.EmbIdTestEntity;
-import org.jboss.envers.test.entities.ids.EmbId;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class BasicNotOwnedSetWithEmbId extends AbstractEntityTest {
- private EmbId str1_id;
- private EmbId str2_id;
-
- private EmbId coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(EmbIdTestEntity.class);
- cfg.addAnnotatedClass(SetRefCollEntityEmbId.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- str1_id = new EmbId(1, 2);
- str2_id = new EmbId(3, 4);
-
- coll1_id = new EmbId(5, 6);
-
- EmbIdTestEntity str1 = new EmbIdTestEntity(str1_id, "str1");
- EmbIdTestEntity str2 = new EmbIdTestEntity(str2_id, "str2");
-
- SetRefCollEntityEmbId coll1 = new SetRefCollEntityEmbId(coll1_id, "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
- em.persist(str2);
-
- coll1.setCollection(new HashSet<EmbIdTestEntity>());
- coll1.getCollection().add(str1);
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str2 = em.find(EmbIdTestEntity.class, str2.getId());
- coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
-
- coll1.getCollection().add(str2);
-
- em.getTransaction().commit();
-
- // Revision 3
- em.getTransaction().begin();
-
- str1 = em.find(EmbIdTestEntity.class, str1.getId());
- coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
-
- coll1.getCollection().remove(str1);
-
- em.getTransaction().commit();
-
- // Revision 4
- em.getTransaction().begin();
-
- coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId());
-
- coll1.getCollection().clear();
-
- em.getTransaction().commit();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityEmbId.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str2_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- EmbIdTestEntity str1 = getEntityManager().find(EmbIdTestEntity.class, str1_id);
- EmbIdTestEntity str2 = getEntityManager().find(EmbIdTestEntity.class, str2_id);
-
- SetRefCollEntityEmbId rev1 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 1);
- SetRefCollEntityEmbId rev2 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 2);
- SetRefCollEntityEmbId rev3 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 3);
- SetRefCollEntityEmbId rev4 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 4);
-
- assert rev1.getCollection().equals(TestTools.makeSet(str1));
- assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
- assert rev3.getCollection().equals(TestTools.makeSet(str2));
- assert rev4.getCollection().equals(TestTools.makeSet());
-
- assert "coll1".equals(rev1.getData());
- assert "coll1".equals(rev2.getData());
- assert "coll1".equals(rev3.getData());
- assert "coll1".equals(rev4.getData());
- }
-}
\ No newline at end of file
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,114 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityMulId;
-import org.jboss.envers.test.entities.ids.MulIdTestEntity;
-import org.jboss.envers.test.entities.ids.MulId;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class BasicNotOwnedSetWithMulId extends AbstractEntityTest {
- private MulId str1_id;
- private MulId str2_id;
-
- private MulId coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(MulIdTestEntity.class);
- cfg.addAnnotatedClass(SetRefCollEntityMulId.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- str1_id = new MulId(1, 2);
- str2_id = new MulId(3, 4);
-
- coll1_id = new MulId(5, 6);
-
- MulIdTestEntity str1 = new MulIdTestEntity(str1_id.getId1(), str1_id.getId2(), "str1");
- MulIdTestEntity str2 = new MulIdTestEntity(str2_id.getId1(), str2_id.getId2(), "str2");
-
- SetRefCollEntityMulId coll1 = new SetRefCollEntityMulId(coll1_id.getId1(), coll1_id.getId2(), "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
- em.persist(str2);
-
- coll1.setCollection(new HashSet<MulIdTestEntity>());
- coll1.getCollection().add(str1);
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str2 = em.find(MulIdTestEntity.class, str2_id);
- coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
-
- coll1.getCollection().add(str2);
-
- em.getTransaction().commit();
-
- // Revision 3
- em.getTransaction().begin();
-
- str1 = em.find(MulIdTestEntity.class, str1_id);
- coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
-
- coll1.getCollection().remove(str1);
-
- em.getTransaction().commit();
-
- // Revision 4
- em.getTransaction().begin();
-
- coll1 = em.find(SetRefCollEntityMulId.class, coll1_id);
-
- coll1.getCollection().clear();
-
- em.getTransaction().commit();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityMulId.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str2_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- MulIdTestEntity str1 = getEntityManager().find(MulIdTestEntity.class, str1_id);
- MulIdTestEntity str2 = getEntityManager().find(MulIdTestEntity.class, str2_id);
-
- SetRefCollEntityMulId rev1 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 1);
- SetRefCollEntityMulId rev2 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 2);
- SetRefCollEntityMulId rev3 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 3);
- SetRefCollEntityMulId rev4 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 4);
-
- assert rev1.getCollection().equals(TestTools.makeSet(str1));
- assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
- assert rev3.getCollection().equals(TestTools.makeSet(str2));
- assert rev4.getCollection().equals(TestTools.makeSet());
-
- assert "coll1".equals(rev1.getData());
- assert "coll1".equals(rev2.getData());
- assert "coll1".equals(rev3.getData());
- assert "coll1".equals(rev4.getData());
- }
-}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,84 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class DataChangesDetachedSet extends AbstractEntityTest {
+ private Integer str1_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(SetRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+
+ SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+
+ coll1.setCollection(new HashSet<StrTestEntity>());
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(SetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str1);
+ coll1.setData("coll2");
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+
+ SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1);
+ SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2);
+
+ assert rev1.getCollection().equals(TestTools.makeSet());
+ assert rev2.getCollection().equals(TestTools.makeSet(str1));
+
+ assert "coll1".equals(rev1.getData());
+ assert "coll2".equals(rev2.getData());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,84 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity;
-import org.jboss.envers.test.entities.StrTestEntity;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class DataChangesNotOwnedSet extends AbstractEntityTest {
- private Integer str1_id;
-
- private Integer coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(StrTestEntity.class);
- cfg.addAnnotatedClass(SetRefCollEntity.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- StrTestEntity str1 = new StrTestEntity("str1");
-
- SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
-
- coll1.setCollection(new HashSet<StrTestEntity>());
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str1 = em.find(StrTestEntity.class, str1.getId());
- coll1 = em.find(SetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().add(str1);
- coll1.setData("coll2");
-
- em.getTransaction().commit();
-
- //
-
- str1_id = str1.getId();
-
- coll1_id = coll1.getId();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
-
- SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1);
- SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2);
-
- assert rev1.getCollection().equals(TestTools.makeSet());
- assert rev2.getCollection().equals(TestTools.makeSet(str1));
-
- assert "coll1".equals(rev1.getData());
- assert "coll2".equals(rev2.getData());
- }
-}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -0,0 +1,108 @@
+package org.jboss.envers.test.integration.onetomany.detached;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.entities.onetomany.detached.DoubleSetRefCollEntity;
+import org.jboss.envers.test.entities.StrTestEntity;
+import org.jboss.envers.test.tools.TestTools;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class DoubleDetachedSet extends AbstractEntityTest {
+ private Integer str1_id;
+ private Integer str2_id;
+
+ private Integer coll1_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StrTestEntity.class);
+ cfg.addAnnotatedClass(DoubleSetRefCollEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StrTestEntity str1 = new StrTestEntity("str1");
+ StrTestEntity str2 = new StrTestEntity("str2");
+
+ DoubleSetRefCollEntity coll1 = new DoubleSetRefCollEntity(3, "coll1");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(str1);
+ em.persist(str2);
+
+ coll1.setCollection(new HashSet<StrTestEntity>());
+ coll1.getCollection().add(str1);
+ em.persist(coll1);
+
+ coll1.setCollection2(new HashSet<StrTestEntity>());
+ coll1.getCollection2().add(str2);
+ em.persist(coll1);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+ em.getTransaction().begin();
+
+ str2 = em.find(StrTestEntity.class, str2.getId());
+ coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().add(str2);
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ str1 = em.find(StrTestEntity.class, str1.getId());
+ coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId());
+
+ coll1.getCollection().remove(str1);
+ coll1.getCollection2().add(str1);
+
+ em.getTransaction().commit();
+
+ //
+
+ str1_id = str1.getId();
+ str2_id = str2.getId();
+
+ coll1_id = coll1.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(DoubleSetRefCollEntity.class, coll1_id));
+
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
+ assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
+ }
+
+ @Test
+ public void testHistoryOfColl1() {
+ StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
+ StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
+
+ DoubleSetRefCollEntity rev1 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 1);
+ DoubleSetRefCollEntity rev2 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 2);
+ DoubleSetRefCollEntity rev3 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 3);
+
+ assert rev1.getCollection().equals(TestTools.makeSet(str1));
+ assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
+ assert rev3.getCollection().equals(TestTools.makeSet(str2));
+
+ assert rev1.getCollection2().equals(TestTools.makeSet(str2));
+ assert rev2.getCollection2().equals(TestTools.makeSet(str2));
+ assert rev3.getCollection2().equals(TestTools.makeSet(str1, str2));
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155)
+++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156)
@@ -1,108 +0,0 @@
-package org.jboss.envers.test.integration.onetomany.detached;
-
-import org.jboss.envers.test.integration.AbstractEntityTest;
-import org.jboss.envers.test.entities.onetomany.detached.DoubleSetRefCollEntity;
-import org.jboss.envers.test.entities.StrTestEntity;
-import org.jboss.envers.test.tools.TestTools;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import javax.persistence.EntityManager;
-import java.util.Arrays;
-import java.util.HashSet;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class DoubleNotOwnedSet extends AbstractEntityTest {
- private Integer str1_id;
- private Integer str2_id;
-
- private Integer coll1_id;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(StrTestEntity.class);
- cfg.addAnnotatedClass(DoubleSetRefCollEntity.class);
- }
-
- @BeforeClass(dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- StrTestEntity str1 = new StrTestEntity("str1");
- StrTestEntity str2 = new StrTestEntity("str2");
-
- DoubleSetRefCollEntity coll1 = new DoubleSetRefCollEntity(3, "coll1");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(str1);
- em.persist(str2);
-
- coll1.setCollection(new HashSet<StrTestEntity>());
- coll1.getCollection().add(str1);
- em.persist(coll1);
-
- coll1.setCollection2(new HashSet<StrTestEntity>());
- coll1.getCollection2().add(str2);
- em.persist(coll1);
-
- em.getTransaction().commit();
-
- // Revision 2
- em.getTransaction().begin();
-
- str2 = em.find(StrTestEntity.class, str2.getId());
- coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().add(str2);
-
- em.getTransaction().commit();
-
- // Revision 3
- em.getTransaction().begin();
-
- str1 = em.find(StrTestEntity.class, str1.getId());
- coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId());
-
- coll1.getCollection().remove(str1);
- coll1.getCollection2().add(str1);
-
- em.getTransaction().commit();
-
- //
-
- str1_id = str1.getId();
- str2_id = str2.getId();
-
- coll1_id = coll1.getId();
- }
-
- @Test
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(DoubleSetRefCollEntity.class, coll1_id));
-
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id));
- assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id));
- }
-
- @Test
- public void testHistoryOfColl1() {
- StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id);
- StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id);
-
- DoubleSetRefCollEntity rev1 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 1);
- DoubleSetRefCollEntity rev2 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 2);
- DoubleSetRefCollEntity rev3 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 3);
-
- assert rev1.getCollection().equals(TestTools.makeSet(str1));
- assert rev2.getCollection().equals(TestTools.makeSet(str1, str2));
- assert rev3.getCollection().equals(TestTools.makeSet(str2));
-
- assert rev1.getCollection2().equals(TestTools.makeSet(str2));
- assert rev2.getCollection2().equals(TestTools.makeSet(str2));
- assert rev3.getCollection2().equals(TestTools.makeSet(str1, str2));
- }
-}
\ No newline at end of file
16 years, 3 months
JBoss Envers SVN: r155 - in trunk/src/main/org/jboss/envers: entities/mapper/id and 3 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-21 16:42:00 -0400 (Sun, 21 Sep 2008)
New Revision: 155
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.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/relation/query/TwoEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java
trunk/src/main/org/jboss/envers/tools/query/Parameters.java
Log:
ENVERS-42: fixed and migrated many-to-many relations querying to the query builder
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -41,8 +41,9 @@
componentDatas);
} else if (idDatas.size() == 1) {
return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData,
- idDatas.get(0));
+ idDatas.get(0), componentDatas);
} else if (idDatas.size() == 1) {
+ // TODO: add componentDatas
return new ThreeEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData,
idDatas.get(0), idDatas.get(1));
} else {
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -48,88 +48,39 @@
}
}
- public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) {
- List<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
-
- Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
-
- for (QueryParameterData paramData : paramDatas) {
- parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue());
- }
- }
-
- public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) {
- List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
-
- Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
-
- for (QueryParameterData paramData : paramDatas) {
- parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName());
- }
- }
-
- public String getIdsEqualQuery(String prefix1, String prefix2) {
- List<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) {
+ public void addIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2) {
List<QueryParameterData> paramDatas1 = mapToQueryParametersFromId(null);
List<QueryParameterData> paramDatas2 = mapper2.mapToQueryParametersFromId(null);
- StringBuilder query = new StringBuilder();
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas1);
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 ");
- }
+ parametersToUse.addWhere(paramData1.getProperty(prefix1), false, "=", paramData2.getProperty(prefix2), false);
}
-
- return query.toString();
}
- public String getIdEqualsQuery(String prefix, boolean equals) {
- List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+ public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) {
+ List<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
- StringBuilder query = new StringBuilder();
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
- Iterator<QueryParameterData> paramDataIter = paramDatas.iterator();
+ for (QueryParameterData paramData : paramDatas) {
+ parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue());
+ }
+ }
- while (paramDataIter.hasNext()) {
- QueryParameterData paramData = paramDataIter.next();
+ public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) {
+ List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
- query.append(paramData.getProperty(prefix));
- if (equals) {
- query.append(" = ");
- } else {
- query.append(" != ");
- }
- query.append(":").append(paramData.getQueryParameterName());
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
- if (paramDataIter.hasNext()) {
- query.append(" AND ");
- }
+ for (QueryParameterData paramData : paramDatas) {
+ parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName());
}
-
- return query.toString();
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -55,22 +55,25 @@
List<QueryParameterData> mapToQueryParametersFromId(Object obj);
/**
- * Gets a query string, which contains equalities, which express the property that the id of the entity
+ * Adds query statements, which contains restrictions, 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.
+ * @param parameters Parameters, to which to add the statements.
+ * @param prefix1 First alias of the entity + prefix to add to the properties.
+ * @param prefix2 Second alias of the entity + prefix to add to the properties.
*/
- String getIdsEqualQuery(String prefix1, String prefix2);
+ void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2);
/**
* Adds query statements, which contains restrictions, 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).
+ * 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 parameters Parameters, to which to add the statements.
* @param prefix1 First alias of the entity + prefix to add to the properties.
+ * @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 + prefix to add to the properties.
*/
- void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2);
+ void addIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2);
/**
* Adds query statements, which contains restrictions, which express the property that the id of the entity
@@ -91,26 +94,4 @@
* @param equals Should this query express the "=" relation or the "<>" relation.
*/
void addNamedIdEqualsToQuery(Parameters parameters, 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.
- */
- 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.
- */
- String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2);
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -2,11 +2,16 @@
import org.jboss.envers.entities.mapper.id.QueryParameterData;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import org.hibernate.Query;
+import java.util.Collections;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
@@ -17,22 +22,24 @@
public TwoEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
String versionsMiddleEntityName,
MiddleIdData referencingIdData,
- MiddleIdData referencedIdData) {
+ MiddleIdData referencedIdData,
+ MiddleComponentData... componentDatas) {
this.referencingIdData = referencingIdData;
/*
* The query that we need to create:
* SELECT new list(ee, e) FROM versionsReferencedEntity e, middleEntity ee
* WHERE
+ * (entities referenced by the middle table; id_ref_ed = id of the referenced entity)
+ * ee.id_ref_ed = e.id_ref_ed AND
+ * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
+ * ee.id_ref_ing = :id_ref_ing AND
* (selecting e entities at revision :revision)
* e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity 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
+ * WHERE e2.revision <= :revision AND e2.id_ref_ed = e.id_ref_ed) 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
+ * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND
* (only non-deleted entities and associations)
* ee.revision_type != DEL AND
* e.revision_type != DEL
@@ -40,44 +47,58 @@
String revisionPropertyPath = verEntCfg.getRevisionPropPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
- StringBuilder querySB = new StringBuilder();
- querySB
- .append("SELECT new list(ee, e) FROM ").append(referencedIdData.getVersionsEntityName()).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(referencedIdData.getVersionsEntityName()).append(" e2 ")
- // e2.revision <= :revision
- .append("WHERE e2.").append(revisionPropertyPath).append(" <= :revision AND ")
- // e2.id1 = e.id1)
- .append(referencedIdData.getOriginalMapper().getIdsEqualQuery("e." + originalIdPropertyName, "e2." + originalIdPropertyName))
- .append(") AND ")
- // ee.id1 = e.id1
- .append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." + originalIdPropertyName,
- referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName))
- .append(" AND ")
- // ee.id2 = :id2
- .append(referencingIdData.getPrefixedMapper().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(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true))
- .append(" AND ")
- // ee2.id1 = ee.id1)
- .append(referencedIdData.getPrefixedMapper().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");
+ String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;
- queryString = querySB.toString();
+ // SELECT new list(ee) FROM middleEntity ee
+ QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee");
+ qb.addFrom(referencedIdData.getVersionsEntityName(), "e");
+ qb.addProjection("new list", "ee, e", false, false);
+ // WHERE
+ Parameters rootParameters = qb.getRootParameters();
+ // ee.id_ref_ed = e.id_ref_ed
+ referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
+ referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName);
+ // ee.originalId.id_ref_ing = :id_ref_ing
+ referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
+
+ // SELECT max(e.revision) FROM versionsReferencedEntity e2
+ QueryBuilder maxERevQb = qb.newSubQueryBuilder(referencedIdData.getVersionsEntityName(), "e2");
+ maxERevQb.addProjection("max", revisionPropertyPath, false);
+ // WHERE
+ Parameters maxERevQbParameters = maxERevQb.getRootParameters();
+ // e2.revision <= :revision
+ maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
+ // e2.id_ref_ed = e.id_ref_ed
+ referencedIdData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, "e." + originalIdPropertyName, "e2." + originalIdPropertyName);
+
+ // e.revision = (SELECT max(...) ...)
+ rootParameters.addWhere("e." + revisionPropertyPath, false, "=", maxERevQb);
+
+ // SELECT max(ee2.revision) FROM middleEntity ee2
+ QueryBuilder maxEeRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2");
+ maxEeRevQb.addProjection("max", revisionPropertyPath, false);
+ // WHERE
+ Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters();
+ // ee2.revision <= :revision
+ maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
+ // ee2.originalId.* = ee.originalId.*
+ String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
+ referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ for (MiddleComponentData componentData : componentDatas) {
+ componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ }
+
+ // ee.revision = (SELECT max(...) ...)
+ rootParameters.addWhere(revisionPropertyPath, "=", maxEeRevQb);
+
+ // ee.revision_type != DEL
+ rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype");
+ // e.revision_type != DEL
+ rootParameters.addWhereWithNamedParam("e." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype");
+
+ StringBuilder sb = new StringBuilder();
+ qb.build(sb, Collections.<String, Object>emptyMap());
+ queryString = sb.toString();
}
public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) {
Modified: trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java
===================================================================
--- trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -129,8 +129,6 @@
if (FlushMode.isManualFlushMode(session.getFlushMode())) {
Session temporarySession = null;
try {
- //noinspection deprecation
- //temporarySession = session.getSessionFactory().openSession(session.connection());
temporarySession = session.getFactory().openTemporarySession();
executeInSession(temporarySession);
Modified: trunk/src/main/org/jboss/envers/tools/query/Parameters.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 19:13:24 UTC (rev 154)
+++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 20:42:00 UTC (rev 155)
@@ -178,10 +178,18 @@
}
public void addWhere(String left, String op, QueryBuilder right) {
+ addWhere(left, true, op, right);
+ }
+
+ public void addWhere(String left, boolean addAlias, String op, QueryBuilder right) {
StringBuilder expression = new StringBuilder();
- expression.append(alias).append(".").append(left);
+ if (addAlias) {
+ expression.append(alias).append(".");
+ }
+ expression.append(left);
+
expression.append(" ").append(op).append(" ");
expression.append("(");
16 years, 3 months
JBoss Envers SVN: r154 - in trunk: src/main/org/jboss/envers/configuration/metadata and 10 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-21 15:13:24 -0400 (Sun, 21 Sep 2008)
New Revision: 154
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
Modified:
trunk/envers.iml
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.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/relation/AbstractCollectionMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.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/MiddleMapKeyComponentMapper.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/MapCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/tools/query/Parameters.java
trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
trunk/src/test/org/jboss/envers/test/tools/TestTools.java
Log:
ENVERS-42: fixed support for sets of simple values, added support for maps of simple values
Modified: trunk/envers.iml
===================================================================
--- trunk/envers.iml 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/envers.iml 2008-09-21 19:13:24 UTC (rev 154)
@@ -11,7 +11,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
- <orderEntry type="inheritedJdk" />
+ <orderEntry type="jdk" jdkName="1.5" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -6,12 +6,11 @@
import org.hibernate.type.*;
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.entities.mapper.relation.*;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
+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.MiddleDummyComponentMapper;
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.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
@@ -23,6 +22,7 @@
import java.util.*;
import java.util.Set;
import java.util.List;
+import java.util.Map;
/**
* Generates metadata for collection-valued properties.
@@ -169,8 +169,8 @@
// Only valid for an inverse relation; null otherwise.
String mappedBy;
- String referencingPrefix;
- // Only valid if referencedEntityName isn't null.
+ // The referencing prefix is always for a related entity. So it has always the "_" at the end added.
+ String referencingPrefixRelated;
String referencedPrefix;
if (value.isInverse()) {
@@ -180,19 +180,19 @@
throw new MappingException("Unable to read the mapped by attribute for " + name);
}
- referencingPrefix = mappedBy + "_";
- referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_";
+ referencingPrefixRelated = mappedBy + "_";
+ referencedPrefix = referencedEntityName == null ? "element" : StringTools.getLastComponent(referencedEntityName);
} else {
mappedBy = null;
- referencingPrefix = StringTools.getLastComponent(entityName) + "_";
- referencedPrefix = name + "_";
+ referencingPrefixRelated = StringTools.getLastComponent(entityName) + "_";
+ referencedPrefix = referencedEntityName == null ? "element" : name;
}
// Storing the id data of the referencing entity: original mapper, prefixed mapper and entity name.
MiddleIdData referencingIdData = new MiddleIdData(
referencingIdMapping.getIdMapper(),
- referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix),
+ referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefixRelated),
entityName,
mainGenerator.getVerEntCfg().getVersionsEntityName(entityName));
@@ -207,7 +207,7 @@
middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
// Adding related-entity (in this case: the referencing's entity id) id mapping to the xml.
- addRelatedToXmlMapping(middleEntityXmlId, referencingPrefix, value.getKey(), referencingIdMapping);
+ addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, value.getKey(), referencingIdMapping);
// Adding the revision number as a foreign key to the revision info entity to the composite id of the
// middle table.
@@ -229,7 +229,8 @@
MiddleComponentData indexComponentData;
if (value instanceof IndexedCollection) {
IndexedCollection indexedValue = (IndexedCollection) value;
- indexComponentData = null;
+ indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId,
+ queryGeneratorBuilder, "mapkey");
// TODO
} else {
// No index - creating a dummy mapper.
@@ -242,18 +243,31 @@
// ******
// Building the query generator.
- MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build();
+ MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(elementComponentData, indexComponentData);
+ // Creating common data
+ CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(
+ mainGenerator.getVerEntCfg(), versionsMiddleEntityName, name, referencingIdData, queryGenerator);
+
// Checking the type of the collection and adding an appropriate mapper.
Type type = value.getType();
- if (type instanceof SetType) {
- currentMapper.addComposite(name, new BasicCollectionMapper<Set>(mainGenerator.getVerEntCfg(),
- versionsMiddleEntityName, referencingIdData, HashSet.class, SetProxy.class, queryGenerator,
- name, elementComponentData));
+ if (type instanceof SortedSetType) {
+ currentMapper.addComposite(name, new BasicCollectionMapper<Set>(commonCollectionMapperData,
+ TreeSet.class, SortedSetProxy.class, elementComponentData));
+ } else if (type instanceof SetType) {
+ currentMapper.addComposite(name, new BasicCollectionMapper<Set>(commonCollectionMapperData,
+ HashSet.class, SetProxy.class, elementComponentData));
+ } else if (type instanceof SortedMapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not null.
+ currentMapper.addComposite(name, new MapCollectionMapper<Map>(commonCollectionMapperData,
+ TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData));
+ } else if (type instanceof MapType) {
+ // Indexed collection, so <code>indexComponentData</code> is not null.
+ currentMapper.addComposite(name, new MapCollectionMapper<Map>(commonCollectionMapperData,
+ HashMap.class, MapProxy.class, elementComponentData, indexComponentData));
} else if (type instanceof BagType) {
- currentMapper.addComposite(name, new BasicCollectionMapper<List>(mainGenerator.getVerEntCfg(),
- versionsMiddleEntityName, referencingIdData, ArrayList.class, ListProxy.class, queryGenerator,
- name, elementComponentData));
+ currentMapper.addComposite(name, new BasicCollectionMapper<List>(commonCollectionMapperData,
+ ArrayList.class, ListProxy.class, elementComponentData));
} else {
throw new RuntimeException();
}
@@ -289,6 +303,8 @@
String prefix) {
Type type = value.getType();
if (type instanceof ManyToOneType) {
+ String prefixRelated = prefix + "_";
+
ToOne toOneValue = (ToOne) value;
String referencedEntityName = toOneValue.getReferencedEntityName();
IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get(
@@ -297,11 +313,11 @@
// 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>middleEntityXml</code> is not null).
if (middleEntityXml != null) {
- addRelatedToXmlMapping(middleEntityXml, prefix, value, referencedIdMapping);
+ addRelatedToXmlMapping(middleEntityXml, prefixRelated, value, referencedIdMapping);
}
// Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
- IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefix);
+ IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefixRelated);
MiddleIdData referencedIdData = new MiddleIdData(
referencedIdMapping.getIdMapper(),
referencedPrefixedIdMapper,
@@ -314,9 +330,9 @@
queryGeneratorBuilder.getCurrentIndex());
} else if (type instanceof ImmutableType || type instanceof MutableType) {
// TODO: add support for enums, components, custom types
- mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, "element", value, null, ModificationStore.FULL, true);
+ mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true);
- return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), "element"),
+ return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix),
queryGeneratorBuilder.getCurrentIndex());
} else {
// TODO: throw an exception
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,6 +1,7 @@
package org.jboss.envers.configuration.metadata;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.OneEntityQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.TwoEntityQueryGenerator;
@@ -34,9 +35,10 @@
idDatas.add(idData);
}
- MiddleTableQueryGenerator build() {
+ MiddleTableQueryGenerator build(MiddleComponentData... componentDatas) {
if (idDatas.size() == 0) {
- return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData);
+ return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData,
+ componentDatas);
} else if (idDatas.size() == 1) {
return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData,
idDatas.get(0));
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -30,15 +30,18 @@
* @author Adam Warski (adam at warski dot org)
*/
public abstract class AbstractIdMapper implements IdMapper {
+ private Parameters getParametersToUse(Parameters parameters, List<QueryParameterData> paramDatas) {
+ if (paramDatas.size() > 1) {
+ return parameters.addSubParameters("and");
+ } else {
+ return parameters;
+ }
+ }
+
public void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2) {
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
- Parameters parametersToUse;
- if (paramDatas.size() > 1) {
- parametersToUse = parameters.addSubParameters("and");
- } else {
- parametersToUse = parameters;
- }
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
for (QueryParameterData paramData : paramDatas) {
parametersToUse.addWhere(paramData.getProperty(prefix1), false, "=", paramData.getProperty(prefix2), false);
@@ -48,18 +51,23 @@
public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) {
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
- Parameters parametersToUse;
- if (paramDatas.size() > 1) {
- parametersToUse = parameters.addSubParameters("and");
- } else {
- parametersToUse = parameters;
- }
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
for (QueryParameterData paramData : paramDatas) {
parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue());
}
}
+ public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) {
+ List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
+
+ for (QueryParameterData paramData : paramDatas) {
+ parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName());
+ }
+ }
+
public String getIdsEqualQuery(String prefix1, String prefix2) {
List<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -83,6 +83,16 @@
void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals);
/**
+ * Adds query statements, which contains named parameters, which express the property that the id of the entity
+ * with alias prefix, is equal to the given object. It is the responsibility of the using method to read
+ * parameter values from the id and specify them on the final query object.
+ * @param parameters Parameters, to which to add the statements.
+ * @param prefix Prefix to add to the properties (may be null).
+ * @param equals Should this query express the "=" relation or the "<>" relation.
+ */
+ void addNamedIdEqualsToQuery(Parameters parameters, 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)
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -2,28 +2,39 @@
import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.RevisionType;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.exception.VersionsException;
+import org.jboss.envers.tools.reflection.ReflectionTools;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.configuration.VersionsConfiguration;
import org.hibernate.collection.PersistentCollection;
+import org.hibernate.property.Setter;
import java.util.*;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Constructor;
/**
* @author Adam Warski (adam at warski dot org)
*/
-public abstract class AbstractCollectionMapper implements PropertyMapper {
- private final VersionsEntitiesConfiguration verEntCfg;
- private final String versionsMiddleEntityName;
- private final String collectionReferencingPropertyName;
- private final MiddleIdData referencingIdData;
+public abstract class AbstractCollectionMapper<T> implements PropertyMapper {
+ protected final CommonCollectionMapperData commonCollectionMapperData;
+ protected final Class<? extends T> collectionClass;
- protected AbstractCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName,
- String collectionReferencingPropertyName, MiddleIdData referencingIdData) {
- this.verEntCfg = verEntCfg;
- this.versionsMiddleEntityName = versionsMiddleEntityName;
- this.collectionReferencingPropertyName = collectionReferencingPropertyName;
- this.referencingIdData = referencingIdData;
+ private final Constructor<? extends T> proxyConstructor;
+
+ protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
+ Class<? extends T> collectionClass, Class<? extends T> proxyClass) {
+ this.commonCollectionMapperData = commonCollectionMapperData;
+ this.collectionClass = collectionClass;
+
+ try {
+ proxyConstructor = proxyClass.getConstructor(Initializor.class);
+ } catch (NoSuchMethodException e) {
+ throw new VersionsException(e);
+ }
}
protected abstract Collection getNewCollectionContent(PersistentCollection newCollection);
@@ -41,16 +52,17 @@
for (Object changedObj : changed) {
Map<String, Object> entityData = new HashMap<String, Object>();
Map<String, Object> originalId = new HashMap<String, Object>();
- entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
+ entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(), originalId);
- collectionChanges.add(new PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedObj));
+ collectionChanges.add(new PersistentCollectionChangeData(
+ commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, changedObj));
// Mapping the collection owner's id.
- referencingIdData.getPrefixedMapper().mapToMapFromId(originalId, id);
+ commonCollectionMapperData.getReferencingIdData().getPrefixedMapper().mapToMapFromId(originalId, id);
// Mapping collection element and index (if present).
mapToMapFromObject(originalId, changedObj);
- entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
+ entityData.put(commonCollectionMapperData.getVerEntCfg().getRevisionTypePropName(), revisionType);
}
}
@@ -58,7 +70,7 @@
public List<PersistentCollectionChangeData> mapCollectionChanges(String referencingPropertyName,
PersistentCollection newColl,
Serializable oldColl, Serializable id) {
- if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
+ if (!commonCollectionMapperData.getCollectionReferencingPropertyName().equals(referencingPropertyName)) {
return null;
}
@@ -87,4 +99,23 @@
// Changes are mapped in the "mapCollectionChanges" method.
return false;
}
+
+ protected abstract Initializor<T> getInitializor(VersionsConfiguration verCfg,
+ VersionsReaderImplementor versionsReader, Object primaryKey,
+ Number revision);
+
+ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey,
+ VersionsReaderImplementor versionsReader, Number revision) {
+ Setter setter = ReflectionTools.getSetter(obj.getClass(),
+ commonCollectionMapperData.getCollectionReferencingPropertyName());
+ try {
+ setter.set(obj, proxyConstructor.newInstance(getInitializor(verCfg, versionsReader, primaryKey, revision)), null);
+ } catch (InstantiationException e) {
+ throw new VersionsException(e);
+ } catch (IllegalAccessException e) {
+ throw new VersionsException(e);
+ } catch (InvocationTargetException e) {
+ throw new VersionsException(e);
+ }
+ }
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,71 +1,33 @@
package org.jboss.envers.entities.mapper.relation;
import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
import org.jboss.envers.entities.mapper.relation.lazy.initializor.BasicCollectionInitializor;
import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.tools.reflection.ReflectionTools;
-import org.jboss.envers.exception.VersionsException;
import org.hibernate.collection.PersistentCollection;
-import org.hibernate.property.Setter;
import java.util.Map;
-import java.util.List;
import java.util.Collection;
import java.io.Serializable;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
/**
* @author Adam Warski (adam at warski dot org)
*/
-public final class BasicCollectionMapper<T extends Collection> extends AbstractCollectionMapper implements PropertyMapper {
- private final Class<? extends T> collectionClass;
- private final MiddleTableQueryGenerator queryGenerator;
- private final String propertyName;
+public final class BasicCollectionMapper<T extends Collection> extends AbstractCollectionMapper<T> implements PropertyMapper {
private final MiddleComponentData elementComponentData;
- private final Constructor<? extends T> proxyConstructor;
-
- public BasicCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName,
- MiddleIdData referencingIdData,
+ public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
Class<? extends T> collectionClass, Class<? extends T> proxyClass,
- MiddleTableQueryGenerator queryGenerator,
- String propertyName, MiddleComponentData elementComponentData) {
- super(verEntCfg, versionsMiddleEntityName, propertyName, referencingIdData);
- this.collectionClass = collectionClass;
- this.queryGenerator = queryGenerator;
- this.propertyName = propertyName;
+ MiddleComponentData elementComponentData) {
+ super(commonCollectionMapperData, collectionClass, proxyClass);
this.elementComponentData = elementComponentData;
-
- try {
- proxyConstructor = proxyClass.getConstructor(Initializor.class);
- } catch (NoSuchMethodException e) {
- throw new VersionsException(e);
- }
}
- public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey,
- VersionsReaderImplementor versionsReader, Number revision) {
- // Creating the initializator and passing to the proxy, which will use it to initialize the collection
- // when and if it is accessed.
- Initializor<? extends Collection> collectionInitializor = new BasicCollectionInitializor<T>(verCfg,
- versionsReader, queryGenerator, primaryKey, revision, collectionClass, elementComponentData);
-
- Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
- try {
- setter.set(obj, proxyConstructor.newInstance(collectionInitializor), null);
- } catch (InstantiationException e) {
- throw new VersionsException(e);
- } catch (IllegalAccessException e) {
- throw new VersionsException(e);
- } catch (InvocationTargetException e) {
- throw new VersionsException(e);
- }
+ protected Initializor<T> getInitializor(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader,
+ Object primaryKey, Number revision) {
+ return new BasicCollectionInitializor<T>(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(),
+ primaryKey, revision, collectionClass, elementComponentData);
}
protected Collection getNewCollectionContent(PersistentCollection newCollection) {
@@ -73,7 +35,9 @@
}
protected Collection getOldCollectionContent(Serializable oldCollection) {
- if (oldCollection instanceof Map) {
+ if (oldCollection == null) {
+ return null;
+ } else if (oldCollection instanceof Map) {
return ((Map) oldCollection).keySet();
} else {
return (Collection) oldCollection;
Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java (rev 0)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -0,0 +1,46 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+
+/**
+ * Data that is used by all collection mappers, regardless of the type.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class CommonCollectionMapperData {
+ private final VersionsEntitiesConfiguration verEntCfg;
+ private final String versionsMiddleEntityName;
+ private final String collectionReferencingPropertyName;
+ private final MiddleIdData referencingIdData;
+ private final MiddleTableQueryGenerator queryGenerator;
+
+ public CommonCollectionMapperData(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName,
+ String collectionReferencingPropertyName, MiddleIdData referencingIdData,
+ MiddleTableQueryGenerator queryGenerator) {
+ this.verEntCfg = verEntCfg;
+ this.versionsMiddleEntityName = versionsMiddleEntityName;
+ this.collectionReferencingPropertyName = collectionReferencingPropertyName;
+ this.referencingIdData = referencingIdData;
+ this.queryGenerator = queryGenerator;
+ }
+
+ public VersionsEntitiesConfiguration getVerEntCfg() {
+ return verEntCfg;
+ }
+
+ public String getVersionsMiddleEntityName() {
+ return versionsMiddleEntityName;
+ }
+
+ public String getCollectionReferencingPropertyName() {
+ return collectionReferencingPropertyName;
+ }
+
+ public MiddleIdData getReferencingIdData() {
+ return referencingIdData;
+ }
+
+ public MiddleTableQueryGenerator getQueryGenerator() {
+ return queryGenerator;
+ }
+}
Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,82 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.entities.mapper.relation.lazy.DetachedRelationInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-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.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class ManyToManyNotOwningMapper extends AbstractOneToManyMapper implements PropertyMapper {
- /**
- * Name of the entity that declares the relation.
- */
- private final String referencedEntityName;
-
- // private final DetachedRelationQueryGenerator queryGenerator;
-
- public ManyToManyNotOwningMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName,
- String referencedEntityName, String collectionReferencingPropertyName,
- String versionsReferencedEntityName, String versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- super(referencingEntityName, collectionReferencingPropertyName);
-
- this.referencedEntityName = referencedEntityName;
-
- // queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName,
- // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper);
- }
-
- public List<PersistentCollectionChangeData> mapCollectionChanges(String referencingPropertyName,
- PersistentCollection newColl,
- Serializable oldColl, Serializable id) {
- // This is the not-owning side of the collection.
- return null;
- }
-
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj, Object oldObj) {
- // This is the not-owning side of the collection.
- return false;
- }
-
- protected <T> Initializor<T> getInitializator(VersionsConfiguration verCfg,
- VersionsReaderImplementor versionsReader,
- Class<?> entityClass, Object primaryKey,
- Number revision, Class<T> collectionClass) {
- return null; //new DetachedRelationInitializor<T>(verCfg, referencedEntityName, queryGenerator,
- // versionsReader, primaryKey, revision, collectionClass);
- }
-}
\ No newline at end of file
Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java (from rev 151, trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java)
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java (rev 0)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -0,0 +1,55 @@
+package org.jboss.envers.entities.mapper.relation;
+
+import org.jboss.envers.entities.mapper.PropertyMapper;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
+import org.jboss.envers.entities.mapper.relation.lazy.initializor.MapCollectionInitializor;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.hibernate.collection.PersistentCollection;
+
+import java.util.Map;
+import java.util.Collection;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class MapCollectionMapper<T extends Map> extends AbstractCollectionMapper<T> implements PropertyMapper {
+ private final MiddleComponentData elementComponentData;
+ private final MiddleComponentData indexComponentData;
+
+ public MapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData,
+ Class<? extends T> collectionClass, Class<? extends T> proxyClass,
+ MiddleComponentData elementComponentData, MiddleComponentData indexComponentData) {
+ super(commonCollectionMapperData, collectionClass, proxyClass);
+ this.elementComponentData = elementComponentData;
+ this.indexComponentData = indexComponentData;
+ }
+
+ protected Initializor<T> getInitializor(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader,
+ Object primaryKey, Number revision) {
+ return new MapCollectionInitializor<T>(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(),
+ primaryKey, revision, collectionClass, elementComponentData, indexComponentData);
+ }
+
+ protected Collection getNewCollectionContent(PersistentCollection newCollection) {
+ if (newCollection == null) {
+ return null;
+ } else {
+ return ((Map) newCollection).entrySet();
+ }
+ }
+
+ protected Collection getOldCollectionContent(Serializable oldCollection) {
+ if (oldCollection == null) {
+ return null;
+ } else {
+ return ((Map) oldCollection).entrySet();
+ }
+ }
+
+ protected void mapToMapFromObject(Map<String, Object> data, Object changed) {
+ elementComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getValue());
+ indexComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getKey());
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,144 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- *
- * See the copyright.txt in the distribution for a full listing of individual
- * contributors. 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.entities.mapper.relation.lazy.DetachedRelationInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.entities.mapper.id.IdMapper;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.RevisionType;
-import org.hibernate.collection.PersistentCollection;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyDetachedMapper extends AbstractOneToManyMapper implements PropertyMapper {
- private final VersionsEntitiesConfiguration verEntCfg;
- /**
- * Name of the entity that declares the relation.
- */
- private final String referencedEntityName;
- /**
- * Name of the property in <code>referencedEntityName</code> that holds the value of the relation.
- */
- private final String collectionReferencingPropertyName;
- /**
- * Generated middle entity, which stores the bindings of the relation.
- */
- private final String versionsMiddleEntityName;
- private final IdMapper referencingMiddleIdMapper;
- private final IdMapper referencedMiddleIdMapper;
-
- //private final DetachedRelationQueryGenerator queryGenerator;
-
- public OneToManyDetachedMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName,
- String referencedEntityName, String collectionReferencingPropertyName,
- String versionsReferencedEntityName, String versionsMiddleEntityName,
- IdMapper referencingMiddleIdMapper, IdMapper referencedMiddleIdMapper,
- IdMapper referencedIdMapper) {
- super(referencingEntityName, collectionReferencingPropertyName);
-
- this.verEntCfg = verEntCfg;
- this.referencedEntityName = referencedEntityName;
- this.collectionReferencingPropertyName = collectionReferencingPropertyName;
- this.versionsMiddleEntityName = versionsMiddleEntityName;
- this.referencingMiddleIdMapper = referencingMiddleIdMapper;
- this.referencedMiddleIdMapper = referencedMiddleIdMapper;
-
- //queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName,
- // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper);
- }
-
- private Collection getOldCollection(Serializable oldColl) {
- if (oldColl instanceof Map) {
- return ((Map) oldColl).keySet();
- } else {
- return (Collection) oldColl;
- }
- }
-
- private void addCollectionChanges(List<PersistentCollectionChangeData> collectionChanges, Set<Object> changed,
- RevisionType revisionType, Serializable id) {
- for (Object changedEntity : changed) {
- Map<String, Object> entityData = new HashMap<String, Object>();
- Map<String, Object> originalId = new HashMap<String, Object>();
- entityData.put(verEntCfg.getOriginalIdPropName(), originalId);
-
- collectionChanges.add(new PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedEntity));
- referencingMiddleIdMapper.mapToMapFromId(originalId, id);
- referencedMiddleIdMapper.mapToMapFromEntity(originalId, changedEntity);
-
- entityData.put(verEntCfg.getRevisionTypePropName(), revisionType);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- public List<PersistentCollectionChangeData> mapCollectionChanges(String referencingPropertyName,
- PersistentCollection newColl,
- Serializable oldColl, Serializable id) {
- if (!collectionReferencingPropertyName.equals(referencingPropertyName)) {
- return null;
- }
-
- List<PersistentCollectionChangeData> collectionChanges = new ArrayList<PersistentCollectionChangeData>();
-
- Collection newCollection = (Collection) newColl;
- Collection oldCollection = getOldCollection(oldColl);
-
- Set<Object> added = new HashSet<Object>();
- if (newColl != null) { added.addAll(newCollection); }
- if (oldColl != null) { added.removeAll(oldCollection); }
-
- addCollectionChanges(collectionChanges, added, RevisionType.ADD, id);
-
- Set<Object> deleted = new HashSet<Object>();
- if (oldColl != null) { deleted.addAll(oldCollection); }
- if (newColl != null) { deleted.removeAll(newCollection); }
-
- addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id);
-
- return collectionChanges;
- }
-
- @SuppressWarnings({"unchecked"})
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj, Object oldObj) {
- // Changes are mapped in the "mapCollectionChanges" method.
- return false;
- }
-
- protected <T> Initializor<T> getInitializator(VersionsConfiguration verCfg,
- VersionsReaderImplementor versionsReader,
- Class<?> entityClass, Object primaryKey,
- Number revision, Class<T> collectionClass) {
- return null;// new DetachedRelationInitializor<T>(verCfg, referencedEntityName, queryGenerator,
- //versionsReader, primaryKey, revision, collectionClass);
- }
-}
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-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,6 +1,7 @@
package org.jboss.envers.entities.mapper.relation.component;
import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -24,4 +25,14 @@
* @param obj Object to map from.
*/
void mapToMapFromObject(Map<String, Object> data, Object obj);
+
+ /**
+ * Adds query statements, which contains restrictions, which express the property that part of the middle
+ * entity with alias prefix1, is equal to part of the middle entity with alias prefix2 (the entity is the same).
+ * The part is the component's representation in the middle entity.
+ * @param parameters Parameters, to which to add the statements.
+ * @param prefix1 First alias of the entity + prefix to add to the properties.
+ * @param prefix2 Second alias of the entity + prefix to add to the properties.
+ */
+ void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2);
}
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-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,12 +1,12 @@
package org.jboss.envers.entities.mapper.relation.component;
import org.jboss.envers.entities.EntityInstantiator;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
/**
* @author Adam Warski (adam at warski dot org)
- * @deprecated
*/
public final class MiddleDummyComponentMapper implements MiddleComponentMapper {
public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map<String, Object> data, Number revision) {
@@ -15,4 +15,7 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) {
+ }
}
\ No newline at end of file
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -26,4 +27,8 @@
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-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -23,4 +24,8 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
relatedIdData.getPrefixedMapper().mapToMapFromEntity(data, obj);
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) {
+ relatedIdData.getPrefixedMapper().addIdsEqualToQuery(parameters, prefix1, prefix2);
+ }
}
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-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -2,6 +2,7 @@
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.tools.query.Parameters;
import java.util.Map;
@@ -25,4 +26,8 @@
public void mapToMapFromObject(Map<String, Object> data, Object obj) {
data.put(propertyName, obj);
}
+
+ public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) {
+ parameters.addWhere(prefix1 + "." + propertyName, false, "=", prefix2 + "." + propertyName, false);
+ }
}
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-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -12,8 +12,8 @@
* Initializes a map.
* @author Adam Warski (adam at warski dot org)
*/
-public class MapCollectionInitializor<T extends Map<Object, Object>> extends AbstractCollectionInitializor<T> {
- private final Class<T> collectionClass;
+public class MapCollectionInitializor<T extends Map> extends AbstractCollectionInitializor<T> {
+ private final Class<? extends T> collectionClass;
private final MiddleComponentData elementComponentData;
private final MiddleComponentData indexComponentData;
@@ -21,7 +21,7 @@
VersionsReaderImplementor versionsReader,
MiddleTableQueryGenerator queryGenerator,
Object primaryKey, Number revision,
- Class<T> collectionClass,
+ Class<? extends T> collectionClass,
MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
super(verCfg, versionsReader, queryGenerator, primaryKey, revision);
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -74,6 +74,7 @@
}
public boolean add(U o) {
+ checkInit();
return delegate.add(o);
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -106,12 +106,20 @@
return delegate.entrySet();
}
+ @Override
+ public String toString() {
+ checkInit();
+ return delegate.toString();
+ }
+
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
- public boolean equals(Object o) {
+ @Override
+ public boolean equals(Object obj) {
checkInit();
- return delegate.equals(o);
+ return delegate.equals(obj);
}
+ @Override
public int hashCode() {
checkInit();
return delegate.hashCode();
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,12 +1,17 @@
package org.jboss.envers.entities.mapper.relation.query;
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.entities.mapper.id.QueryParameterData;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.RevisionType;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
import org.hibernate.Query;
+import java.util.Collections;
+
/**
* @author Adam Warski (adam at warski dot org)
*/
@@ -16,42 +21,53 @@
public OneEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
String versionsMiddleEntityName,
- MiddleIdData referencingIdData) {
+ MiddleIdData referencingIdData,
+ MiddleComponentData... componentDatas) {
this.referencingIdData = referencingIdData;
/*
* The query that we need to create:
* SELECT new list(ee) FROM middleEntity ee WHERE
- * (only entities referenced by the association)
- * ee.id2 = :id2 AND
+ * (only entities referenced by the association; id_ref_ing = id of the referencing entity)
+ * ee.originalId.id_ref_ing = :id_ref_ing AND
* (the association at revision :revision)
* ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2
- * WHERE ee2.revision <= :revision AND ee2.id2 = :id2) AND
+ * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND
* (only non-deleted entities and associations)
* ee.revision_type != DEL
*/
String revisionPropertyPath = verEntCfg.getRevisionPropPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
- StringBuilder querySB = new StringBuilder();
- querySB
- .append("SELECT new list(ee) FROM ").append(versionsMiddleEntityName).append(" ee ")
- .append("WHERE ")
- // ee.id2 = :id2
- .append(referencingIdData.getPrefixedMapper().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(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true))
- .append(") AND ")
- // ee.revision_type != DEL
- .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype");
+ // SELECT new list(ee) FROM middleEntity ee
+ QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee");
+ qb.addProjection("new list", "ee", false, false);
+ // WHERE
+ Parameters rootParameters = qb.getRootParameters();
+ // ee.originalId.id_ref_ing = :id_ref_ing
+ referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
+ // SELECT max(ee2.revision) FROM middleEntity ee2
+ QueryBuilder maxRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2");
+ maxRevQb.addProjection("max", revisionPropertyPath, false);
+ // WHERE
+ Parameters maxRevQbParameters = maxRevQb.getRootParameters();
+ // ee2.revision <= :revision
+ maxRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision");
+ // ee2.originalId.* = ee.originalId.*
+ String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName;
+ String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName;
+ referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ for (MiddleComponentData componentData : componentDatas) {
+ componentData.getComponentMapper().addMiddleEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
+ }
+ // ee.revision = (SELECT max(...) ...)
+ rootParameters.addWhere(revisionPropertyPath, "=", maxRevQb);
+ // ee.revision_type != DEL
+ rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype");
- queryString = querySB.toString();
+ StringBuilder sb = new StringBuilder();
+ qb.build(sb, Collections.<String, Object>emptyMap());
+ queryString = sb.toString();
}
public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) {
Modified: trunk/src/main/org/jboss/envers/tools/query/Parameters.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -135,15 +135,22 @@
}
public void addWhereWithParam(String left, boolean addAlias, String op, Object paramValue) {
+ String paramName = generateQueryParam();
+ localQueryParamValues.put(paramName, paramValue);
+
+ addWhereWithNamedParam(left, addAlias, op, paramName);
+ }
+
+ public void addWhereWithNamedParam(String left, String op, String paramName) {
+ addWhereWithNamedParam(left, true, op, paramName);
+ }
+
+ public void addWhereWithNamedParam(String left, boolean addAlias, String op, String paramName) {
StringBuilder expression = new StringBuilder();
if (addAlias) { expression.append(alias).append("."); }
expression.append(left);
-
expression.append(" ").append(op).append(" ");
-
- String paramName = generateQueryParam();
- localQueryParamValues.put(paramName, paramValue);
expression.append(":").append(paramName);
expressions.add(expression.toString());
Modified: trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -130,10 +130,14 @@
}
public void addProjection(String function, String propertyName, boolean distinct) {
+ addProjection(function, propertyName, distinct, true);
+ }
+
+ public void addProjection(String function, String propertyName, boolean distinct, boolean addAlias) {
if (function == null) {
- projections.add((distinct ? "distinct " : "") + alias + "." + propertyName);
+ projections.add((distinct ? "distinct " : "") + (addAlias ? alias+ "." : "") + propertyName);
} else {
- projections.add(function + "(" + (distinct ? "distinct " : "") + alias + "." + propertyName + ")");
+ projections.add(function + "(" + (distinct ? "distinct " : "") + (addAlias ? alias + "." : "") + propertyName + ")");
}
}
@@ -141,7 +145,7 @@
* Builds the given query, appending results to the given string buffer, and adding all query parameter values
* that are used to the map provided.
* @param sb String builder to which the query will be appended.
- * @param queryParamValues Values of parameters used in the query.
+ * @param queryParamValues Map to which name and values of parameters used in the query should be added.
*/
public void build(StringBuilder sb, Map<String, Object> queryParamValues) {
sb.append("select ");
Copied: trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java (from rev 153, trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -0,0 +1,63 @@
+package org.jboss.envers.test.entities.collection;
+
+import org.jboss.envers.Versioned;
+import org.hibernate.annotations.CollectionOfElements;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class StringMapEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @CollectionOfElements
+ private Map<String, String> strings;
+
+ public StringMapEntity() {
+ strings = new HashMap<String, String>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<String, String> getStrings() {
+ return strings;
+ }
+
+ public void setStrings(Map<String, String> strings) {
+ this.strings = strings;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof StringMapEntity)) return false;
+
+ StringMapEntity that = (StringMapEntity) 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 "SME(id = " + id + ", strings = " + strings + ")";
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java (from rev 153, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -0,0 +1,115 @@
+package org.jboss.envers.test.integration.collection;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.collection.StringMapEntity;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Collections;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class StringMap extends AbstractEntityTest {
+ private Integer sme1_id;
+ private Integer sme2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StringMapEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StringMapEntity sme1 = new StringMapEntity();
+ StringMapEntity sme2 = new StringMapEntity();
+
+ // Revision 1 (sme1: initialy empty, sme2: initialy 1 mapping)
+ em.getTransaction().begin();
+
+ sme2.getStrings().put("1", "a");
+
+ em.persist(sme1);
+ em.persist(sme2);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (sme1: adding 2 mappings, sme2: no changes)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().put("1", "a");
+ sme1.getStrings().put("2", "b");
+
+ em.getTransaction().commit();
+
+ // Revision 3 (sme1: removing an existing mapping, sme2: replacing a value)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().remove("1");
+ sme2.getStrings().put("1", "b");
+
+ em.getTransaction().commit();
+
+ // Revision 4 (sme1: removing a non-existing mapping, sme2: replacing with the same value)
+ em.getTransaction().begin();
+
+ sme1 = em.find(StringMapEntity.class, sme1.getId());
+ sme2 = em.find(StringMapEntity.class, sme2.getId());
+
+ sme1.getStrings().remove("3");
+ sme2.getStrings().put("1", "b");
+
+ em.getTransaction().commit();
+
+ //
+
+ sme1_id = sme1.getId();
+ sme2_id = sme2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme1_id));
+ assert Arrays.asList(1, 3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme2_id));
+ }
+
+ @Test
+ public void testHistoryOfSse1() {
+ StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme1_id, 1);
+ StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme1_id, 2);
+ StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme1_id, 3);
+ StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme1_id, 4);
+
+ assert rev1.getStrings().equals(Collections.EMPTY_MAP);
+ assert rev2.getStrings().equals(TestTools.makeMap("1", "a", "2", "b"));
+ System.out.println(rev2.getStrings());
+ System.out.println(rev3.getStrings());
+ System.out.println(rev4.getStrings());
+ assert rev3.getStrings().equals(TestTools.makeMap("2", "b"));
+ assert rev4.getStrings().equals(TestTools.makeMap("2", "b"));
+ }
+
+ @Test
+ public void testHistoryOfSse2() {
+ StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme2_id, 1);
+ StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme2_id, 2);
+ StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme2_id, 3);
+ StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme2_id, 4);
+
+ assert rev1.getStrings().equals(TestTools.makeMap("1", "a"));
+ assert rev2.getStrings().equals(TestTools.makeMap("1", "a"));
+ assert rev3.getStrings().equals(TestTools.makeMap("1", "b"));
+ assert rev4.getStrings().equals(TestTools.makeMap("1", "b"));
+ }
+}
\ No newline at end of file
Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -10,7 +10,6 @@
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.Collections;
-import java.util.HashSet;
/**
* @author Adam Warski (adam at warski dot org)
@@ -30,18 +29,18 @@
StringSetEntity sse1 = new StringSetEntity();
StringSetEntity sse2 = new StringSetEntity();
- // Revision 1
+ // Revision 1 (sse1: initialy empty, sse2: initialy 2 elements)
em.getTransaction().begin();
sse2.getStrings().add("sse2_string1");
+ sse2.getStrings().add("sse2_string2");
em.persist(sse1);
em.persist(sse2);
em.getTransaction().commit();
- // Revision 2
-
+ // Revision 2 (sse1: adding 2 elements, sse2: adding an existing element)
em.getTransaction().begin();
sse1 = em.find(StringSetEntity.class, sse1.getId());
@@ -54,7 +53,7 @@
em.getTransaction().commit();
- // Revision 3
+ // Revision 3 (sse1: removing a non-existing element, sse2: removing one element)
em.getTransaction().begin();
sse1 = em.find(StringSetEntity.class, sse1.getId());
@@ -94,8 +93,8 @@
StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse2_id, 2);
StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse2_id, 3);
- assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1"));
- assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1"));
- assert rev3.getStrings().equals(Collections.EMPTY_SET);
+ assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1", "sse2_string2"));
+ assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1", "sse2_string2"));
+ assert rev3.getStrings().equals(TestTools.makeSet("sse2_string2"));
}
}
\ No newline at end of file
Modified: trunk/src/test/org/jboss/envers/test/tools/TestTools.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 12:51:09 UTC (rev 153)
+++ trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 19:13:24 UTC (rev 154)
@@ -1,8 +1,6 @@
package org.jboss.envers.test.tools;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.List;
+import java.util.*;
/**
* @author Adam Warski (adam at warski dot org)
@@ -18,6 +16,17 @@
return ret;
}
+ public static Map<Object, Object> makeMap(Object... objects) {
+ Map<Object, Object> ret = new HashMap<Object, Object>();
+ // The number of objects must be divisable by 2.
+ //noinspection ManualArrayToCollectionCopy
+ for (int i=0; i<objects.length; i+=2) {
+ ret.put(objects[i], objects[i+1]);
+ }
+
+ return ret;
+ }
+
public static <T> boolean checkList(List<T> list, T... objects) {
if (list.size() != objects.length) {
return false;
16 years, 3 months
JBoss Envers SVN: r153 - in trunk: src/main/org/jboss/envers/configuration/metadata and 4 other directories.
by jboss-envers-commits@lists.jboss.org
Author: adamw
Date: 2008-09-21 08:51:09 -0400 (Sun, 21 Sep 2008)
New Revision: 153
Added:
trunk/src/test/org/jboss/envers/test/entities/collection/
trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java
trunk/src/test/org/jboss/envers/test/integration/collection/
trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
Modified:
trunk/resources/test/testng.xml
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
Log:
ENVERS-42: support for sets, with a test
Modified: trunk/resources/test/testng.xml
===================================================================
--- trunk/resources/test/testng.xml 2008-09-21 12:21:25 UTC (rev 152)
+++ trunk/resources/test/testng.xml 2008-09-21 12:51:09 UTC (rev 153)
@@ -4,6 +4,7 @@
<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.components" />
<package name="org.jboss.envers.test.integration.data" />
<package name="org.jboss.envers.test.integration.flush" />
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:21:25 UTC (rev 152)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:51:09 UTC (rev 153)
@@ -261,12 +261,16 @@
// ******
// Storing information about this relation.
// ******
- if (value.isInverse()) {
- mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy,
- referencedEntityName);
- } else {
- mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name,
- referencedEntityName);
+
+ // Only if this is a relation (when there is a referenced entity).
+ if (referencedEntityName != null) {
+ if (value.isInverse()) {
+ mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy,
+ referencedEntityName);
+ } else {
+ mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name,
+ referencedEntityName);
+ }
}
}
Added: trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java 2008-09-21 12:51:09 UTC (rev 153)
@@ -0,0 +1,63 @@
+package org.jboss.envers.test.entities.collection;
+
+import org.jboss.envers.Versioned;
+import org.hibernate.annotations.CollectionOfElements;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class StringSetEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @CollectionOfElements
+ private Set<String> strings;
+
+ public StringSetEntity() {
+ strings = new HashSet<String>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Set<String> getStrings() {
+ return strings;
+ }
+
+ public void setStrings(Set<String> strings) {
+ this.strings = strings;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof StringSetEntity)) return false;
+
+ StringSetEntity that = (StringSetEntity) 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 "SSE(id = " + id + ", strings = " + strings + ")";
+ }
+}
\ No newline at end of file
Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicSet.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java (rev 0)
+++ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 12:51:09 UTC (rev 153)
@@ -0,0 +1,101 @@
+package org.jboss.envers.test.integration.collection;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.jboss.envers.test.entities.collection.StringSetEntity;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class StringSet extends AbstractEntityTest {
+ private Integer sse1_id;
+ private Integer sse2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(StringSetEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ StringSetEntity sse1 = new StringSetEntity();
+ StringSetEntity sse2 = new StringSetEntity();
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ sse2.getStrings().add("sse2_string1");
+
+ em.persist(sse1);
+ em.persist(sse2);
+
+ em.getTransaction().commit();
+
+ // Revision 2
+
+ em.getTransaction().begin();
+
+ sse1 = em.find(StringSetEntity.class, sse1.getId());
+ sse2 = em.find(StringSetEntity.class, sse2.getId());
+
+ sse1.getStrings().add("sse1_string1");
+ sse1.getStrings().add("sse1_string2");
+
+ sse2.getStrings().add("sse2_string1");
+
+ em.getTransaction().commit();
+
+ // Revision 3
+ em.getTransaction().begin();
+
+ sse1 = em.find(StringSetEntity.class, sse1.getId());
+ sse2 = em.find(StringSetEntity.class, sse2.getId());
+
+ sse1.getStrings().remove("sse1_string3");
+ sse2.getStrings().remove("sse2_string1");
+
+ em.getTransaction().commit();
+
+ //
+
+ sse1_id = sse1.getId();
+ sse2_id = sse2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(StringSetEntity.class, sse1_id));
+ assert Arrays.asList(1, 3).equals(getVersionsReader().getRevisions(StringSetEntity.class, sse2_id));
+ }
+
+ @Test
+ public void testHistoryOfSse1() {
+ StringSetEntity rev1 = getVersionsReader().find(StringSetEntity.class, sse1_id, 1);
+ StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse1_id, 2);
+ StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse1_id, 3);
+
+ assert rev1.getStrings().equals(Collections.EMPTY_SET);
+ assert rev2.getStrings().equals(TestTools.makeSet("sse1_string1", "sse1_string2"));
+ assert rev3.getStrings().equals(TestTools.makeSet("sse1_string1", "sse1_string2"));
+ }
+
+ @Test
+ public void testHistoryOfSse2() {
+ StringSetEntity rev1 = getVersionsReader().find(StringSetEntity.class, sse2_id, 1);
+ StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse2_id, 2);
+ StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse2_id, 3);
+
+ assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1"));
+ assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1"));
+ assert rev3.getStrings().equals(Collections.EMPTY_SET);
+ }
+}
\ No newline at end of file
Property changes on: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java
___________________________________________________________________
Name: svn:mergeinfo
+
16 years, 3 months