-->
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: Discriminator being ignored
PostPosted: Mon Jan 26, 2004 4:28 pm 
Newbie

Joined: Mon Jan 26, 2004 4:13 pm
Posts: 5
I have a base class mapped to a table,

It has two subclasses that have a discriminator on them:

baseclass discriminator="BASE"
subclass1 discriminator="PAY"
subclass2 discriminator="ORDER"


when I call:

session.load(baseclass.class, id);

I get a net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass.

This is what I expect to happen because the row with id = 12 corresponds to a discriminator of ORDER not BASE.


The problem is that when I call

session.load(subclass1.class, id);

Hibernate will return me a valid instance of subclass1 even though the id I pass in belong to a row on the table where the discriminator column reads ORDER.

Why don't I get the same exception when I use the incorrect subclass?

Thanks,

Marco


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:23 pm 
Newbie

Joined: Mon Jan 26, 2004 4:13 pm
Posts: 5
Here is my mapping

<class name="com.BaseClass" table="BASE">

<id column="BASE_ID" name="id" type="long">
<generator class="assigned"/>
</id>

<discriminator column="TYPE_CD" type="string" force="true"/>

<property column="DESCRIPTION" name="description" type="string"/>

<subclass name="com.PAY" discriminator-value="PAY">
</subclass>

<subclass name="com.Order" discriminator-value="ORD">
</subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show the detailed exception message. Are you using a recent Hibernate Version?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:35 pm 
Newbie

Joined: Mon Jan 26, 2004 4:13 pm
Posts: 5
Here is the detailed exception. But again, it is not the exception that I am concerned about. I expected an exception. I am concerned about the second case in which I recieved no exception even though the discriminator for the class I used in the load method PAY and not ORD.



An exception has been caused by: net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.kmart.kima.domain.ActivityPointRole (Discriminator: ORD )
net.sf.hibernate.WrongClassException: Object with id: 12 was not of the specified subclass: com.kmart.kima.domain.ActivityPointRole (Discriminator: ORD )
at net.sf.hibernate.loader.Loader.getInstanceClass(Loader.java:576)
at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:489)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:421)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:204)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:128)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:826)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:846)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:420)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2042)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1916)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I know, just wanted to know if I was reading up in the right part of the source :)


Top
 Profile  
 
 Post subject: Re: Discriminator being ignored
PostPosted: Wed Jan 28, 2004 5:12 am 
Newbie

Joined: Wed Jan 28, 2004 4:54 am
Posts: 1
I can confirm this. I use following mapping:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Site" table="sites" discriminator-value="not null">
<id name="siteID" unsaved-value="-1">
<column name="site_id" not-null="true" />
<generator class="native">
<param name="sequence">sites_site_id_seq</param>
</generator>
</id>

<discriminator column="status" />

<property name="name" />

<subclass name="ActiveSite" discriminator-value="1" />
</class>
</hibernate-mapping>

When I use query 'from ActiveSite', rows with discrminator set to value other than 1 are not returned. But session.load(ActiveSite.class, new Integer(1)) returns row even if discriminator is set to value other than 1. When I enabled logging with debug level, I can see that discriminator column is neither selected nor used in where. Hibernate uses following SQL query for this mapping when ActiveSite.class instance is requested via session.load method:

select activesite0_.site_id as site_id0_, activesite0_.name as name0_ from sites activesite0_ where activesite0_.site_id=?

I think, it should be something like this:

select activesite0_.site_id as site_id0_, activesite0_.name as name0_, activesite0_.status as status0_ from sites activesite0_ where activesite0_.status=1 and activesite0_.site_id=?

Tested on Hibernate 2.1.1.


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.