-->
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: WrongClassException - ?
PostPosted: Mon Nov 10, 2003 9:28 am 
Newbie

Joined: Mon Nov 10, 2003 9:08 am
Posts: 12
Hello all.

When I try to execute the following query, Hibernate throws WrongClassException:

"from my.Item item where item.someProperty is not null"

The Item-related classes are arranged in such way:

my.Item

my.CatalogueItem extends my.Item
my.FullItem extends my.CatalogueItem

my.PriceItem extends my.Item

All these four classes point to one table and no discriminators are used. I realise it's wrong, but question is not about it...

When I execute the mentioned query, Hibernate would give error:

net.sf.hibernate.WrongClassException: Object with id: 2686978 was not of the specified subclass: my.CatalogueItem (loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:300)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:278)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:159)
at net.sf.hibernate.loader.Loader.find(Loader.java:620)
at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1314)

CatalogueItem, FullItem and PriceItem has polymorphism=implicit , but as I understand it has no effect without discriminators. So, what would this error mean?

I can simulate this exception in another way. Build the same scheme with classes and mappings, and:

open session;
ask to load my.Item;
ask to load my.CatalogueItem;

It would load my.Item's all right, but would give the exception on loading CatalogueItem's. As I understand, it's because object from this table with this ID was already loaded before, and is still in the session's cache. However, using evict() doesn't help...

Fine, but this case is different from the previous, where I ask to load only my.Item ... The cache should be empty. That's puzzling...

Why could it happen..?

--Thanks in advance,
yura

P.S. hibernate 2.0.3


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 10:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You havn't shown the actual mappings.


Top
 Profile  
 
 Post subject: mappings
PostPosted: Mon Nov 10, 2003 11:26 am 
Newbie

Joined: Mon Nov 10, 2003 9:08 am
Posts: 12
Item.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="my.Item" table="item">
<id name="id" column="ID" type="long" unsaved-value="any">
<generator class="hilo">
<param name="table">hibernate_unique_key</param>
<param name="column">next_item</param>
</generator>
</id>
<property name="someProperty" column="some_property" type="long"/>
</class>
</hibernate-mapping>

CatalogueItem.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="my.CatalogueItem" table="item">
<!-- Item properties -->
<id name="id" column="ID" type="long" unsaved-value="any">
<generator class="hilo">
<param name="table">hibernate_unique_key</param>
<param name="column">next_item</param>
</generator>
</id>
<property name="someProperty" column="some_property" type="long"/>
<!-- CatalogueItem properties -->
<property name="myCustomProperty" column="my_custom_property" type="string"/>
</class>
</hibernate-mapping>

database:

create table item (
id bigint not null,
some_property bigint,
my_custom_property varchar(255)
);

FullItem and PriceItem have same mappings but with their own custom properties which have corresponding columns in the database.

If you think it'd help, I could post the whole files, but I should say it's a real mess.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 12:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Don't youd mean to use "explicit" polymorphism in these mappings? Have you read the "lightweight class" pattern on the design patterns section of wiki?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:08 am 
Newbie

Joined: Mon Nov 10, 2003 9:08 am
Posts: 12
Understood, thanks. Got mixed up with my own mappings.


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.