I map some objects using Hibernate 3 for my project and just explain that I have a view of this:
Student entity (tstudent table)UniversityStudent entity (diversity table)University entity (diversity table)
UniversityStudent extends from Student and has its own attributes, such as the university itself, which is the foreign key in the tuniversitystudent table. It also appears as a subclass of the Student class using the discriminator field:
<class name="mycompany.Student" table="tstudent" discriminator-value="BASIC"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <discriminator column="type" /> <property name="name" column="name" /> <property name="surname" column="surname" /> <property name="phoneNumber" column="phone_number" /> <subclass discriminator-value="UNIVERSITY" name="mycompany.UniversityStudent"> <join table="tuniversitystudent"> <key column="id_student" /> <many-to-one name="university" class="mycompany.University"> <column name="id_university" /> </many-to-one> </join> </subclass> </class>
Well, now I want to have a Set collection with UniversityStudent objects for each University . Therefore, I find it as follows:
<class name="mycompany.University" table="tuniversity"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" column="name" /> <set name="universityStudents" table="tuniversitystudent"> <key> <column name="id_university" /> </key> <one-to-many class="mycompany.UniversityStudent" /> </set> </class>
My problem occurs when I want to load a University object, Hibernate complains that id_university does not exist in the tstudent table. I checked the generated SQL query and it is really trying to load it from tstudent.
Unknown column 'student0_.id_university' in 'list of fields'
It seems that he recognizes that he is a subclass of the main Student and tries to join the collection using the field in the parent table, but, however, the field is indeed in the child table, since only university students can have the university appointed.
I tried another workaround that seems to work, but it is not valid for me that displaying UniversityStudent as joined-subclass instead of subclass with join inside:
<joined-subclass name="mycompany.UniversityStudent" table="tuniversitystudent"> <key column="id_student" /> <many-to-one name="university" class="mycompany.University"> <column name="id_university" /> </many-to-one> </joined-subclass>
However, I am interested in saving it as a subclass with a discriminator value. Any idea?