-->
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.  [ 4 posts ] 
Author Message
 Post subject: ClassCastException on n-n lazy/not lazy with inheritance
PostPosted: Wed Aug 24, 2005 4:13 pm 
Newbie

Joined: Mon Sep 01, 2003 1:22 pm
Posts: 16
Location: France
Hi,

I'm having a weird ClassCastException here. I have a many-to-many relationship between Category and Product. I create a Software, subclass of Product. When I retrieve a Category and get its Products (which are actually instances of the Software class), I actually get the CGLib enhanced class for Product (and not Software). Hence my ClassCastException when I try to cast to Software.

Actually when I do a toString() on one of the products I get a com.chooss.model.product.software.Software@e488b681 which looks fine. But when I do getClass.getName() I get com.chooss.model.product.Product$$EnhancerByCGLIB$$13db5343 which seems rather weird to me. And this thing can't be cast to a Software.

Hibernate version: 3.0.4

Mapping documents:
<hibernate-mapping>
<class name="com.chooss.model.product.Product" table="s_product">
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>
<discriminator column="disc"/>
<set name="categories" table="s_category_product" lazy="false">
<key column="fk_product"/>
<many-to-many class="com.chooss.model.product.Category"
column="fk_category"/>
</set>
....
<subclass name="com.chooss.model.product.software.Software"
discriminator-value="software">
...
</subclass>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.chooss.model.product.Category" table="s_category">
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>
...
<set name="products" table="s_category_product" lazy="true">
<key column="fk_category"/>
<many-to-many class="com.chooss.model.product.Product"
column="fk_product"/>
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Category parent = categoryMgr.getCategory(parentLongId);
Set children = parent.getProducts();
ArrayList result = new ArrayList(softwares.size());
for (Iterator softIter = softwares.iterator(); softIter.hasNext();) {
Object obj = softIter.next();
System.out.println(obj);
// => com.chooss.model.product.software.Software@e488b681(id = 83)
System.out.println(obj.getClass().getName());
// => com.chooss.model.product.Product$$EnhancerByCGLIB$$13db5343
Software software = (Software) obj; // ClassCastException !!!!
}

Thanks for your help,

Matthieu.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 4:45 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Take a look at this section from the docs: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-proxies. Seems to address what you are seeing.

I think you need to use the proxy attribute in your subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 5:05 am 
Newbie

Joined: Mon Sep 01, 2003 1:22 pm
Posts: 16
Location: France
Yet this article only refers to single-ended associations. In my case I have a many-to-many bidirectional relationship. Is it handled in the same way?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 10:09 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
The docs don't say it directly, but I assume that DomesticCat interface extends Cat interface. So the question would be, in your case, if you had a Product interface and a Software interface that extends it, whether or not Hibernate retrieves the Category's Products as Softwares if they are actually Softwares. Seems like it should. I've never tried it, though. I'd be interested in hearing if it works.


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