-->
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: Unexpected right join behaviour
PostPosted: Fri Sep 28, 2007 5:30 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2

Mapping documents:
Code:
<hibernate-mapping>
   <class name="Role" table="role" node="Role">
   
      <id name="id" type="long" column="id" unsaved-value="0" node="@id">
         <generator class="sequence">
            <param name="sequence">role_id_seq</param>
         </generator>
      </id>
      
      <version node="@version" name="version" type="long" access="field" unsaved-value="undefined" />
      <property node="@changePerson" name="changePerson" type="string" column="changePerson" not-null="true" />
      <property node="@changeDate" name="changeDate" type="calendar" column="changeDate" not-null="true" />
      <property node="@creationDate" name="creationDate" type="calendar" column="creationDate" not-null="false" />
         ....
      
      <joined-subclass name="Customer" lazy="false" node="Customer">
         <key column="pk_personrole" />
         ...
      </joined-subclass>
      
      <joined-subclass name="Driver" node="Driver">
         
         <key column="pk_personrole" />
         
         <!-- one Driver belongs to one DriverClass -->
         <many-to-one name="customer" node="Customer/@id" class="Customer" column="ref_customer" />
      </joined-subclass>
   ...
   </class>
</hibernate-mapping>


Name and version of the database you are using: Postgres 8.2

The generated SQL (show_sql=true):



Hi,

we have Problems using (right) joins with HQL.

Let's consider two classes, "Customer" and "Driver", they are mapped as above, the driver has a reference to the customer. Now we want to write a query that list all customers and, if there are any, the drivers that are referenced.

Thought it's a right join, so we queried like this:

Quote:
select _customer0.partner, _driver0.partner from de.innuce.masterdata.person.Driver _driver0 right join _driver0.customer _customer0


In our example db we have three customers, just one has a driver. So, we expected three rows returned.
Just got one, the one customer which is referenced by the only driver.

Are we doing sth. wrong, or is HQL right join behaving different than ANSI?

Thanks a lot.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 5:41 am 
Newbie

Joined: Thu Apr 26, 2007 9:42 am
Posts: 14
Try a right outer join instead.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 5:45 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Yes, also thought about and tried. Same behaviour.
I understood the documentation, that left and left outer are same, and right and right outer also.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 5:48 am 
Newbie

Joined: Thu Apr 26, 2007 9:42 am
Posts: 14
Try to do a left outer join with customer first and then driver.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 6:22 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Hi again, thanks for quick replies.

If I try this:


Quote:
select _customer0.partner, _driver0.partner from de.innuce.masterdata.person.Customer _customer0 left outer join de.innuce.masterdata.person.Driver _driver0


result into an exception:

Code:
java.lang.NullPointerException
   at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:317)


any ideas?

seems that there is nothing specified to join on.

If I change the query to:
Quote:
result size increases up to 12...
select _customer0.partner, _driver0.partner from de.innuce.masterdata.person.Driver _driver0, de.innuce.masterdata.person.Customer _customer0 left outer join _driver0.customer
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 7:13 am 
Newbie

Joined: Thu Apr 26, 2007 9:42 am
Posts: 14
Maybe to define a one-to-one relation in Customer to the Driver and write the query with left outer join from Customer to Driver.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 8:56 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Rewriting mappings is no option :/

It's part of a general reporting engine ... any queries are allowed.


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.