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 86cdc73a30..ea0d28e328 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 @@ -29,6 +29,7 @@ import org.hibernate.envers.tools.Tools; /** * @author Adam Warski (adam at warski dot org) + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper implements SimpleIdMapperBuilder { protected Map ids; @@ -45,6 +46,10 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme } public Object mapToIdFromMap(Map data) { + if (data == null) { + return null; + } + Object ret; try { ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance(); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UniRefIngMulIdEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UniRefIngMulIdEntity.java new file mode 100644 index 0000000000..8ff029fc20 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UniRefIngMulIdEntity.java @@ -0,0 +1,82 @@ +package org.hibernate.envers.test.integration.onetoone.unidirectional; + +import org.hibernate.envers.Audited; +import org.hibernate.envers.test.entities.ids.EmbIdTestEntity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +public class UniRefIngMulIdEntity { + @Id + private Integer id; + + @Audited + private String data; + + @Audited + @OneToOne + private EmbIdTestEntity reference; + + public UniRefIngMulIdEntity() { + } + + public UniRefIngMulIdEntity(Integer id, String data, EmbIdTestEntity reference) { + this.id = id; + this.data = data; + this.reference = reference; + } + + 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 EmbIdTestEntity getReference() { + return reference; + } + + public void setReference(EmbIdTestEntity reference) { + this.reference = reference; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof UniRefIngMulIdEntity)) return false; + + UniRefIngMulIdEntity that = (UniRefIngMulIdEntity) 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; + } + + @Override + public int hashCode() { + int result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UniRefIngMulIdEntity[id = " + id + ", data = " + 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 new file mode 100644 index 0000000000..722f1d2a3f --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/unidirectional/UnidirectionalMulIdWithNulls.java @@ -0,0 +1,55 @@ +package org.hibernate.envers.test.integration.onetoone.unidirectional; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.ids.EmbId; +import org.hibernate.envers.test.entities.ids.EmbIdTestEntity; +import org.junit.Test; + +import javax.persistence.EntityManager; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class UnidirectionalMulIdWithNulls extends AbstractEntityTest { + private EmbId ei; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(EmbIdTestEntity.class); + cfg.addAnnotatedClass(UniRefIngMulIdEntity.class); + } + + @Test + @Priority(10) + public void initData() { + ei = new EmbId(1, 2); + + EntityManager em = getEntityManager(); + + // Revision 1 + EmbIdTestEntity eite = new EmbIdTestEntity(ei, "data"); + UniRefIngMulIdEntity notNullRef = new UniRefIngMulIdEntity(1, "data 1", eite); + UniRefIngMulIdEntity nullRef = new UniRefIngMulIdEntity(2, "data 2", null); + + em.getTransaction().begin(); + em.persist(eite); + em.persist(notNullRef); + em.persist(nullRef); + em.getTransaction().commit(); + } + + @Test + public void testNullReference() { + UniRefIngMulIdEntity nullRef = getAuditReader().find(UniRefIngMulIdEntity.class, 2, 1); + assert nullRef.getReference() == null; + } + + @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); + } +} \ No newline at end of file