0 Replies Latest reply on Feb 1, 2006 4:14 AM by Charles Ethridge

    Hibernate3 incorrectly returning joined-subclass?

    Charles Ethridge Newbie

      Hi, everyone.

      This has to do with "Mapping Class Inheritance" (Table per subclass) as per the Hibernate In Action book (and other such books):

      In my Hibernate mapping (Person.hbm.xml), I have a class called "Person" (the superclass) and a joined-subclass called "Professional". (Professional extends Person.):

      <hibernate-mapping package="com.eastridgesoftware.hibernate">

      <joined-subclass name="Professional" table="Professional" lazy="true">
      <key column="PersonId" foreign-key="FK3DF3F24777C197A7"/>



      When I run a query such as this:

      List personList = session.createQuery("from Person p where p.userNm='" + userNm + "' and p.userPass='" + password + "'").list();
      Person p = (Person) personList.get(0);

      ...Hibernate (version 3.1.1) returns Professional objects. I've been up-casting the Professional objects to Person, as shown above, but since I stated "from Person" (i.e. the superclass) and not "from Professional" (the <joined-subclass>) in the HQL, shouldn't Hibernate return Person objects, not Professional objects?

      Seems to me that Hibernate is doing an unnecessary join to the subclass, since I only asked for the superclass:

      select person0_.PersonId as PersonId1_ ..., person0_1_.CompanyNm as CompanyNm2_, person0_1_.BizPhoneAreaCd as BizPhone3_2_,..., case when person0_1_.PersonId is not null then 1 when person0_.PersonId is not null then 0 end as clazz_ from BestP1.Person person0_ left outer join BestP1.Professional person0_1_ on person0_.PersonId=person0_1_.PersonId where person0_.UserNm='ben' and person0_.UserPass='ben1'

      Is this normal Hibernate behavior? or is there a way for me to tell Hibernate that I only want the superclass, and avoid the unnecessary join?