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:

      @SequenceGenerator(name="SeqIdPer", sequenceName="SEQIDPER")
      public abstract class Person implements Serializable {

      public class PhysicalPerson extends Person {

      public class MoralPerson extends Person {

      When I execute the following JPQL:

      String strPerQuery = "SELECT OBJECT(per) FROM Person per where per.idPer = ?";
      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:


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

      I'm using JBoss 5 beta 4.

      Thanks in advance.