-->
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.  [ 3 posts ] 
Author Message
 Post subject: WrongClassException With 2 Persisters --> 1 Table
PostPosted: Thu Dec 09, 2004 1:32 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
I have a situation where I have two classes, Statement and InProgressStatement that both map to the same table, statement. InProgressStatement extends Statement and adds the context of parsing an electronic statement, but does not add any additional persistent fields. I had to promote InProgressStatement to a persistent class, however, to persist the statement. What I am seeing is that when I have both of these classes registered in my configuration, I get WrongClassExceptions when loading two objects with different types(from different tables) in the same session with the same primary key. If I only register one of the Statement classes, it works fine.

I'd appreciate any clarification.


Hibernate version:

2.1.4

Code between sessionFactory.openSession() and session.close():

Collection results = Statement.findByBank(Bank.findBy(100));//finds bankPK = 100, statementPK = 100 throws Exception

Statement.findByBank:

return session.createCriteria(Statement.class)
.add(Expression.eq("bank", bank))
.list();

Bank.findById:

return (Bank)session.get(Bank.class, id);


Full stack trace of any exception that occurs:

UncheckedDatabaseException: HibernateException detected while executing criteria for findByBank. at Statement.findByBank(Statement.java:623) at StatementTest.testFindByBank(StatementTest.java:265) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)Caused by: net.sf.hibernate.WrongClassException: Object with id: 100 was not of the specified subclass: InProgressStatement (loaded object was of wrong class) at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:456) at net.sf.hibernate.loader.Loader.getRow(Loader.java:423) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:209) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.doList(Loader.java:955) at net.sf.hibernate.loader.Loader.list(Loader.java:946) at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:121) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3609) at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238) at

Name and version of the database you are using:

I am seeing this on both Oracle and SQLServer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 4:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
I get WrongClassExceptions when loading two objects with different types(from different tables) in the same session with the same primary key

Incorrect. This will happen (in 2.1.6 and prior) when, within a single session, Hibernate is asked to load the same database row twice and map it to two seperate classes. This is true whether you explicitly ask it to load a class instance, or if it needs to load a class instance via a mapped association.

Mapping two different classes to the same database row is a bad idea, in general; and prior to 2.1.7 it won't even work if you try to load that row into more than one class as I mentioned above.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 10:13 am 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
Thanks for the explanation. I can see how that would be a problem. I will upgrade to the latest version and see if that solves my problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.