Hallo you experts :)
I could use some help:
The DB design is: a person can be a student or a lecturer. both are unitied in one table with a discriminator. Both have a CourseStudy, of course in a different role. So both classes have their own man-to-one relation.
(btw, the pojos and the hbm are generated)
It is working fine except the query (see below)
Hibernate version: Hibernate 3.1.3
RDBMSPostgreSQL
Person.hbm.xmlCode:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.my" auto-import="true" default-access="field">
<class name="Person" discriminator-value="P" mutable="true" dynamic-update="false" dynamic-insert="false" select-before-update="false" lazy="false" abstract="false">
<id name="id" access="property">
<column name="id" sql-type="INT8" not-null="true" length="8"/>
<generator class="native">
</generator>
</id>
<discriminator type="char" force="false" insert="true">
<column name="discriminator"/>
</discriminator>
<property name="firstname" insert="true" update="true" optimistic-lock="true">
<column name="firstname" sql-type="VARCHAR(254)" not-null="true" length="254"/>
</property>
<property name="lastname" insert="true" update="true" optimistic-lock="true">
<column name="lastname" sql-type="VARCHAR(254)" not-null="true" length="254"/>
</property>
<property name="phone" insert="true" update="true" optimistic-lock="true">
<column name="phone" sql-type="VARCHAR(254)" length="254"/>
</property>
<property name="email" insert="true" update="true" optimistic-lock="true">
<column name="email" sql-type="VARCHAR(254)" length="254"/>
</property>
<property name="discriminator" insert="false" update="false" lazy="false" optimistic-lock="true">
<column name="discriminator" sql-type="CHAR(1)" length="1"/>
</property>
<many-to-one name="Address" cascade="all" access="property" class="org.my.Address" outer-join="false" update="true" insert="true">
<column name="Add_id" sql-type="INT4" not-null="false" length="4"/>
</many-to-one>
<subclass name="Lecturer" discriminator-value="L" dynamic-update="false" dynamic-insert="false" select-before-update="false" lazy="true" abstract="false">
<property name="officeNumber" insert="true" update="true" optimistic-lock="true">
<column name="officeNumber" sql-type="VARCHAR(254)" length="254"/>
</property>
<property name="consultation" insert="true" update="true" optimistic-lock="true">
<column name="consultation" sql-type="VARCHAR(254)" length="254"/>
</property>
<set name="Course" lazy="true" optimistic-lock="true">
<key on-delete="noaction" unique="false">
<column name="Lec_id" sql-type="INT8" not-null="false" length="8"/>
</key>
<one-to-many class="Course"/>
</set>
<many-to-one name="CourseStudy" access="property" class="org.my.CourseStudy" update="true" insert="true">
<column name="Cou_id" sql-type="INT4" not-null="false" length="4"/>
</many-to-one>
<many-to-one name="LecturerType" access="property" class="org.my.LecturerType" update="true" insert="true">
<column name="Lec_id" sql-type="INT4" not-null="false" length="4"/>
</many-to-one>
</subclass>
<subclass name="Student" discriminator-value="S" dynamic-update="false" dynamic-insert="false" select-before-update="false" lazy="true" abstract="false">
<property name="matriculationNumber" insert="true" update="true" optimistic-lock="true">
<column name="matriculationNumber" sql-type="VARCHAR(254)" length="254"/>
</property>
<property name="matriculationDate" insert="true" update="true" optimistic-lock="true">
<column name="matriculationDate" sql-type="DATE"/>
</property>
<set name="CourseAssignment" lazy="true" optimistic-lock="true">
<key on-delete="noaction" unique="false">
<column name="Stu_id" sql-type="INT8" not-null="false" length="8"/>
</key>
<one-to-many class="org.my.CourseAssignment"/>
</set>
<many-to-one name="CourseStudy" access="property" class="org.my.CourseStudy" update="true" insert="true">
<column name="Cou_id2" sql-type="INT4" not-null="false" length="4"/>
</many-to-one>
</subclass>
</class>
</hibernate-mapping>
Problematic codeCode:
The List is always empty. As well as I remove the second crit.
Session session = HibernateSessionManager.getSession(true);
Criteria crit = session.createCriteria(Lecturer.class);
crit.setFetchMode("CourseStudy", FetchMode.JOIN).setFetchMode("Faculty", FetchMode.JOIN);
Criteria cr2 = crit.createCriteria("CourseStudy");
Criteria cr1 = cr2.createCriteria("Faculty");
List list = crit.list();
The same thing with the students works fine!!!
Code:
Session session = HibernateSessionManager.getSession(true);
Criteria crit = session.createCriteria(Student.class);
crit.setFetchMode("CourseStudy", FetchMode.JOIN).setFetchMode("Faculty", FetchMode.JOIN);
Criteria cr2 = crit.createCriteria("CourseStudy");
Criteria cr1 = cr2.createCriteria("Faculty");
List list = crit.list();
this ist how the restrictions look like
Code:
if (rest2 != null)
cr2.add(Restrictions.idEq(crit2.getId()));
if (rest1 != null)
cr1.add(Restrictions.idEq(crit1.getId()));
What I want to do is to join over 3 tables: Person>CourseStudy>Faculty in order to filter with 2 restrictions (CourseStudy and Faculty)
Pls help!
Here a link to the same topic just in german
click