-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Object with id <value> was not of the specified subclass
PostPosted: Wed Mar 23, 2011 5:25 pm 
Newbie

Joined: Wed Aug 04, 2010 3:58 am
Posts: 12
I'm running into a problem with polymorphic objects. I'll explain my current situation:
- I have a base class called Person
- I have a class called Student which inherits from Person
- I have a class called Teacher which inherits from Person

I have a table for every class, all the base data is stored in the Person's class and table (Firstname, Lastname, etc.). I also have a Student table with a primary key PersonID (which links to the Person table). The Teacher table also has a primary key called PersonID, which also links to the Person table).

Below is the mapping:
Code:
  <class name="Person" table="Persons" lazy="false">

    <!-- Identity mapping -->
    <id name="ID" unsaved-value="0">
      <column name="ID" />
      <generator class="identity" />
    </id>

    <!-- Simple mappings -->
    <property name="Initials" />
    <property name="FirstName" />
    <property name="MiddleName" />
    <property name="LastName" />
    <property name="Gender" />
    <property name="DateOfBirth" type="date" />
    <property name="Street" />
    <property name="HouseNumber" />
    <property name="HouseNumberExtension" />
    <property name="ZipCode" />
    <property name="City" />
    <property name="Phone" />
    <property name="CellPhone" />
    <property name="Email" />
    <property name="CreatedOn" />

    <property name="IsStudent" formula="(SELECT (CASE WHEN (COUNT(*) = 0) THEN 0 ELSE 1 END) FROM Students WHERE Students.PersonID=ID)"/>
    <property name="IsTeacher" formula="(SELECT (CASE WHEN (COUNT(*) = 0) THEN 0 ELSE 1 END) FROM Teachers WHERE Teachers.PersonID=ID)"/>

    <joined-subclass name="Student" table="Students" lazy="false">
      <key column="PersonID" />

    <joined-subclass name="Teacher" table="Teachers" lazy="false">
      <key column="PersonID" />
  </class>


When I call the method List<Person>() on the IQuery interface, it throws the exception:
Code:
Object with id: 2 was not of the specified subclass: Teacher (loading object was of wrong class Student)


The weird part is that when I call the method List<Student>() on the IQuery interface, it doesn't throw any exception. It loads all the data correctly. I can't call List<Teacher>() and I can't call List<Person>() either...

Can someone please give me a clue where to look at? I've tried using HQL but it didn't work either (0 results).

Code:
from Person p where p.class = Person


If I change it to:

Code:
from Person


It throws the same exception as mentioned earlier.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.