HHH-5142:

Exception when initializing lazy @ManyToMany indexed collection containing not audited entities

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20226 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2010-08-23 14:21:30 +00:00
parent 1065aa72a7
commit c020d25a70
3 changed files with 220 additions and 7 deletions

View File

@ -23,15 +23,15 @@
*/ */
package org.hibernate.envers.entities.mapper.relation.lazy.initializor; package org.hibernate.envers.entities.mapper.relation.lazy.initializor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.mapper.relation.MiddleComponentData; import org.hibernate.envers.entities.mapper.relation.MiddleComponentData;
import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator; import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.hibernate.envers.reader.AuditReaderImplementor; import org.hibernate.envers.reader.AuditReaderImplementor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/** /**
* Initializes a map. * Initializes a map.
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
@ -64,8 +64,10 @@ public class ListCollectionInitializor extends AbstractCollectionInitializor<Lis
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
protected void addToCollection(List collection, Object collectionRow) { protected void addToCollection(List collection, Object collectionRow) {
Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex());
Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, Object element = elementData instanceof Map ?
(Map<String, Object>) elementData, null, revision); elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
(Map<String, Object>) elementData, null, revision)
: elementData ;
Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex());
Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator,
@ -74,4 +76,4 @@ public class ListCollectionInitializor extends AbstractCollectionInitializor<Lis
collection.set(index, element); collection.set(index, element);
} }
} }

View File

@ -0,0 +1,92 @@
package org.hibernate.envers.test.entities.manytomany.unidirectional;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;
import org.hibernate.envers.test.entities.UnversionedStrTestEntity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author Vladimir Klyushnikov
*/
@Entity
public class M2MIndexedListTargetNotAuditedEntity {
@Id
private Integer id;
@Audited
private String data;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@OrderColumn(name = "sortOrder")
@ManyToMany(fetch = FetchType.LAZY)
private List<UnversionedStrTestEntity> references = new ArrayList<UnversionedStrTestEntity>();
public M2MIndexedListTargetNotAuditedEntity() {
}
public M2MIndexedListTargetNotAuditedEntity(Integer id, String data) {
this.id = id;
this.data = data;
}
public M2MIndexedListTargetNotAuditedEntity(Integer id, String data, List<UnversionedStrTestEntity> references) {
this.id = id;
this.data = data;
this.references = references;
}
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<UnversionedStrTestEntity> getReferences() {
return references;
}
public void setReferences(List<UnversionedStrTestEntity> references) {
this.references = references;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
M2MIndexedListTargetNotAuditedEntity that = (M2MIndexedListTargetNotAuditedEntity) o;
//noinspection RedundantIfStatement
if (data != null ? !data.equals(that.data) : that.data != null) return false;
return true;
}
@Override
public int hashCode() {
return data != null ? data.hashCode() : 0;
}
@Override
public String toString() {
return "M2MIndexedListTargetNotAuditedEntity(id = " + id + ", data = " + data + ")";
}
}

View File

@ -0,0 +1,119 @@
package org.hibernate.envers.test.integration.manytomany.unidirectional;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.UnversionedStrTestEntity;
import org.hibernate.envers.test.entities.manytomany.unidirectional.M2MIndexedListTargetNotAuditedEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.List;
import static org.hibernate.envers.test.tools.TestTools.checkList;
import static org.testng.Assert.*;
/**
* A test for auditing a many-to-many indexed list where the target entity is not audited.
*
* @author Vladimir Klyushnikov
* @author Adam Warski
*/
public class M2MIndexedListNotAuditedTarget extends AbstractEntityTest {
private Integer itnae1_id;
private Integer itnae2_id;
private UnversionedStrTestEntity uste1;
private UnversionedStrTestEntity uste2;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(UnversionedStrTestEntity.class);
cfg.addAnnotatedClass(M2MIndexedListTargetNotAuditedEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
uste1 = new UnversionedStrTestEntity("str1");
uste2 = new UnversionedStrTestEntity("str2");
// No revision
em.getTransaction().begin();
em.persist(uste1);
em.persist(uste2);
em.getTransaction().commit();
// Revision 1
em.getTransaction().begin();
uste1 = em.find(UnversionedStrTestEntity.class, uste1.getId());
uste2 = em.find(UnversionedStrTestEntity.class, uste2.getId());
M2MIndexedListTargetNotAuditedEntity itnae1 = new M2MIndexedListTargetNotAuditedEntity(1, "tnae1");
itnae1.getReferences().add(uste1);
itnae1.getReferences().add(uste2);
em.persist(itnae1);
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
M2MIndexedListTargetNotAuditedEntity itnae2 = new M2MIndexedListTargetNotAuditedEntity(2, "tnae2");
itnae2.getReferences().add(uste2);
em.persist(itnae2);
em.getTransaction().commit();
// Revision 3
em.getTransaction().begin();
itnae1.getReferences().set(0, uste2);
itnae1.getReferences().set(1, uste1);
em.getTransaction().commit();
itnae1_id = itnae1.getId();
itnae2_id = itnae2.getId();
}
@Test
public void testRevisionsCounts() {
List<Number> revisions = getAuditReader().getRevisions(M2MIndexedListTargetNotAuditedEntity.class, itnae1_id);
assertEquals(revisions, Arrays.asList(1, 3));
revisions = getAuditReader().getRevisions(M2MIndexedListTargetNotAuditedEntity.class, itnae2_id);
assertEquals(revisions, Arrays.asList(2));
}
@Test
public void testHistory1() throws Exception {
M2MIndexedListTargetNotAuditedEntity rev1 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae1_id, 1);
M2MIndexedListTargetNotAuditedEntity rev2 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae1_id, 2);
M2MIndexedListTargetNotAuditedEntity rev3 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae1_id, 3);
assertTrue(checkList(rev1.getReferences(), uste1, uste2));
assertTrue(checkList(rev2.getReferences(), uste1, uste2));
assertTrue(checkList(rev3.getReferences(), uste2, uste1));
}
@Test
public void testHistory2() throws Exception {
M2MIndexedListTargetNotAuditedEntity rev1 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae2_id, 1);
M2MIndexedListTargetNotAuditedEntity rev2 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae2_id, 2);
M2MIndexedListTargetNotAuditedEntity rev3 = getAuditReader().find(M2MIndexedListTargetNotAuditedEntity.class, itnae2_id, 3);
assertNull(rev1);
assertTrue(checkList(rev2.getReferences(), uste2));
assertTrue(checkList(rev3.getReferences(), uste2));
}
}