-->
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.  [ 5 posts ] 
Author Message
 Post subject: BUG: Collection retrieval query does not use discriminator
PostPosted: Wed Nov 09, 2005 11:15 am 
Newbie

Joined: Thu Jun 30, 2005 8:18 am
Posts: 6
Hibernate 3.0.5 & 3.1rc2

Mapping

<class name="ProductDefinition">
...
<set name="contractProducts" inverse="true">
<key column="pde_id"/>
<one-to-many class="ContractProduct"/>
</set>

<set name="specialProducts" inverse="true">
<key column="pde_id"/>
<one-to-many class="SpecialProduct"/>
</set>
</class>

// Hierarchy to single table mapping:
<class name="ProductComponent">
...
<discriminator column="class" type="java.lang.String" length="2"/>

<subclass name="ContractProduct" discriminator-value="CP">
<many-to-one name="productDefinition"
class="ProductDefinition"
column="pde_id"/>
</subclass>

<subclass name="SpecialProduct" discriminator-value="SP">
<many-to-one name="productDefinition"
class="com.tpgmail.spp.model.ProductDefinition"
column="pde_id"/>
</subclass>
</class>

If I retrieve ProductDefinition.contractProducts & ProductDefinition.specialProducts I get a WrongClassException because hibernate does not use the discriminator value retrieving these collections.

The result of this is that both collection retrieval queries return all rows that map to the given parent and there is no discrimination for the correct type.

Are there any workarounds?

These are the generated OneToManyLoader statements:

14:43:10:621 DEBUG (OneToManyLoader.java:106) Static select for one-to-many com.tpgmail.spp.model.ProductDefinition.specialProducts: select specialpro0_.pde_id as pde12_1_, specialpro0_.id as id1_, specialpro0_.id as id0_, specialpro0_.tcn as tcn19_0_, specialpro0_.last_change as last4_19_0_, specialpro0_.last_group_change as last5_19_0_, specialpro0_.dov_id as dov6_19_0_, specialpro0_.pco_id as pco7_19_0_, specialpro0_.sequence as sequence19_0_, specialpro0_.number_of_outlets as number10_19_0_, specialpro0_.pde_id as pde12_19_0_ from spp_product_component specialpro0_ where specialpro0_.pde_id=?
14:43:10:591 DEBUG (OneToManyLoader.java:106) Static select for one-to-many com.tpgmail.spp.model.ProductDefinition.contractProducts: select contractpr0_.pde_id as pde12_1_, contractpr0_.id as id1_, contractpr0_.id as id0_, contractpr0_.tcn as tcn19_0_, contractpr0_.last_change as last4_19_0_, contractpr0_.last_group_change as last5_19_0_, contractpr0_.dov_id as dov6_19_0_, contractpr0_.pco_id as pco7_19_0_, contractpr0_.sequence as sequence19_0_, contractpr0_.number_of_outlets as number10_19_0_, contractpr0_.pde_id as pde12_19_0_, contractpr0_.con_id as con13_19_0_ from spp_product_component contractpr0_ where contractpr0_.pde_id=?


Top
 Profile  
 
 Post subject: Missing discriminator-value?
PostPosted: Wed Nov 09, 2005 6:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You haven't put a discriminator-value on your ProductComponent class. When I had this problem (which admittedly was back with Hibernate 2.1) I added an unused value on my "base" class so that it never got created (something like 'discriminator-value="XX"') and that fixed it.

I don't know if that's the official fix, but it worked for me.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 3:29 am 
Newbie

Joined: Thu Jun 30, 2005 8:18 am
Posts: 6
Hi,

Thanx for the suggestion, unfortunately it does not make a difference for hibernate3. I added the discriminator value to the baseclass but the generated sql is still the same and I still get the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 3:57 am 
Newbie

Joined: Thu Jun 30, 2005 8:18 am
Posts: 6
I got it working using the force attribute for discriminator
Code:
<discriminator column="class" type="java.lang.String" length="2" force="true"/>

There is some information about this option in the documentation but it does not refer to this specific situation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 2:47 pm 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
There's still a problem when using discriminator force="true". The subclass colelctions get read correctly but when you remove all objects from the collection of one subclass, Hibernate deletes all objects of *all* subclasses and not just the subclass in question.

In your example if you want to remove all "contractProducts" associated with a ProductDefinition, you clear the contractProducts set and when you try to save the parent ProductDefinition object, Hibernate deletes all specialProducts too.


The query it executes would look like :
delete from spp_product_component contractpr0_ where contractpr0_.pde_id=?

I hope this issue gets fixed.

Sanjiv


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.