-->
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.  [ 7 posts ] 
Author Message
 Post subject: Polymorphic query problems
PostPosted: Mon Sep 24, 2007 10:08 am 
Newbie

Joined: Mon Sep 24, 2007 9:25 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

More detailed description at the end.

Hibernate version: 3.2 cr4, August 24, 2006

Mapping documents:
<!-- User.hbm.xml -->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="timereport.domain.users">
<class abstract="true" name="User" table="USERS" polymorphism="implicit">
<id name="id">
<generator class="sequence" />
</id>

<property name="name" not-null="true" />
<property name="password" not-null="true" />
<property name="contact" />
</class>
</hibernate-mapping>



<!-- Worker.hbm.xml -->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="timereport.domain.users">
<union-subclass name="Worker" extends="User">
<many-to-one name="employer" column="EMPLOYER_ID" update="false" not-null="true" lazy="false" />
</union-subclass>
</hibernate-mapping>



<!-- Employer.hbm.xml -->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="timereport.domain.users">
<union-subclass name="Employer" extends="User">
</union-subclass>
</hibernate-mapping>


Name and version of the database you are using: hsqldb 1.8.2

The generated SQL (show_sql=true):select user0_.id as id0_, user0_.name as name0_, user0_.password as password0_, user0_.contact as contact0_, user0_.EMPLOYER_ID as EMPLOYER1_2_, user0_.clazz_ as clazz_ from ( select id, null as EMPLOYER_ID, name, contact, password, 1 as clazz_ from Employer union select id, EMPLOYER_ID, name, contact, password, 2 as clazz_ from Worker ) user0_ where user0_.id=? and user0_.password=?


Hello, I have the following class hierarchy:
abstract class User
common properties, just primitives

class Worker extends User
Employer employer

class Employer extends User

The problem is that if i execute the HSQL query "from User where id = :id and password = :password", with the parameters matching a Worker it returns a Worker object, however, the employer association isn't populated.

This is the result i got after executing the union of the to sub-selects:
(that is basically (select * employer union select * worker), with some padding)
Sorry for the insane formatting on this column, don't know how to make it look right :-/

ID EMP_ID NAME CONTACT PASSWD CLAZZ
1 [null] TL Systems . . . . .. . .. . tl 1
14 [null] ICA [null] ica 1
17 [null] TL Systems [null] tl 1
22 [null] TL Systems [null] tl 1
29 [null] apa apa 1
47 17 Tommy Lindberg det är ju jag.. tl 2

The first 5 rows match objects of type Employer, and the last one is a Worker. So far so well, however, after the final select on this union, with ID = 47 and passwd = tl, i get this result:
47 [null] Tommy Lindberg det är ju jag.. tl 2

As a result, the Worker.employer = null, ofcourse.
How come EMP_ID (actually EMPLOYER_ID..) is [null] when it should be 17? I did turn of lazy loading on the employer property of Worker after all.

Feel free to ask any questions, if you don't understand my rambling :P

Kind regards,
Tommy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 7:53 am 
Newbie

Joined: Mon Jul 09, 2007 8:04 am
Posts: 12
Location: England
Hey Tommy,

I'm *very* new to Hibernate, so you'll have to excuse me if this sounds a silly suggestion...

I've only recently read the chapter in the JPWH book, and the mappings are different in there for this scenario.

In the book, their union subclass declarations are in the parent mapping.
So for your example, they would have User with the subclass union mappings, and them not in Employer/Worker.
Check out P196 if you have it.

Otherwise, if you query on the User table, how is Hibernate to know that it has subclasses without checking every single one (could be very slow)?

Just my two cents... I'm learning on this one too.

Hope that helps,

MG

_________________
MG


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 25, 2007 5:13 pm 
Newbie

Joined: Mon Sep 24, 2007 9:25 am
Posts: 2
Look at the generated SQL-query, it does look inside both the Employer table and Worker, and finds a result. But for some reason it sets the EMPLOYER_ID in the Worker row to null, even though it finds that it should be 17.. (sorry for the messed up table, but thats what it reads).

Regards,
Tommy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 4:55 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 4:26 am
Posts: 26
Location: Germany, Dortmund
Hi Tommy!

I have a very similar problem with <union-subclass>.

After some tests with HQL, criteria and load(), I have found out, that the query is fine (a union over all tables and all columns). The result list contains the correct classes (not always the super class). However the sub classes are only filled with the common attributes from the super class.

IMHO this is a bug. When I have time I will have a deeper look into Hibernate how it works. Perhaps I can provide a Jira bug with an patch later.

Bye
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 9:46 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 4:26 am
Posts: 26
Location: Germany, Dortmund
Hi again,

in my case it is a bug, but not a Hibernate bug.
The problem is HSQLDB which I am using.
A union like yours:
Code:
select user0_.id as id0_, user0_.name as name0_, user0_.password as password0_, user0_.contact as contact0_, user0_.EMPLOYER_ID as EMPLOYER1_2_, user0_.clazz_ as clazz_
from (
select id, null as EMPLOYER_ID, name, contact, password, 1 as clazz_ from Employer
union
select id, EMPLOYER_ID, name, contact, password, 2 as clazz_ from Worker ) user0_
where user0_.id=? and user0_.password=?


will always return null for
Code:
user0_.EMPLOYER_ID
for HSQLDB. For Oracle it works fine. If you swap the both select statements from the union it works also for HSQLDB. Unfortunately I have of a work around. I can live with it, because HSQLDB is only a test DB.

_________________
Bye
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 2:37 pm 
Newbie

Joined: Tue Sep 09, 2008 2:26 pm
Posts: 2
I've run into this same bug.

I logged a bug against hsqldb. I've been told on their forums that it's a know issue and the solution is to change all

null as <column_name>

to

cast(null as <data_type>) as <column_name>

Of course because I'm using hibernate I can't do that. I'm not sure if this should be fix in the hsqldb dialect or not. Might be worth logging a bug against the dialect and see who fixes the bug first. :)

There is a workaround I've found, but it's not ideal.

If you call 'em.refresh(obj)' after a find it will populate everything correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 4:15 pm 
Newbie

Joined: Tue Sep 09, 2008 2:26 pm
Posts: 2
I went through the hibernate bug list and I found this

http://opensource.atlassian.com/project ... e/HHH-2920

It appears to be the same problem.


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