-->
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.  [ 9 posts ] 
Author Message
 Post subject: discriminator from other class??
PostPosted: Thu Jun 01, 2006 6:01 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
hi all

i would like to to a subclass mapping and was wondering if a can use a discriminator from another class??

I have a mapping of table TKURS which has several columns, and a foreign key to table TANGEBOT. There are different types of KURS which i want to define in my subclass mapping.

The type of the KURS/ANGEBOT is defined in table TANGEBOT. This type i would like to use as discriminator in my TKURS hibernate-mapping file.

How does this discriminator tag look like?? I mean how can i define it to refer to the table TANGEBOT. is it possible?

Kind regards
Angela


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:04 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Since your discriminator values are spread out accross multiple tables, you may want to use a formula on your discriminator element. If you look at the tests folder of the Hibernate distribution, there is a folder called discriminator. This test uses a formula-based discriminator to define the subclasses. Your formula will be slightly different as you will need to join 2 tables, but it's the same basic idea.

When you encounter KURS/ANGEBOT, you'll want to use the <join> element to pull in data from the TANGEBOT table. You'll be mixing the table per class hierarchy with table per subclass as defined here:

http://www.hibernate.org/hib_docs/v3/re ... ersubclass

I've done a bit more exotic inheritence mappings before and I think this should do the trick.

Ryan-

P.S. Don't forget to rate! :)

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 10:54 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
Thanks!! That helped me a lot!! :-)

angela


Top
 Profile  
 
 Post subject: help please?
PostPosted: Tue Jun 13, 2006 5:19 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
could you please show your solution (map files) ? I'm having a similar problem and I'm not sure how to use the <join> correctly. thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 5:32 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
Hello!
this is my solution:

Code:
<hibernate-mapping>
    <class name="najsre7.model.Kurs" table="TKURS" schema="NAJSRE7_WEB" discriminator-value="0">
        <id name="id" type="java.lang.Integer">
            <meta attribute="use-in-tostring">true</meta>
            <meta attribute="use-in-equals">true</meta>
            <column name="ILAUFNUMMER" precision="9" scale="0" />
            <generator class="sequence">
                <param name="sequence">SEQ_TKURS</param>
            </generator>
        </id>
       
        <!-- <discriminator
         type="java.lang.Integer"
         formula=" ( SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER) "/>
         -->
       
        <discriminator
         type="java.lang.Integer"
         formula=" CASE 
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=1 THEN 1
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=2 AND (SELECT TAGB.FK_TAART_ILAUFNR FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=1 THEN 2
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=2 AND (SELECT TAGB.FK_TAART_ILAUFNR FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=2 THEN 21
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=3 THEN 3
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=4 AND (SELECT TAGB.FK_TAART_ILAUFNR FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=3 THEN 4
                  WHEN (SELECT TAGB.INUTZERGRUPPE FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=4 AND (SELECT TAGB.FK_TAART_ILAUFNR FROM TANGEBOT TAGB WHERE FK_TANGE_ILAUFNR=TAGB.ILAUFNUMMER)=4 THEN 41
                  ELSE 0
                  END "/>
       
        <timestamp name="mutDatum" column="DMUTDAT" />   
        <many-to-one name="angebot" class="najsre7.model.Angebot" fetch="join">
            <column name="FK_TANGE_ILAUFNR" precision="9" scale="0" not-null="true" />
        </many-to-one>
         <many-to-one name="sportart" class="najsre7.model.Sportart" fetch="join">
            <column name="FK_TSPA_ILAUFNR" precision="9" scale="0" not-null="true" />
        </many-to-one>
        <property name="gruppenName" type="java.lang.String">
            <meta attribute="use-in-tostring">true</meta>
            <meta attribute="use-in-equals">true</meta>
            <column name="SGRUPPE" length="25" not-null="true" />
        </property>
        <property name="kursStatus" type="najsre7.dao.hibernate.userType.immutable.kurs.KursStatusUT" >
            <meta attribute="use-in-tostring">true</meta>
            <meta attribute="use-in-equals">true</meta>
            <column name="ISTATUS" length="3" not-null="true" />
        </property>

       //more common properties
       
        <subclass name="najsre7.model.KursNutzergruppe1" discriminator-value="1">
         <property name="kursArt" type="najsre7.dao.hibernate.userType.immutable.kurs.KursArtUT">
               <meta attribute="use-in-tostring">true</meta>
               <meta attribute="use-in-equals">true</meta>
               <column name="SIPAR1" precision="5" scale="0" not-null="true" />
           </property>
           <property name="unterrichtsDauer" type="najsre7.dao.hibernate.userType.immutable.kurs.UnterrichtsDauerExtendedUT">
               <meta attribute="use-in-tostring">true</meta>
               <meta attribute="use-in-equals">true</meta>
               <column name="SIPAR2" precision="5" scale="0" not-null="true" />
           </property>
           //more properties
       </subclass>
       
       <subclass name="najsre7.model.KursNutzergruppe2_Allgemein" discriminator-value="2">   
          <property name="kursArt" type="najsre7.dao.hibernate.userType.immutable.kurs.KursArtExtendedUT">
               <meta attribute="use-in-tostring">true</meta>
               <meta attribute="use-in-equals">true</meta>
               <column name="SIPAR1" precision="5" scale="0" not-null="true" />
           </property>
          //more properties and so on
       </subclass>
              
    </class>
</hibernate-mapping>


regards angela


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 7:34 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Nice work Angela! Glad it worked out.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject: question...
PostPosted: Tue Jun 13, 2006 7:44 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Can the discriminator COLUMN be defined on a field from a <join> operation?

for example:
<class name="MyClass" table="MYTABLE">
<id name="id" column="ID"/>
<discriminator column="Type" type="long"/>
<join table="TYPES">
<key column="TypeID"/>
<property name="type" column="Type"/>
</join>
</class>

For some reason, this doesn't work, claiming the "Type" column does not exist (for the descriminator to use). If I move the <discriminator> tag below the <join> tag, I can a mapping exception, as if the syntax is incorrect.


Top
 Profile  
 
 Post subject: question...
PostPosted: Tue Jun 13, 2006 7:45 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
(sorry, forgot the 'code' tag)

Can the discriminator COLUMN be defined on a field from a <join> operation?

for example:
Code:
    <class name="MyClass" table="MYTABLE">
        <id name="id" column="ID"/>
        <discriminator column="Type" type="long"/>
        <join table="TYPES">
            <key column="TypeID"/>
            <property name="type" column="Type"/>
        </join>
    </class>

For some reason, this doesn't work, claiming the "Type" column does not exist (for the descriminator to use). If I move the <discriminator> tag below the <join> tag, I can a mapping exception, as if the syntax is incorrect.


Top
 Profile  
 
 Post subject: I've tested your solution
PostPosted: Tue Jun 13, 2006 11:12 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I've tested your solution and it does work, but there are several SQL statements created in order to do this, instead of the straightforward INNER JOIN that would have done the trick, no?
In addition, the formula is SQLServer-specific, which makes portability a problem.
Isn't there a way to do this and cause hibernate to use a simple INNER JOIN?


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

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.