-->
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.  [ 6 posts ] 
Author Message
 Post subject: Polymorphic mapping not as expected
PostPosted: Tue Dec 21, 2004 8:12 pm 
Newbie

Joined: Wed Aug 27, 2003 8:35 am
Posts: 6
Location: Tampa, FL
You'll find all the debug info below. I've seen a few moderately similar problems posted in the forum, but nothing seems to fix me.

Here's what I've got. A class hierarchy starting with:

Authorization
- UserAuthorization
-- UserActivationAdjustmentAuthorization
-- UserTimebankAdjustmentAuthorization

I would expect to be able to say (and this is what I'm trying):

session.find("from UserAuthorization auth where auth.guid = ?")

[ returning both Activation and Timebank authorizations if the auth.guid condition were not there. ]

Or even

session.load(UserAuthorization.class, authGuid);

Either way I end up with a WrongClassException that states my object is not of the specified subclass. It's saying "Authorization" is the subclass when it's really "UserActivationAdjustmentAuthorization" that is the subclass


Hibernate version: 2.1.7

Mapping documents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.aebn.aia.core.domain.authorization.Authorization" table="AiaAuthorization">
<id name="guid" column="guid" type="java.lang.String">
<generator class="uuid.hex"/>
</id>
<discriminator column="type" force="true" insert="true"/>
<many-to-one name="partner" class="com.aebn.aia.core.domain.Partner" column="partner_guid"/>
<property name="created" type="long" column="created" update="false" insert="false"/>
<subclass name="com.aebn.aia.core.domain.authorization.UserAuthorization">
<many-to-one name="user" class="com.aebn.aia.core.domain.User" column="user_guid"/>
<subclass name="com.aebn.aia.core.domain.authorization.UserActivationAdjustmentAuthorization">
<property name="userActiveState" type="boolean" column="user_active_state"/>
</subclass>
<subclass name="com.aebn.aia.core.domain.authorization.UserTimebankAdjustmentAuthorization">
<property name="userTimebankAdjustment" type="integer" column="user_timebank_adjustment"/>
<property name="userTimebankMediaFormat" type="integer" column="user_timebank_media_format"/>
<property name="userTimebankBalance" type="integer" column="user_timebank_balance"/>
</subclass>
</subclass>
</class>
</hibernate-mapping>



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

UserAuthorization auth;
try {
auth = (UserAuthorization)getHibernateTemplate().find(
"from UserAuthorization auth where auth.guid = ?",
authorizationGuid).get(0);
} catch (ArrayIndexOutOfBoundsException e) {
throw new UnknownAuthorizationException();
}

return (auth);

Full stack trace of any exception that occurs:

[junit] net.sf.hibernate.WrongClassException: Object with id: 4028815a00f816aa0100f816b1240007 was not of the specified s
ubclass: com.aebn.aia.core.domain.authorization.UserAuthorization (Discriminator: com.aebn.aia.core.domain.authorization.User
TimebankAdjustmentAuthorization )
[junit] at net.sf.hibernate.loader.Loader.getInstanceClass(Loader.java:656)
[junit] at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:569)
[junit] at net.sf.hibernate.loader.Loader.getRow(Loader.java:501)
[junit] at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:213)
[junit] at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
[junit] at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
[junit] at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
[junit] at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
[junit] at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
[junit] at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1553)
[junit] at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
[junit] at org.springframework.orm.hibernate.HibernateTemplate$24.doInHibernate(HibernateTemplate.java:506)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:228)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:248)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:497)
[junit] at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:492)
[junit] at com.aebn.aia.core.dao.hibernate.UserAuthorizationDaoImpl.getUserAuthorization(UserAuthorizationDaoImpl.jav
a:60)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:295)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.ja
va:154)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:121)
[junit] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56)
[junit] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:143)
[junit] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
[junit] at $Proxy2.getUserAuthorization(Unknown Source)
[junit] at com.aebn.aia.core.dao.AbstractUserAuthorizationDaoTest.testUserTeimbankAdjustAuthorizationSaved(AbstractUs
erAuthorizationDaoTest.java:80)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

