diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java index ea0d28e328..b534cb6806 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java @@ -58,7 +58,9 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme } for (SingleIdMapper mapper : ids.values()) { - mapper.mapToEntityFromMap(ret, data); + if (!mapper.mapToEntityFromMap(ret, data)) { + return null; + } } return ret; diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/EmbeddedIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/EmbeddedIdMapper.java index 092a01659a..d32e0c7f43 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/EmbeddedIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/EmbeddedIdMapper.java @@ -61,9 +61,9 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl mapToMapFromId(data, getter.get(obj)); } - public void mapToEntityFromMap(Object obj, Map data) { + public boolean mapToEntityFromMap(Object obj, Map data) { if (data == null || obj == null) { - return; + return false; } Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyData); @@ -71,11 +71,17 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl try { Object subObj = ReflectHelper.getDefaultConstructor( getter.getReturnType() ).newInstance(); - setter.set(obj, subObj, null); + boolean ret = true; for (IdMapper idMapper : ids.values()) { - idMapper.mapToEntityFromMap(subObj, data); + ret &= idMapper.mapToEntityFromMap(subObj, data); } + + if (ret) { + setter.set(obj, subObj, null); + } + + return ret; } catch (Exception e) { throw new AuditException(e); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/IdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/IdMapper.java index de3db3a7e9..2bcef0e8bb 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/IdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/IdMapper.java @@ -34,7 +34,12 @@ public interface IdMapper { void mapToMapFromEntity(Map data, Object obj); - void mapToEntityFromMap(Object obj, Map data); + /** + * @param obj Object to map to. + * @param data Data to map. + * @return True if data was mapped; false otherwise (when the id is {@code null}). + */ + boolean mapToEntityFromMap(Object obj, Map data); Object mapToIdFromEntity(Object data); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java index e947a17aa3..a215a3f8cb 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java @@ -47,10 +47,13 @@ public class MultipleIdMapper extends AbstractCompositeIdMapper implements Simpl mapToMapFromId(data, obj); } - public void mapToEntityFromMap(Object obj, Map data) { + public boolean mapToEntityFromMap(Object obj, Map data) { + boolean ret = true; for (IdMapper idMapper : ids.values()) { - idMapper.mapToEntityFromMap(obj, data); + ret &= idMapper.mapToEntityFromMap(obj, data); } + + return ret; } public IdMapper prefixMappedProperties(String prefix) { diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/SingleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/SingleIdMapper.java index fc0728c057..5c4e8b4d86 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/SingleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/SingleIdMapper.java @@ -53,13 +53,20 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu this.propertyData = propertyData; } - public void mapToEntityFromMap(Object obj, Map data) { + public boolean mapToEntityFromMap(Object obj, Map data) { if (data == null || obj == null) { - return; + return false; + } + + Object value = data.get(propertyData.getName()); + if (value == null) { + return false; } Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData); - setter.set(obj, data.get(propertyData.getName()), null); + setter.set(obj, value, null); + + return true; } public Object mapToIdFromMap(Map data) { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UnidirectionalMulIdWithNulls.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UnidirectionalMulIdWithNulls.java index 722f1d2a3f..7a2b629429 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UnidirectionalMulIdWithNulls.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UnidirectionalMulIdWithNulls.java @@ -9,6 +9,8 @@ import org.junit.Test; import javax.persistence.EntityManager; +import static org.junit.Assert.*; + /** * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ @@ -42,14 +44,14 @@ public class UnidirectionalMulIdWithNulls extends AbstractEntityTest { @Test public void testNullReference() { UniRefIngMulIdEntity nullRef = getAuditReader().find(UniRefIngMulIdEntity.class, 2, 1); - assert nullRef.getReference() == null; + assertNull(nullRef.getReference()); } @Test public void testNotNullReference() { EmbIdTestEntity eite = getAuditReader().find(EmbIdTestEntity.class, ei, 1); UniRefIngMulIdEntity notNullRef = getAuditReader().find(UniRefIngMulIdEntity.class, 1, 1); - assert notNullRef.getReference() != null; - assert notNullRef.getReference().equals(eite); + assertNotNull(notNullRef.getReference()); + assertEquals(notNullRef.getReference(), eite); } } \ No newline at end of file