1 Reply Latest reply on Jan 30, 2013 4:09 PM by Adam Warski

    Tree structure not working in Envers

    Felix Scheffer Newbie


      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:




      public class CategoryEntity {



          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)


          private Integer                  children_ORDER = 0;


          // getters & setters



      Some psydo-code to reproduce the issue:


      // step 1

      CategoryEntity a  = new CategoryEntity();




      // step 2

      CategoryEntity b  = new CategoryEntity();




      // step 3

      CategoryEntity c  = new CategoryEntity();




      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.


      AuditQueryCreator creator = AuditReaderFactory.get(session).createQuery();

      AuditQuery query = creator.forEntitiesAtRevision(CategoryEntity.class, revisionNumber);

      List<CategoryEntity> list = query.getResultList();


      Hibernate version: 4.1.9.Final


      Help appreciated!



        • 1. Re: Tree structure not working in Envers
          Adam Warski Master

          Hmm you definitely need the verison with @AuditMappedBy, as the @OneToMany annotation specifies that it own the relation. So you only use @AMB when also using mappedBy attribute on a relation-annotation.


          Regarding the null parent, that probably has sth to do with inseratble=false, updateable=false. Can you create a bug report for that?