title: hibernate fetch strategy when using one table per class hierarchy inheritance
body: <p>Hello
  I am using the one table per class hierarchy inheritance mapping to legacy data as described here</p>
  
  <p><a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html#inheritance-tableperclass" rel="nofollow">http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html#inheritance-tableperclass</a></p>
  
  <p>here is what my mapping roughly looks like</p>
  
  <pre><code>&lt;class abstract="true" name="note" table="NOTES"&gt;
   &lt;id name="id" type="long" column="NOTE_ID"&gt;
       &lt;generator class="native"/&gt;
   &lt;/id&gt;
   &lt;discriminator column="NOTE_TYPE" type="string"/&gt;
   &lt;property name="orderId" column="ORDER_ID"/&gt;
   &lt;property name="text" column="TEXT"/&gt;
   &lt;subclass name="PurchaseNote" discriminator-value="PUR" /&gt;
   &lt;subclass name="CancelNote" discriminator-value="CAN" /&gt;
   &lt;subclass name="RefundNote" discriminator-value="REF" /&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>There can be many subclasses of notes to each order id</p>
  
  <p>The Notes are described as associations in a Order Class something like</p>
  
  <pre><code>&lt;class name="order" table="ORDERS"&gt;
   &lt;id name="id" type="long" column="ORDER_ID"&gt;
       &lt;generator class="native"/&gt;
   &lt;/id&gt;
   .....
  &lt;set name="purchaseNotes"&gt;
      &lt;key column="orderId" /&gt;
      &lt;one-to-many class="PurchaseNote"/&gt;
  &lt;/set&gt;
  &lt;set name="cancelNotes"&gt;
      &lt;key column="orderId" /&gt;
      &lt;one-to-many class="cancelNote"/&gt;
  &lt;/set&gt;
  &lt;set name="refundNotes"&gt;
      &lt;key column="orderId" /&gt;
      &lt;one-to-many class="refundNote"/&gt;
  &lt;/set&gt;
  
  &lt;/class&gt;
  </code></pre>
  
  <p>I can get the associated collections fetched by hibernate if I do the following.</p>
  
  <p>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</p>
  
  <p>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.</p>
  
  <p>All standard hibernate stuff?</p>
  
  <p>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....?</p>
  
  <p>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.....?</p>
  
  <p>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.....?</p>
  
  <p>Thanks for any advice</p>