-->
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: WrongClassExceptions et al
PostPosted: Mon Nov 17, 2003 9:47 am 
Newbie

Joined: Mon Sep 15, 2003 4:37 am
Posts: 6
Location: Frankfurt Germany
We have a rather complex mapping-job, where the classes >person<, >department< and >firm<, are extending the class >contact<.
A person belongs to a firm or a department.
A department belongs to a firm or a parent-department.
and a firm stands for its own.

What i tried to do first is to do a mapping through joined subclasses which worked fine.
In our second approach we tried to use the (recommended) subclass-strategy which permanently threw
WrongClassExceptions when loading the object-tree of firms.
We used this mapping (well, a bit stripped down):

------------
<?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="Contact" table="T_CONTACT" >
<id column="UNIQUE_ID" name="uniqueId" type="long">
<generator class="sequence">
<param name="sequence">SEQ_UNID</param>
</generator>
</id>
<discriminator column="DISCRIMINATOR" type="string" />
<property name="name" column="NAME" type="string" />
<subclass name="Person" discriminator-value="Person" >
<property name="firstName" column="FIRSTNAME" type="string" />
<many-to-one name="parentFirm" column="FIRM_ID" />
<many-to-one name="parentDepartment" column="DEPARTMENT_ID" />
</subclass>
<subclass name="Firm" discriminator-value="Firm" >
<set name="employees" >
<key column="FIRM_ID"/>
<one-to-many class="Person"/>
</set>
<set name="subDepartments" >
<key column="FIRM_ID"/>
<one-to-many class="Department"/>
</set>
</subclass>
<subclass name="Department" discriminator-value="Department" >
<many-to-one name="parentDepartment" column="DEPARTMENT_ID" />
<many-to-one name="parentFirm" column="FIRM_ID" />
<set name="subDepartments" >
<key column="DEPARTMENT_ID"/>
<one-to-many class="Department"/>
</set>
<set name="employees" >
<key column="DEPARTMENT_ID"/>
<one-to-many class="Person"/>
</set>
</subclass>
</class>
</hibernate-mapping>
--------------
I finally found a solution in not reusing the foreign-keys between the used
classes and now use the following mapping without getting WrongClassExceptions.
---------------
<?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="Contact" table="T_CONTACT" >
<id column="UNIQUE_ID" name="uniqueId" type="long">
<generator class="sequence">
<param name="sequence">SEQ_UNID</param>
</generator>
</id>
<discriminator column="DISCRIMINATOR" type="string" />
<property name="name" column="NAME" type="string" />
<subclass name="Person" discriminator-value="Person" >
<property name="firstName" column="FIRSTNAME" type="string" />
<many-to-one name="parentFirm" column="PERSON_FIRM_ID" />
<many-to-one name="parentDepartment" column="PERSON_DEPARTMENT_ID" />
</subclass>
<subclass name="Firm" discriminator-value="Firm" >
<set name="employees" >
<key column="PERSON_FIRM_ID"/>
<one-to-many class="Person"/>
</set>
<set name="subDepartments" >
<key column="DEPARTMENT_FIRM_ID"/>
<one-to-many class="Department"/>
</set>
</subclass>
<subclass name="Department" discriminator-value="Department" >
<many-to-one name="parentDepartment" column="DEPARTMENT_DEPARTMENT_ID" />
<many-to-one name="parentFirm" column="DEPARTMENT_FIRM_ID" />
<set name="subDepartments" >
<key column="DEPARTMENT_DEPARTMENT_ID"/>
<one-to-many class="Department"/>
</set>
<set name="employees" >
<key column="PERSON_DEPARTMENT_ID"/>
<one-to-many class="Person"/>
</set>
</subclass>
</class>
</hibernate-mapping>
---------------------
Does this work as designed?
Why does the hibernate-documentation recommend, not to use joined-subclasses, what sounds more straightforward?
And what still bothers me is that hibernate permanently uses (self-)joins when trying to retrieve the object-tree.
This does not make sense as each retrieved object can be mapped to a single row of T_CONTACT and is a performance-killer.

Thanks in advance

Peter K


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 9:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Does this work as designed?


yes. the alternative option is to do something like this:


Code:
<set name="subDepartments"
   where = "DISCRIMINATOR='Department'>
   <key column="DEPARTMENT_DEPARTMENT_ID"/>
   <one-to-many class="Department"/>
</set>


Quote:
Why does the hibernate-documentation recommend, not to use joined-subclasses, what sounds more straightforward?


It performs better, and the generated SQL is more understandable.


Quote:
And what still bothers me is that hibernate permanently uses (self-)joins when trying to retrieve the object-tree.


Eh?

I don't know what you are saying .... but you do know how to disable outer join fetching, right?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 10:28 am 
Newbie

Joined: Mon Sep 15, 2003 4:37 am
Posts: 6
Location: Frankfurt Germany
Quote:
Eh?

I don't know what you are saying .... but you do know how to disable outer join fetching, right?


Now I know ;)

Thanks a lot

Peter K


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.