Author: adamw
Date: 2009-08-12 10:49:55 -0400 (Wed, 12 Aug 2009)
New Revision: 17273
Modified:
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/MultiPropertyMapper.java
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/components/Components.java
Log:
HHH-3957:
- when all properties of an embedded object are null, don't set the embedded object on
the entity
Modified:
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java
===================================================================
---
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java 2009-08-12
04:22:23 UTC (rev 17272)
+++
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java 2009-08-12
14:49:55 UTC (rev 17273)
@@ -43,7 +43,7 @@
*/
public class ComponentPropertyMapper implements PropertyMapper, CompositeMapperBuilder {
private final PropertyData propertyData;
- private final ExtendedPropertyMapper delegate;
+ private final MultiPropertyMapper delegate;
private final String componentClassName;
public ComponentPropertyMapper(PropertyData propertyData, String componentClassName)
{
@@ -75,14 +75,26 @@
Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData);
- try {
- Object subObj = ReflectHelper.getDefaultConstructor(
- Thread.currentThread().getContextClassLoader().loadClass(componentClassName)).newInstance();
- setter.set(obj, subObj, null);
- delegate.mapToEntityFromMap(verCfg, subObj, data, primaryKey, versionsReader,
revision);
- } catch (Exception e) {
- throw new AuditException(e);
- }
+ // If all properties are null and single, then the component has to be null also.
+ boolean allNullAndSingle = true;
+ for (Map.Entry<PropertyData, PropertyMapper> property :
delegate.getProperties().entrySet()) {
+ if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof
SinglePropertyMapper)) {
+ allNullAndSingle = false;
+ break;
+ }
+ }
+
+ // And we don't have to set anything on the object - the default value is null
+ if (!allNullAndSingle) {
+ try {
+ Object subObj = ReflectHelper.getDefaultConstructor(
+ Thread.currentThread().getContextClassLoader().loadClass(componentClassName)).newInstance();
+ setter.set(obj, subObj, null);
+ delegate.mapToEntityFromMap(verCfg, subObj, data, primaryKey, versionsReader,
revision);
+ } catch (Exception e) {
+ throw new AuditException(e);
+ }
+ }
}
public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName,
Modified:
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/MultiPropertyMapper.java
===================================================================
---
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/MultiPropertyMapper.java 2009-08-12
04:22:23 UTC (rev 17272)
+++
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/MultiPropertyMapper.java 2009-08-12
14:49:55 UTC (rev 17273)
@@ -26,6 +26,7 @@
import java.io.Serializable;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.configuration.AuditConfiguration;
@@ -150,4 +151,8 @@
return null;
}
}
+
+ public Map<PropertyData, PropertyMapper> getProperties() {
+ return properties;
+ }
}
Modified:
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/components/Components.java
===================================================================
---
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/components/Components.java 2009-08-12
04:22:23 UTC (rev 17272)
+++
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/components/Components.java 2009-08-12
14:49:55 UTC (rev 17273)
@@ -42,6 +42,7 @@
private Integer id1;
private Integer id2;
private Integer id3;
+ private Integer id4;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(ComponentTestEntity.class);
@@ -56,10 +57,12 @@
ComponentTestEntity cte1 = new ComponentTestEntity(new Component1("a",
"b"), new Component2("x", "y"));
ComponentTestEntity cte2 = new ComponentTestEntity(new Component1("a2",
"b2"), new Component2("x2", "y2"));
ComponentTestEntity cte3 = new ComponentTestEntity(new Component1("a3",
"b3"), new Component2("x3", "y3"));
+ ComponentTestEntity cte4 = new ComponentTestEntity(null, null);
em.persist(cte1);
em.persist(cte2);
em.persist(cte3);
+ em.persist(cte4);
em.getTransaction().commit();
@@ -70,10 +73,15 @@
cte1 = em.find(ComponentTestEntity.class, cte1.getId());
cte2 = em.find(ComponentTestEntity.class, cte2.getId());
cte3 = em.find(ComponentTestEntity.class, cte3.getId());
+ cte4 = em.find(ComponentTestEntity.class, cte4.getId());
cte1.setComp1(new Component1("a'", "b'"));
cte2.getComp1().setStr1("a2'");
cte3.getComp2().setStr6("y3'");
+ cte4.setComp1(new Component1());
+ cte4.getComp1().setStr1("n");
+ cte4.setComp2(new Component2());
+ cte4.getComp2().setStr5("m");
em.getTransaction().commit();
@@ -84,9 +92,12 @@
cte1 = em.find(ComponentTestEntity.class, cte1.getId());
cte2 = em.find(ComponentTestEntity.class, cte2.getId());
cte3 = em.find(ComponentTestEntity.class, cte3.getId());
+ cte4 = em.find(ComponentTestEntity.class, cte4.getId());
cte1.setComp2(new Component2("x'", "y'"));
cte3.getComp1().setStr2("b3'");
+ cte4.setComp1(null);
+ cte4.setComp2(null);
em.getTransaction().commit();
@@ -103,6 +114,7 @@
id1 = cte1.getId();
id2 = cte2.getId();
id3 = cte3.getId();
+ id4 = cte4.getId();
}
@Test
@@ -112,6 +124,8 @@
assert Arrays.asList(1, 2,
4).equals(getAuditReader().getRevisions(ComponentTestEntity.class, id2));
assert Arrays.asList(1,
3).equals(getAuditReader().getRevisions(ComponentTestEntity.class, id3));
+
+ assert Arrays.asList(1, 2,
3).equals(getAuditReader().getRevisions(ComponentTestEntity.class, id4));
}
@Test
@@ -146,4 +160,16 @@
assert getAuditReader().find(ComponentTestEntity.class, id3, 3).equals(ver2);
assert getAuditReader().find(ComponentTestEntity.class, id3, 4).equals(ver2);
}
+
+ @Test
+ public void testHistoryOfId4() {
+ ComponentTestEntity ver1 = new ComponentTestEntity(id4, null, null);
+ ComponentTestEntity ver2 = new ComponentTestEntity(id4, new
Component1("n", null), null);
+ ComponentTestEntity ver3 = new ComponentTestEntity(id4, null, null);
+
+ assert getAuditReader().find(ComponentTestEntity.class, id4, 1).equals(ver1);
+ assert getAuditReader().find(ComponentTestEntity.class, id4, 2).equals(ver2);
+ assert getAuditReader().find(ComponentTestEntity.class, id4, 3).equals(ver3);
+ assert getAuditReader().find(ComponentTestEntity.class, id4, 4).equals(ver3);
+ }
}