2 Replies Latest reply on Sep 8, 2005 12:05 PM by epbernard

    EntityManager.getReference returned object class

    ebu

      As far as i can guess from the spec getReference shuld return an instance of the Class passed to it as first param. Thus, having

      public class A {
       private String name;
      }
      
      ...
      
      A a = em.getReference(A.class, key);
      


      i suppose a.getClass().getDeclaredField("name") to return the appropriate field. But em returns me an instance of mypack.A$$EnhancerByCGLIB$$4f0ecd01 and further i can't find A's fields in it. Will this be fixed in the future releases or we should not expect real instances from getReference?

      wbr, eugen.


        • 1. Re: EntityManager.getReference returned object class
          bill.burke

          AFAIK, Hibernate (the underlying persistence mechanisms) extends your persistence class so that it can "intercept" get/set methods to perform more efficient dirty checking and lazy loading.

          So your best bet is:

          a.getClass().getSuperclass().getDeclaredField("name");

          • 2. Re: EntityManager.getReference returned object class
            epbernard

            This might even fail if by chance the object was already in the session, since the actual A class will be returned.
            Note this is compliant with the spec since mypack.A$$EnhancerByCGLIB$$4f0ecd01 is actually a subclass of A, thus a is an instance of A.

            To do what you want, you can do Hibernate.getClass(a).getDeclaredField()