-->
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: Alternative to table per subclass using a discriminator
PostPosted: Tue Sep 23, 2008 8:32 am 
Newbie

Joined: Wed Sep 17, 2008 2:59 am
Posts: 10
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".


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.