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

    Tree structure not working in Envers

    Felix Scheffer Newbie

      Hi

      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 1

      CategoryEntity 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

        • 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?

           

          Adam