-->
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: Query parsing using INNER or LEFT JOINS
PostPosted: Tue Jan 22, 2008 9:17 am 
Newbie

Joined: Tue Jan 23, 2007 4:25 pm
Posts: 3
Hello all,

I'm using NHibernate, but my problem also occur in Hibernate, I think.

I've an object model like this (omitting getters and setters):

class ChangeRequest {

private long id; //PK
private string title;
private User responsible; //FK to User table
}

class User {

private long id; //PK
private string name;

}

The relational model is straightforward. Now, I'd like to write an HQL query to get only the ChangeRequest's title and it's responsible name.

The HQL would be like this:

session.CreateQuery("SELECT cr.title, cr.responsible.name FROM ChangeRequest as cr");

The data I have in my database is:

ChangeRequest table
-------------------

id title Responsible_ID
1 Error in Main Form 2
2 Exception raised in Login Form NULL
3 Concurrency issue in admin module NULL

User table
----------

id name
1 Sean
2 Julio

My problem is the following: I'd like to get the following result from this query:

cr.title cr.responsible.name
Error in Main Form Julio
Exception raised in Login Form NULL
Concurrency issue in admin module NULL

This way, I could show in the grid a report with the 3 CRs, and the respective responsible (even if null).

But I noticed that Hibernate is always parsing the query using INNER JOINS, so I only receive the first line of the result set.

My question: is there ANY way to instruct Hibernate to use LEFT JOINS, or I'll have to change my queries in the WHOLE application...?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 1:58 pm 
Newbie

Joined: Tue Jan 23, 2007 4:25 pm
Posts: 3
Anyone? :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 3:50 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
dpmelo,

By default, your one-tom-many associations are not joined at all, because their "lazy" parameter is set to "proxy", which is a stub of your object Hibernate creates in order to intelligently decide when to load your children's sets.

Normally you do not specify "left join" in an HQL query. Instead, a join is supposed to be lazy and you, occasionally and for performance reasons, might use "join fetch" (instead of the "join" keyword alone) in order to indicate that you want a full join there.

_________________
Gonzalo Díaz


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.