-->
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: Issue with multi-level inheritance (table per hierarchy)
PostPosted: Wed Feb 04, 2009 6:56 am 
Newbie

Joined: Wed Feb 04, 2009 6:46 am
Posts: 3
Hibernate version: 3.3.1 GA, with Apache Derby Embedded 10.4.2.0

I started from the sample about inheritance in the book Java Persistence with Hibernate, with a table per hierarchy mapping strategy (see Fig 5.2 on P.199).

I extended the example by adding a RemuneratedAccount class that extends BankAccount.

RemuneratedAccount has a unique property, rate of type float.

So the hierarchy looks like:

Code:
BillingDetails
|- CreditCard
|- BankAccount
   |- RemuneratedAccount


The mapping provided is as follows:

Code:
<hibernate-mapping>
   <class name="testcase.BillingDetails" table="BILLING_DETAILS" discriminator-value="BD">
      <id name="id" column="BD_ID"/>
      <discriminator column="DISC"/>
      <property name="owner" column="BD_OWNER"/>
      <subclass name="testcase.CreditCard" extends="testcase.BillingDetails" discriminator-value="CC">
         <property name="number" column="CC_NUMBER"/>
         <property name="expMonth" column="CC_EXPMONTH"/>
         <property name="expYear" column="CC_EXPYEAR"/>
      </subclass>
      <subclass name="testcase.BankAccount" extends="testcase.BillingDetails" discriminator-value="BA">
         <property name="account" column="BA_ACCOUNT"/>
         <property name="bankName" column="BA_BANKNAME"/>
      </subclass>
      <subclass name="testcase.RemuneratedAccount" extends="testcase.BankAccount" discriminator-value="RA">
         <property name="rate" column="RA_RATE"/>
      </subclass>
   </class>
</hibernate-mapping>


Then, in the code of my application, I populate some instances of RemuneratedAccount and set a value for each property (id, owner, account, bankname, rate), including inherited ones, before saving and commiting them.

When I fetch my instances, the values for account and bankname (each one defined in BankAccount) are null. Every other property value is retrieved back with the right value.

When inserting, the generated JDBC statement is as follows:

insert into BILLING_DETAILS (BD_OWNER, RA_RATE, DISC, BD_ID) values (?, ?, 'RA', ?)

where we can see that columns BA_ACCOUNT and BA_BANKNAME are missing.

Did I do something wrong ? Can someone explain this strange behavior ?

Thanks in advance,

arnoth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:16 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Hi,

It looks like you want RemuneratedAccount to be a subclass of BankAccount, but your mapping shows it as simply being a subclass of BillingDetails.

You should nest the RemuneratedAccount subclass XML within the BankAccount subclass XML to tell Hibernate about the hierarchy.


Hope that helps.


--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:28 am 
Newbie

Joined: Wed Feb 04, 2009 6:46 am
Posts: 3
Hi Sounie,

Thanks for your answer.

I thought that the use of the extends XML attribute was sufficient to express the hierarchy.

I'll try this and tell you of the result.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:37 am 
Newbie

Joined: Wed Feb 04, 2009 6:46 am
Posts: 3
yes it worked.

thanks alot.

So the right mapping is as follows:

Code:
<hibernate-mapping>
   <class name="testcase.BillingDetails" table="BILLING_DETAILS" discriminator-value="BD">
      <id name="id" column="BD_ID"/>
      <discriminator column="DISC"/>
      <property name="owner" column="BD_OWNER"/>
      <subclass name="testcase.CreditCard" extends="testcase.BillingDetails" discriminator-value="CC">
         <property name="number" column="CC_NUMBER"/>
         <property name="expMonth" column="CC_EXPMONTH"/>
         <property name="expYear" column="CC_EXPYEAR"/>
      </subclass>
      <subclass name="testcase.BankAccount" extends="testcase.BillingDetails" discriminator-value="BA">
         <property name="account" column="BA_ACCOUNT"/>
         <property name="bankName" column="BA_BANKNAME"/>
         <subclass name="testcase.RemuneratedAccount" extends="testcase.BankAccount" discriminator-value="RA">
            <property name="rate" column="RA_RATE"/>
         </subclass>
      </subclass>      
   </class>
</hibernate-mapping>


But shouldn't Hibernate trigger an error because there is an inconsistency between the nesting of the hierarchy in the mapping and what is defined in the extends attributes ?


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.