Merge pull request #81 from lukasz-antoniak/HHH-5808

HHH-5808 - ObjectNotFoundException for an audited optional association. Envers is creating a proxy even when the association is null.
This commit is contained in:
Adam Warski 2011-05-23 10:26:08 -07:00
commit 7b2e5251d3
3 changed files with 142 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import org.hibernate.envers.tools.Tools;
/** /**
* @author Adam Warski (adam at warski dot org) * @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 { public abstract class AbstractCompositeIdMapper extends AbstractIdMapper implements SimpleIdMapperBuilder {
protected Map<PropertyData, SingleIdMapper> ids; protected Map<PropertyData, SingleIdMapper> ids;
@ -45,6 +46,10 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme
} }
public Object mapToIdFromMap(Map data) { public Object mapToIdFromMap(Map data) {
if (data == null) {
return null;
}
Object ret; Object ret;
try { try {
ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance(); ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance();

View File

@ -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 + "]";
}
}

View File

@ -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);
}
}