-->
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: Vererbung mit discriminator-Spalte zeigt seltsames Verhalten
PostPosted: Sun Sep 24, 2006 7:07 am 
Newbie

Joined: Mon Nov 28, 2005 12:07 pm
Posts: 17
Also ich hab hier eine typische Vererbung Person>Student Person>Lehrer mit einer Tabelle und einem Discriminator realisiert. Das funktioniert soweit auch wie vorgestellt. Die Pojos und hbm Files wurden generiert, deswegen fangen auch ein paar Attribute dummerweise mit einem Grossbuchstaben an, z.B. CourseStudy
Genau das Element (many-to-one) ist auch das Problem, denn der Query mit dem Criteria liefert stehts eine leere Liste (siehe unten).
Hibernate version: Hibernate 3.1.3
RDBMSPostgreSQL
Person.hbm.xml
Code:
<!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 code
Code:
      
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();

Seltsamerweise funktioniert das Gegenstueck mit den Stunden einwandfrei:
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();

Die Restrictions hab ich der Einfachheit weggelassen:
Code:
if (rest2 != null)
   cr2.add(Restrictions.idEq(crit2.getId()));
if (rest1 != null)
   cr1.add(Restrictions.idEq(crit1.getId()));

Die Datenbank hat auch Lehrer. Weiss echt nicht, warum das nicht geht. Fuer mich sieht dass nach einem Bug aus.

Was ich mit dem Query bezwecken will:
Ein Student/Lehrer hat ein Fach (CourseStudy) und ein Fach hat eine Fachbereich (Faculty). Ueber diesen Verbund will sowohl nach Kursen (restriction1) als auch Fachbereichen (rest2) filtern. Und ja, es sind auch Daten in der Datenbank ;)

Bitte helft mir!


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.