Menu

Lazy polymorphic associations and collections

Help
2008-02-14
2013-04-25
  • Rafael Cordones Marcos

    Hi!

    First of all, thanks for such a handy and useful library! I have started using in conjunction with Hibernate and XStream to generate XML DTOs for a web application.

    I have nevertheless run into a problem that has to do with lazy polymorphic associations and collections.
    See this link for background information:
    http://www.hibernate.org/280.html

    In summary, given the following scenario:

    class A { ... }
    class B extends A { ... }
    class C extends A { ... }

    class Foo {
      String name;
      A aProperty;
      List aList;
      ...
    }

    Foo.a and Foo.aList are both mapped in Hibernate as lazy and when using HibernateBeanReplicator to create a DTO the proxies are resolved to objects of class A and not B or C. Therefore, I get this

    More specifically, the proxy objects that Hibernate generates at runtime are of type A$CGLIB_ENHANCED and since the class net.sf.beanlib.hibernate.UnEnhancer uses the method getSuperclass() to "resolve" the proxy, the objects are not resolved correctly. I *always* get this

    <Foo>
      <name>blah!</name>
      <aProperty>
        <A>...</A>
      </aProperty>
      <aList>
        <A>...</A>
        <A>...</A>
        <A>...</A>
        ...
        <A>...</A>
      </aList>
    </Foo>

    and not

    <Foo>
      <name>blah!</name>
      <aProperty>
        <B>...</B>
      </aProperty>
      <aList>
        <C>...</C>
        <B>...</B>
        <B>...</B>
        <C>...</C>
        ...
        <B>...</B>
      </aList>
    </Foo>

    The only way this can be solved is by letting Hibernate hit the database and resolve the proxy accordingly. AFAIK this can only be done by calling one of the proxied object's methods.

    My question is, would there be a way to provide a custom UnEnhacer to HibernateBeanReplicator that would use reflection to invoke a method in the object (all my object model objects have a getId() method) and thus correctly resolve the proxy?

    Thanks again for the beanlib library and for your time!

    /rafa

     
    • Hanson Char

      Hanson Char - 2008-02-14

      Rafael,

      It would be nice if some kind of unit test can be created for these cases. 

      Anyway, I am wondering if digging out the original persistent class from the HibernateProxy object would help.  I have put in a new method UnEnhancer.getActualClass to achieve that, together with the related changes to other classes.

      Would you be able to check out the latest from SVN and let me know if that helps ?

       
    • Hanson Char

      Hanson Char - 2008-02-14

      Alternatively, you can download the latest bundle from

        http://beanlib.sourceforge.net/tgz/beanlib-3.3.0alpha12.tar.gz

       

Log in to post a comment.