7 Replies Latest reply on Mar 31, 2008 3:30 AM by tschlechter

    Proxy-Class

    mistamoasn

      Hello!

      I got a problem which is similar to the following posts (no solution yet provided):
      http://www.jboss.com/index.html?module=bb&op=viewtopic&t=88515
      http://www.jboss.com/index.html?module=bb&op=viewtopic&t=121044

      I have entities which are organized in an inheritance hierarchy (please have a look a the code below). When i execute a query (please look at the stateful session bean below) it returns a proxy-object. But it should be an instance of the concrete entity bean. I am facing this problem only when querying in an inheritance hierarchy. For "normal" entity beans it works fine.

      How can i tell the entity beans in the inheritance hierarchy that the whole hierarchy should be fetched eager without eager fetching.

      I found a (not suitable) workaround when i use the annotation @Proxy(lazy =false) at base class level. With this annotation i get the concrete instance of the entity bean (RfAuthProject-instance with id 483 like in the example below) but all associations of each entity in the hierarchy are eager fetched too. That's a problem because, it gets really slow.

      How can i achieve that only the inheritance hierarchy is fetched and none of the associations?

      Thanks in advance for your help.

      kind regards
      Patrik

      ps.:
      Environment: JDK1.5, JBoss 4.2.1 (with EJB3.0 embedded)

      pps.:
      Example code below:

      @Stateful
      public class MainProjectDTO implements MainProjectLocal, MainProjectRemote
      {
       public Project getProject()
       {
       // trivial example for my problem
       Query query1 = em.createQuery("FROM Project p WHERE p.id = 483");
       Project project = query1.getSingleResult();
      
       // the returned project is a proxy like Project_$$_javassist_16 but
       // should be an instance of RfAuthProject because it really is one.
      
      
       // here lies my problem
       // project with id=483 is definitly a RfAuthProject
       if (project instanceof Project) --> true
       if (project instanceof RfProject) --> false
       if (project instanceof RfAuthProject) --> false
      
       // following class cast fails
       RfAuthProject rfAuthProject = (RfAuthProject) project;
      
      
       return project;
       }
      }
      


      Three entity beans in an inheritance hierarchy.

      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      @Table(name = "project")
      public class Project
      {
      }
      
      @Entity
      @Table(name = "rf_project")
      @Inheritance(strategy = InheritanceType.JOINED)
      @PrimaryKeyJoinColumn(name = "id")
      public class RfProject extends Project
      {
      }
      
      @Entity
      @Table(name = "rf_auth_project")
      @Inheritance(strategy = InheritanceType.JOINED)
      @PrimaryKeyJoinColumn(name = "id")
      public class RfAuthProject extends RfProject
      {
      }
      


        • 1. Re: Proxy-Class
          mistamoasn

          Hi!

          I already figured out what went wrong. The behavior with the proxy-class is right, i misunderstood some thing.

          The stateful session bean gets it's EntityManager (= variable em) injected. While i've been working within the method getProject() (with the instanceof operator) i'm "walking" inside the persistence context. Inside the persistence context there are proxy-objects.

          Well, outside the persistence context the object has the right instance and i can work with it as desired. I had the impression the inheritance hierarchy was not eager fetched due to that.

          Fortunately, everything's fine now...

          kind regards
          Patrik

          • 2. Re: Proxy-Class
            waynebaylor

            i know it doesn't matter in your case, but you only need the @Inheritance on the Project class...

            • 3. Re: Proxy-Class
              mistamoasn

              thanks for the info... i'm going to fix this. it was a relic of a former version ;-)

              • 4. Re: Proxy-Class
                tschlechter

                Hi,

                What if I am forced to put an "instanceof"-test inside the persistence context? Does anybody know a real solution to this problem, not just a work-around? Maybe we should report it as a bug?

                Thank you,
                Tun Schlechter

                • 5. Re: Proxy-Class
                  alrubinger

                  How about Class.isAssignableFrom(Class<?>) ?

                  S,
                  ALR

                  • 6. Re: Proxy-Class
                    tschlechter

                    I just tried to use

                    IdleJob.class.isAssignableFrom(myJob.getClass())

                    instead of

                    myJob instanceof IdleJob

                    unfortunately, both evaluate to false.

                    In fact, myJob _is_ an IdleJob that is a direct SubClass of Job. (BTW instanceof Job evaluates to true).

                    Interestingly, myJob.toString() returns something like:
                    mypackagename.IdleJob@1315153
                    and
                    myJob.getClass().toString() returns something like:
                    mypackagename.Job_$$_javassist_166

                    Where does the correct classname in the toString() experiment come from and why still the instance-test does not work?
                    I am more and more convinced that this is a bug.

                    Greetings,
                    Tun

                    • 7. Re: Proxy-Class
                      tschlechter

                      This seems to be a known Hibernate Bug. See http://opensource.atlassian.com/projects/hibernate/browse/HHH-2477 for more information.