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-tableperclasshere 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