The mapping is straight forward but why does Hib think the supervisor id is in the user table/class? If I retrieve an employee the supervisor is a valid employee.
Hibernate version:
3.2
Mapping documents:
<hibernate-mapping>
<class name="com.javalobby.tnt.domain2.User" table="USER" lazy="true">
<id name="id" type="long" column="user_id">
<generator class="identity" />
</id>
<discriminator column="type" type="string"/>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<subclass name="com.javalobby.tnt.domain2.Employee" discriminator-value="employee">
<join table="employee">
<key column="employee_id"/>
<property name="jobTitle" column="job_title"/>
<many-to-one name="supervisor" column="supervisor_id" not-null="true" />
</join>
<subclass name="com.javalobby.tnt.domain2.Employer" discriminator-value="employer">
<set name="subordinates" >
<key column="supervisor_id" not-null="true" />
<one-to-many class="com.javalobby.tnt.domain2.Employee"/>
</set>
<join table="employer">
<key column="employer_id"/>
<property name="companyCarBrand" column="company_car_brand"/>
</join>
</subclass>
</subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = HibernateSessionFactory.currentSession();
try {
Employer empr = (Employer)session.load(Employer.class, new Long(1));
System.out.println(empr);
System.out.println(empr.getSubordinates().size());
}
catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
session.close();
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
mysql 5
The generated SQL (show_sql=true):
Hibernate: select employer0_.user_id as user1_0_0_, employer0_.first_name as first3_0_0_, employer0_
.last_name as last4_0_0_, employer0_1_.job_title as job2_1_0_, employer0_1_.supervisor_id as supervi
sor3_1_0_, employer0_2_.company_car_brand as company2_2_0_ from USER employer0_ inner join employee
employer0_1_ on employer0_.user_id=employer0_1_.employee_id inner join employer employer0_2_ on empl
oyer0_.user_id=employer0_2_.employer_id where employer0_.user_id=? and employer0_.type='employer'
Class: class com.javalobby.tnt.domain2.Employer
ID: 1
First Name: R.J.
Last Name: Lorimer
Last Version: 0
Job Title: Master of All
Supervisor: null
Company Car Brand: Ferrari
Hibernate: select subordinat0_.supervisor_id as supervisor5_1_,
subordinat0_.user_id as user1_1_,
subordinat0_.user_id as user1_0_0_,
subordinat0_.first_name as first3_0_0_,
subordinat0_.last_name as last4_0_0_,
subordinat0_1_.job_title as job2_1_0_,
subordinat0_1_.supervisor_id as supervisor3_1_0_,
subordinat0_2_.company_car_brand as company2_2_0_, subordinat0_.type as type0_0_
from USER subordinat0_ inner join employee subordinat0_1_ on subordinat0_.user_id=subordinat0_1_.employee_id left outer join employer subordinat0_2_ on subordinat0_.user_id=subordinat0_2_.employer_id
where subordinat0_.supervisor_id=?
08:06:01,240 WARN JDBCExceptionReporter:77 - SQL Error: 1054, SQLState: 42S22
08:06:01,240 ERROR JDBCExceptionReporter:78 - Unknown column 'subordinat0_.supervisor_id' in 'field
list'
o
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html