9 Replies Latest reply on Oct 7, 2010 4:46 AM by oxyde42

    Envers, inheritance and javassist proxy

    oxyde42

      Hey,

       

      I have a mapping with single table strategy.

      I have 4 classes, let's say classA, classB extends classA, subClass1 extends ClassB and subclass2 extends ClassB

      All classes are @Audited.

      Basically classA may have children which are of type subClass1 or subClass2.

      The goal of the method getRoot() is to get the root of the whole hierarchy, getParent() just return the direct parent node.

       

       

      public abstract class ClassB extends ClassA{

       

           public abstract ClassA getParent();

       

           public ClassA getRoot(){

                if ( getParent() instanceof ClassB){

                     return getParent().getRoot();

                }

                return getParent();

           }

      }

       

      SubClass1 and SubClass2 have their own implementation of getParent().

       

      My problem is that when i m in the past (using envers) if i call getRoot() on my object of type SubClass1, i get a proxy of the parent. But the proxy is typed as ClassA even if it is a SubClass1 or SubClass2, because getParent()  returns an object casted as ClassA.

      In debug mode i can see the proxy is a ClassA and its 'target' a SubClass1.

       

      I thought i could avoid this problem by eagerly fetching the parent and don t bother with proxies, but envers seems to have a bug and fetch all relations lazyly. I tried to fix that but i dont know how to load the relationship (EntityPersister.instanciate(...) instead of EntityPersister.createProxy(...) ?)

       

      I know using "instance of" is a bad smell, but i have legacy code i can t get rid off for now...

       

      EDIT: To make things maybe a l ittle bit clearer, let s say i have the followings parent/child relationships ClassA -> SubClass1 -> SubClass2.

      I want the root of my instance of SubClass2.

       

      The code works well when i don t use envers (like current data version) because hibernate eagerly fetches the parent of the SubClass2 and cast it as SubClass1 so i can get the parent of SubClass1 which is ClassA. But when i use envers (like yesterday's data version), i get a proxy of the SubClass2's parent which is casted as ClassA instead of SubClass1. So i cannot get the parent of SubClass1!

       

      Any help would be greatly appreciated !

       

      Tested with Hibernate 3.5.1, 3.5.5 , 3.5.6