-->
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.  [ 4 posts ] 
Author Message
 Post subject: Discriminator in a joined-subclass
PostPosted: Sat Jul 22, 2006 12:54 am 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Hibernate version:
3.1.3

Mapping documents:
Code:
    <class name="jc.Event" table="events">
        <id name="id" column="id"><generator class="native"/></id>
        <property name="title"/>
       
        <joined-subclass name="jc.Meeting" table="meetings">
            <key column="event_id"/>

            <discriminator column="type"/>
           
            <property name="meetingDetails"/>
           
            <subclass name="jc.FooMeeting" discriminator-value="FOO" />
            <subclass name="jc.BarMeeting" discriminator-value="BAR" />
        </joined-subclass>
       
    </class>

Full stack trace of any exception that occurs:
The content of element type "joined-subclass" must match "(meta*,subselect?, synchronize*,comment?,tuplizer*, key,(property|many-to-one|one-to-one| component|dynamic-component|properties| any|map|set|list|bag|idbag|array|primitive-array)*, joined-subclass*,loader?,sql-insert?,sql-update?, sql-delete?,resultset*,(query|sql-query)*)".

The mapping above is not valid. I cannot use a discriminator in a joined-subclass element.
How can I map this hierarchy of classes? I have a Meeting class which extends the Event class. The Meeting class has some subclasses which do not have any extra fields so I want to discriminate their run-time type using column "type". How is this possible?

Should I use more joined-subclass elements without any property elements inside? I feel that it's not worth to do a database join just for a subclass which does not add anything to the base class.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 23, 2006 9:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you have a discriminator, then use <subclass>, not <joined-subclass>. If you have to join to another table once you've figure out which subclass you're using, then use the <join> tag inside the <subclass> tag.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 11:30 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 10:20 am
Posts: 32
Location: Brazil
tenwit wrote:
If you have a discriminator, then use <subclass>, not <joined-subclass>. If you have to join to another table once you've figure out which subclass you're using, then use the <join> tag inside the <subclass> tag.

Can you please elaborate on this? How do you join to another table once you've figure out which subclass you're using?

I came to this forum looking for the following answer: if I don't need to specify a discriminator value for each (joined) subclass, then how does Hibernate know which class to instantiate when reconstructing an object from the DB?

_________________
Don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 4:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
DanielSerodio wrote:
Can you please elaborate on this? How do you join to another table once you've figure out which subclass you're using?

Inside the subclass' mapping, use the <join> mapping.

DanielSerodio wrote:
if I don't need to specify a discriminator value for each (joined) subclass, then how does Hibernate know which class to instantiate when reconstructing an object from the DB?

If a row in the superclass table has a corresponding row in the joined-subclass table "A", then it's an instance of class A. If instead it has a row in joined-subclass table "B", then it's an instance of class B.

Using the example in section 9.1.2 of the refdocs (section number may have changed, I have an old version of the refdocs):
Code:
<class name="Payment" table="PAYMENT">
    <id name="id" type="long" column="PAYMENT_ID">
        <generator class="native"/>
    </id>
    <property name="amount" column="AMOUNT"/>
    ...
    <joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
        <key column="PAYMENT_ID"/>
        <property name="creditCardType" column="CCTYPE"/>
        ...
    </joined-subclass>
    <joined-subclass name="CashPayment" table="CASH_PAYMENT">
        <key column="PAYMENT_ID"/>
        ...
    </joined-subclass>
    <joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
        <key column="PAYMENT_ID"/>
        ...
    </joined-subclass>
</class>

Given an instance of the Payment superclass, with ID 1, hibernate can check the CREDIT_PAYMENT, CASH_PAYMENT and CHEQUE_PAYMENT tables. It finds value 1 in the PAYMENT_ID column of the CHEQUE_PAYMENT table, then it knows that Payment instance 1 is a ChequePayment.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.