Name and version of the database you are using:

Microsoft SQL Server 2000

The generated SQL (show_sql=true):

[junit] Hibernate: select userauthor0_.guid as guid, userauthor0_.type as type, userauthor0_.user_guid as user_guid, user
author0_.partner_guid as partner_3_, userauthor0_.created as created, userauthor0_.user_active_state as user_act6_, userautho
r0_.user_timebank_adjustment as user_tim7_, userauthor0_.user_timebank_media_format as user_tim8_, userauthor0_.user_timebank
_balance as user_tim9_ from AiaAuthorization userauthor0_ where userauthor0_.type in ('com.aebn.aia.core.domain.authorization
.UserTimebankAdjustmentAuthorization', 'com.aebn.aia.core.domain.authorization.UserAuthorization', 'com.aebn.aia.core.domain.
authorization.UserActivationAdjustmentAuthorization') and ((userauthor0_.guid=? ))

Debug level Hibernate log excerpt:

[junit] 19:06:23,094 DEBUG [hql.QueryTranslator] compiling query
[junit] 19:06:23,094 DEBUG [hql.QueryTranslator] HQL: from com.aebn.aia.core.domain.authorization.UserAuthorization auth
where auth.guid = ?
[junit] 19:06:23,094 DEBUG [hql.QueryTranslator] SQL: select userauthor0_.guid as guid, userauthor0_.type as type, userau
thor0_.user_guid as user_guid, userauthor0_.partner_guid as partner_3_, userauthor0_.created as created, userauthor0_.user_ac
tive_state as user_act6_, userauthor0_.user_timebank_adjustment as user_tim7_, userauthor0_.user_timebank_media_format as use
r_tim8_, userauthor0_.user_timebank_balance as user_tim9_ from AiaAuthorization userauthor0_ where userauthor0_.type in ('com
.aebn.aia.core.domain.authorization.UserTimebankAdjustmentAuthorization', 'com.aebn.aia.core.domain.authorization.UserAuthori
zation', 'com.aebn.aia.core.domain.authorization.UserActivationAdjustmentAuthorization') and ((userauthor0_.guid=? ))
[junit] 19:06:23,094 DEBUG [impl.BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
[junit] 19:06:23,094 DEBUG [hibernate.SQL] select userauthor0_.guid as guid, userauthor0_.type as type, userauthor0_.user
_guid as user_guid, userauthor0_.partner_guid as partner_3_, userauthor0_.created as created, userauthor0_.user_active_state
as user_act6_, userauthor0_.user_timebank_adjustment as user_tim7_, userauthor0_.user_timebank_media_format as user_tim8_, us
erauthor0_.user_timebank_balance as user_tim9_ from AiaAuthorization userauthor0_ where userauthor0_.type in ('com.aebn.aia.c
ore.domain.authorization.UserTimebankAdjustmentAuthorization', 'com.aebn.aia.core.domain.authorization.UserAuthorization', 'c
om.aebn.aia.core.domain.authorization.UserActivationAdjustmentAuthorization') and ((userauthor0_.guid=? ))
[junit] Hibernate: select userauthor0_.guid as guid, userauthor0_.type as type, userauthor0_.user_guid as user_guid, user
author0_.partner_guid as partner_3_, userauthor0_.created as created, userauthor0_.user_active_state as user_act6_, userautho
r0_.user_timebank_adjustment as user_tim7_, userauthor0_.user_timebank_media_format as user_tim8_, userauthor0_.user_timebank
_balance as user_tim9_ from AiaAuthorization userauthor0_ where userauthor0_.type in ('com.aebn.aia.core.domain.authorization
.UserTimebankAdjustmentAuthorization', 'com.aebn.aia.core.domain.authorization.UserAuthorization', 'com.aebn.aia.core.domain.
authorization.UserActivationAdjustmentAuthorization') and ((userauthor0_.guid=? ))
[junit] 19:06:23,094 DEBUG [impl.BatcherImpl] preparing statement
[junit] 19:06:23,094 DEBUG [type.StringType] binding '4028815a00f818790100f81880380007' to parameter: 1
[junit] 19:06:23,094 DEBUG [loader.Loader] processing result set
[junit] 19:06:23,094 DEBUG [type.StringType] returning '4028815a00f818790100f81880380007' as column: guid
[junit] 19:06:23,094 DEBUG [loader.Loader] result row: 4028815a00f818790100f81880380007
[junit] 19:06:23,110 DEBUG [type.StringType] returning 'com.aebn.aia.core.domain.authorization.UserTimebankAdjustmentAuth
orization ' as column: type
[junit] 19:06:23,110 DEBUG [impl.BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
[junit] 19:06:23,110 DEBUG [impl.BatcherImpl] closing statement
[junit] 19:06:23,110 DEBUG [transaction.JDBCTransaction] rollback
[junit] 19:06:23,110 DEBUG [impl.SessionImpl] transaction completion
[junit] 19:06:23,110 DEBUG [transaction.JDBCTransaction] re-enabling autocommit
[junit] 19:06:23,110 DEBUG [impl.SessionImpl] closing session
[junit] 19:06:23,110 DEBUG [impl.SessionImpl] disconnecting session
[junit] 19:06:23,110 DEBUG [impl.SessionImpl] transaction completion


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 4:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is all explained in Hibernate In Action btw.

If you want the concrete type use load with the specific class you expect - otherwise hibernate will obey your wish for having a proxied object which will be of the "lowest" type.

so, if you want something that is of the specific type use session.load(SpecificType.class, id)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 11:25 am 
Newbie

Joined: Wed Aug 27, 2003 8:35 am
Posts: 6
Location: Tampa, FL
I can understand session.load needing to work that way. But what about querying with HQL? Its implied in the hibernate docs that querying for a superclass will return any objects of a subclass.

Querying "from java.lang.Object" would return everything, right?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 11:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it should if you use find + the object has not been loaded before as a proxied one. (but then you can use load to get it typesafe)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 11:46 am 
Newbie

Joined: Wed Aug 27, 2003 8:35 am
Posts: 6
Location: Tampa, FL
Thats exactly what I'm doing:

session.find("from UserAuthorization where auth.guid = ?");

This is a brand new session and I'm still getting the WrongClassException, saying my UserTimebankAdjustmentAuthorization is NOT a UserAuthorization, which it most certainly is.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 1:00 pm 
Newbie

Joined: Wed Aug 27, 2003 8:35 am
Posts: 6
Location: Tampa, FL
The only difference I can see between this and other examples is the multi-level hierarchy. The subclass I am querying for is a subclass of a subclass. Do you think that is causing the exception?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.aebn.aia.core.domain.authorization.Authorization" table="AiaAuthorization" polymorphism="explicit">
<id name="guid" column="guid" type="java.lang.String">
<generator class="uuid.hex"/>
</id>
<discriminator column="type" force="true" insert="true"/>
<many-to-one name="partner" class="com.aebn.aia.core.domain.Partner" column="partner_guid"/>
<property name="created" type="long" column="created" update="false" insert="false"/>
<subclass name="com.aebn.aia.core.domain.authorization.UserAuthorization">
<many-to-one name="user" class="com.aebn.aia.core.domain.User" column="user_guid"/>
<subclass name="com.aebn.aia.core.domain.authorization.UserActivationAdjustmentAuthorization">
<property name="userActiveState" type="boolean" column="user_active_state"/>
</subclass>
<subclass name="com.aebn.aia.core.domain.authorization.UserTimebankAdjustmentAuthorization">
<property name="userTimebankAdjustment" type="integer" column="user_timebank_adjustment"/>
<property name="userTimebankMediaFormat" type="integer" column="user_timebank_media_format"/>
<property name="userTimebankBalance" type="integer" column="user_timebank_balance"/>
</subclass>
</subclass>
</class>
</hibernate-mapping>


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