-->
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.  [ 8 posts ] 
Author Message
 Post subject: Inheritance mapping works with HQL but not with association
PostPosted: Fri Oct 15, 2004 10:41 am 
Newbie

Joined: Fri Oct 15, 2004 9:42 am
Posts: 3
Have a question regarding results I'm observing for an inheritance mapping (using the hierarchy in a single table approach, Hibernate version 2.1.6). With the mapping below for a Closeout, I am able to successfully retrieve subclass instances via HQL. However, when I try to load them via one-to-many assocation from another object, the discriminator value for the subclass seems to be ignored and Hibernate appears to retrieve all instances of the parent class.

Here's the mapping:

Code:
<hibernate-mapping package="com.nlg.nlgv.paris.model">
   <class name="Restriction" table="calendar_rule_vw" discriminator-value="0">
      <id name="id" column="rule_id">
         <generator class="native"/>
      </id>
      <discriminator column="predicate_type_id" type="integer"/>
      
      <property name="targetId" column="rule_target_id"/>
      <property name="description" column="rule_desc"/>
      <property name="status" column="status"/>
      <property name="updated" column="updt_timestamp"/>
   </class>
   
   <subclass name="Closeout"
      extends="com.nlg.nlgv.paris.model.Restriction" discriminator-value="18">
      <property name="travelStart" column="argument_1" type="date"/>
      <property name="travelEnd" column="argument_2" type="date"/>
   </subclass>
</hibernate-mapping>


The following HQL call (wrapped in Spring):

Code:
return getHibernateTemplate().find("from Closeout c where c.targetId=?", rateCodeId);


results in this SQL (columns omitted for clarity):

Code:
select *
from calendar_rule_vw closeout0_
where closeout0_.predicate_type_id=18
and ((closeout0_.rule_target_id=? ))


This works as I'd expect - the discriminator column is used to return only rows where predicate_type_id=18 and only instances of the Closeout subclass are returned. So far so good.

I also reference Closeout via one-to-many assocation from a RateCode using this mapping:

Code:
<hibernate-mapping package="com.nlg.nlgv.paris.model">
   <class name="RateCode" table="p_rate_code">
      <id name="id" column="rate_code_id">
         <generator class="native"/>
      </id>
      <property name="code" column="external_rate_code"/>
      <property name="private" column="is_private"/>
      <property name="status" column="status"/>
      <property name="description" column="rate_code_desc"/>
      <many-to-one name="rateCodeBase" class="RateCodeBase" column="rate_code_base_id"/>
      <many-to-one name="ratePlan" class="RatePlan" column="rate_plan_id"/>
      <many-to-one name="rateDerivation" class="RateDerivation" column="rate_derivation_id"/>
      <set name="closeouts" lazy="true">
         <key column="rule_target_id"/>
         <one-to-many class="com.nlg.nlgv.paris.model.Closeout"/>
      </set>
      <property name="created" column="create_timestamp"/>
      <property name="updated" column="updt_timestamp"/>
      <property name="createUserId" column="create_userid"/>
      <property name="updateUserId" column="updt_userid"/>
   </class>
</hibernate-mapping>


When I call RateCode.getCloseouts(), I would expect to get SQL similar to that above that uses the discriminator to select only Closeouts (again, those rows where predicate_type_id=18). Instead, I get the following:

Code:
select *
from calendar_rule_vw closeouts0_
where closeouts0_.rule_target_id=?


In this case, the discriminator column is not used and I can't figure out why. The success with HQL leads me to believe that the polymorphic mapping is correct. So perhaps there is something amiss with the mapping for RateCode? (Though it seems straightforward.) Any insight is much appreciated, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 12:00 pm 
Beginner
Beginner

Joined: Fri Aug 13, 2004 3:07 pm
Posts: 44
Check out the documentation here.

http://www.hibernate.org/hib_docs/refer ... tance.html

I am not sure what you need, but since you are looking for a polymorphic one-to many association, there is some discussion there and also in Christian's book Hibernate in Action.

_________________
Thanks
Sameet


Top
 Profile  
 
 Post subject: This should work ....
PostPosted: Fri Oct 15, 2004 1:07 pm 
Beginner
Beginner

Joined: Fri Aug 13, 2004 3:07 pm
Posts: 44
Here's is something that should work

<set name="closeouts" lazy="true" where="predicate_type_id=18">
<key column="rule_target_id"/>
<one-to-many class="com.nlg.nlgv.paris.model.Closeout"/>
</set>

This will make sure that the subclass is retrieved. As you add subclasses to this you can add OR in the where clause for the set

_________________
Thanks
Sameet


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:08 pm 
Newbie

Joined: Fri Oct 15, 2004 9:42 am
Posts: 3
I think this workaround will do the trick (this approach also gives me a way to deal with other issues regarding bizarre relationships in our database schema that I'd like to map polymorphically). Still not sure why the mapped discriminator isn't being applied automatically, however. Anyway, thanks Sameet - this helps a bunch.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 1:54 pm 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
Actually this workaround of adding doens't work. It restricts the Set "closeouts" to only the records with discriminator value 18 in your example however if you remove all objects from this set and try to save the RateCode object, hibernate simply issues

delete from calendar_rule_vw closeouts0_ where closeouts0_.rate_code_id=?

In other words, it deletes the entrie table and other subclass records as well.

It should really be issuing
delete from calendar_rule_vw closeouts0_ where closeouts0_.rate_code_id=? and closeouts0_.predicate_type_id=18


This is a serious issue with Hibernate. There seems to be JIRA entries for this . Here's one : http://opensource.atlassian.com/project ... e/HHH-1134


Vote for it!

Sanjiv


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 16, 2007 8:10 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Anyone knows if the issue
http://opensource.atlassian.com/project ... e/HHH-1134

would be fixed anytime soon? If this bug is not valid, then any suggestion for workaround?

This issue is still there in the latest release of Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 2:19 am 
Beginner
Beginner

Joined: Fri May 14, 2004 9:50 am
Posts: 28
Hello,

I don't know if you've got a suitable response, but it will serve for other peoples.

When you use a discriminator column you can add the property "force", to force hibernate to use the discriminator in all the requests :

<discriminator column="your_column" type="string" force="true"/>

_________________
Eric

http://www.viaxoft.com
http://blog.viaxoft.net


Top
 Profile  
 
 Post subject: Re: Inheritance mapping works with HQL but not with association
PostPosted: Sun Apr 03, 2011 12:23 pm 
Newbie

Joined: Tue May 18, 2010 5:39 am
Posts: 19
I have listed a little explanation here : http://anshuiitk.blogspot.com/2011/04/h ... ption.html

_________________
AG
http://anshuiitk.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

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.