Tree structure not working in Envers
fscheffer Jan 18, 2013 10:59 AMHi
I am trying to store a simple category tree but after adding a child node, Envers is breaking the relation between the parent node and the grandparent node.
The mapping looks like that:
{code}
@Entity@Audited
public class CategoryEntity {
@Id
private long id;
@OneToMany(cascade = CascadeType.ALL)
@OrderColumn(name = "children_ORDER")
@JoinColumn(name = "parent_id")
@AuditMappedBy(mappedBy = "parent", positionMappedBy = "children_ORDER")
private List<CategoryEntity> children = new LinkedList<CategoryEntity>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
private CategoryEntity parent;
@Column(updatable = false, insertable = false)
@SuppressWarnings("unused")
private Integer children_ORDER = 0;
// getters & setters
}
{code}
Some psydo-code to reproduce the issue:
{code}
// step 1CategoryEntity a = new CategoryEntity();
session.save(a);
session.flush();
// step 2
CategoryEntity b = new CategoryEntity();
a.getChildren().add(b);
session.flush();
// step 3
CategoryEntity c = new CategoryEntity();
b.getChildren().add(c);
session.flush();
{code}
b.parent_id is now null and therefore breaking the relation between a and b.
As far as I understand, BaseEnversCollectionEventListener.generateFakeBidirecationalRelationWorkUnits() creates a CollectionChangeWorkUnit for the owning entity (CategoryEntity b).
For some reason this CollectionChangeWorkUnit fails to load the parent_id value, causing parent_id to be null.
I also tried the above example without @AuditMappedBy. The relations between the entities are now properly stored in the CategoryEntity_CategoryEntity_AUD table
but when I try to retrieve categories with the following code, the "parent" property is always null.
{code}
AuditQueryCreator creator = AuditReaderFactory.get(session).createQuery();AuditQuery query = creator.forEntitiesAtRevision(CategoryEntity.class, revisionNumber);
List<CategoryEntity> list = query.getResultList();
{code}
Hibernate version: 4.1.9.Final
Help appreciated!
Felix