-->
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.  [ 2 posts ] 
Author Message
 Post subject: polymorphism in one to many (WrongClassException)
PostPosted: Thu Dec 22, 2005 9:29 pm 
Newbie

Joined: Thu Dec 22, 2005 7:13 am
Posts: 1
i create a superclass
Code:
class A{}


and 2 subclass of it
Code:
class AB extends A{}
class AC extends A{}

with mapping of
Code:
  <class polymorphism="implicit" table="a" name="A">
    <id unsaved-value="null" name="id">
      <generator class="native"/>
    </id>
    <discriminator column="type"/>
    <subclass name="AB" discriminator-value="ab"/>
    <subclass name="AC" discriminator-value="ac"/>
  </class>

and i use a collection of AB and AC in other class, namely
Code:
class X
{
private Set ab;
private Set ac;
public Set getAbs(){...}
public Set getAcs(){...}
}

with mapping of
Code:
  <class table="x" name="X">
    <id unsaved-value="null" name="id">
      <generator class="native"/>
    </id>
      <set inverse="true" cascade="save-update" lazy="true" name="abs">
        <key column="id_of_a"/>
        <one-to-many class="AB"/>
      </set>
      <set inverse="true" cascade="save-update" lazy="true" name="acs">
        <key column="id_of_a"/>
        <one-to-many class="AC"/>
      </set>
  </class>

Then i populate the data of ab and ac successfully.
But when i try to call x.getAbs(), it result in a set which consist of both AB and AC
and throw an exception

Code:
org.hibernate.WrongClassException:
Object with id: 36 was not of the specified subclass: AB (loaded object was of wrong class)

It seems that hibernate query for all instance of class A (not spesific for instance of AB), and only filter the query based on the id of X.
What did i do wrong and what do i have to do to get the correct result?

Any help is appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 23, 2005 5:27 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
From the Hibernate online docs:

"Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself."

http://www.hibernate.org/hib_docs/v3/re ... tion-class

Try using polymorphism="explicit".

Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


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