When using the table per subclass using a discriminator inheritance mapping, I could not figure out how to tell Hibernate to lazy load the subclass, despite having "polymorphism=explicit" on the class mapping and "lazy=true" on both class and subclass mappings. All of this and wanting for Hibernate to return the correct subclass of course.
Hence, I am now opting for a different strategy ... That is, the entities that I have, even though there is an inheritance hierarchy, the inheritance hierarchy itself will not be mapped in Hibernate. That is, Hibernate does not know about that one class is a subclass of another, etc.
e.g.:
<class name="Payment" table="PAYMENT" dynamic-update="true">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="assigned"/>
</id>
<property name="amount" column="AMOUNT"/>
</class>
<class name="CreditPayment" table="PAYMENT" dynamic-update="true">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="assigned"/>
</id>
<property name="amount" column="AMOUNT"/>
<join table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" column="CCTYPE"/>
</join>
</class>
So there is one class, which is mapped to the PAYMENT table ... and one or more other classes ( these are subclasses of Payment, but we are not telling Hibernate that they are ) that map to the same table and obtains other columns from another table.
With this, of course:
1) I loose the ability to perform polymorphic queries
2) But I am able to control and prevent the additional join or fetch on the other tables that happens when using any inheritance strategy.
Updating works fine ... although there is one small problem:
If I load Payment with ID of 1, and also load CreditPayment with ID of 1 ( Assuming for the moment that the row in table PAYMENT with ID of 1 is indeed a credit payment ) .... these are two different entities as far as Hibernate is concerned.
Thus, when I mutate the instance of CreditPayment and do a saveOrUpdate(), then I do a get( Payment.class, 1 ) .... Hibernate will return me the instance of Payment that I had earlier before I updated CreditPayment. In short, I know have "stale" data in my session, because Hibernate does not "know" that CreditPayment(1) and Payment(1) belong to the same row in the same table.
Oh well .... I guess I can force a refresh() ... or maybe merge() ... on the instance of Payment, but that would mean that I would have to do that every single time I do a saveOrUpdate() on any of the "subclasses".
|