0 Replies Latest reply on Mar 26, 2008 2:19 PM by Juan Manuel Medina Apodaca

    JPQL polymorphic query

    Juan Manuel Medina Apodaca Newbie

      I have a class hierarchy of 3 Entities, one of which is the superclass for the others:

      @Entity
      @Table(name="PER")
      @Inheritance(strategy=InheritanceType.JOINED)
      @SequenceGenerator(name="SeqIdPer", sequenceName="SEQIDPER")
      public abstract class Person implements Serializable {
      ...
      }

      @Entity
      @Table(name="PHYPER")
      @PrimaryKeyJoinColumn(name="IDPHYPER")
      public class PhysicalPerson extends Person {
      ..
      }

      @Entity
      @Table(name="MORALPER")
      @PrimaryKeyJoinColumn(name="IDMORALPER")
      public class MoralPerson extends Person {
      ...
      }

      When I execute the following JPQL:

      String strPerQuery = "SELECT OBJECT(per) FROM Person per where per.idPer = ?";
      query=em.createQuery(strPerQuery);
      query.setParameter(1, idPerson);
      Person resPer = (Person)query.getSingleResult();

      I get an object of the class Person, but I cannot downcast it to any of the subclasses. For example, even when I know that the person I sought is an instance of PhysicalPerson, the following cast throws a ClassCastException:

      PhysicalPerson phyPer = (PhysicalPerson )resPer;

      Even more, when I print the runtime class of the object using resPer.getClass(), I get the following class:

      mypackage.Person_$$_javassist_16

      Shouldn't the query be polymorphic, and return an object of the subclass?

      I'm using JBoss 5 beta 4.

      Thanks in advance.