-->
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: hibernate fetch strategy using one table per class hierarchy
PostPosted: Tue May 17, 2011 5:47 pm 
Newbie

Joined: Wed Feb 09, 2011 8:26 am
Posts: 4
Hello I am using the one table per class hierarchy inheritance mapping to legacy data as described here

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html#inheritance-tableperclass


here is what my mapping roughly looks like

Code:
<class abstract="true" name="note" table="NOTES">
<id name="id" type="long" column="NOTE_ID">
     <generator class="native"/>
</id>
    <discriminator column="NOTE_TYPE" type="string"/>
    <property name="orderId" column="ORDER_ID"/>
    <property name="text" column="TEXT"/>
    <subclass name="PurchaseNote" discriminator-value="PUR" />
    <subclass name="CancelNote" discriminator-value="CAN" />
    <subclass name="RefundNote" discriminator-value="REF" />
</class>


There can be many subclasses of notes to each order id

The Notes are described as associations in a Order Class something like

Code:
<class name="order" table="ORDERS">
<id name="id" type="long" column="ORDER_ID">
     <generator class="native"/>
</id>
.....
<set name="purchaseNotes">
    <key column="orderId" />
    <one-to-many class="PurchaseNote"/>
</set>
<set name="cancelNotes">
    <key column="orderId" />
    <one-to-many class="cancelNote"/>
</set>
<set name="refundNotes">
    <key column="orderId" />
    <one-to-many class="refundNote"/>
</set>

</class>

I can get the associated collections fetched by hibernate if I do the following.

1) Use a "Where" clause in the set definition in the order xml or 2) Use a force="true" as part of the discriminator definition in the notes xml

I can also, in a DAO call through a hibernate session from a session factory, use the HQL "from RefundNote where orderId = ?" or "from Note note where note.class = RefundNote and orderId = ?" to get a list of the correct RefundNote subclasses and populate the list in the Order class by using the setter on refundNotes.

All standard hibernate stuff?

Any of these methods will create the collections firing off 3 queries (one for each collection) to the same table. As the number of subclasses grows this seems ineffiecient....?

I've read lots but can't see any way that hibernate (through fetch strategies, join,subselect,etc) could minimise these calls to a singe db call and still populate my collections.....?

I can get all notes and iterate through in Java to build the collections through class.simpleName inspection but was wondering if I'm missing something hibernate will do.....?

Thanks for any advice


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.