-->
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: Want Hibernate to use outer join as default in HQL
PostPosted: Mon Feb 14, 2011 6:42 pm 
Newbie

Joined: Mon Feb 14, 2011 6:17 pm
Posts: 5
Hi,

I am new bie at hibernate. I need someone's help.
What I need hibernate to use left outer join in it's query as default.
Suppose, I have two table 1) Employee 2) Task with one to many relationship.
Now If I say findbyquery("select count(*) from Task t order by t.Employee.Name")
Hibernate generate following query with inner join:
select count(*) as col_0_0_ from Task Task0_, Employee Employee1_ where Task0_.Employee_ID=Employee1_.Employee_ID order by Employee1_.NAME
Now, I want hibernate to generate following query with left outer join to get all data with from Task despite null in EmployeeId.
select count(*) as col_0_0_ from Task Task0_
left outer join Employee Employee1_
on Task0_.Employee_ID=Employee1_.Employee_ID
order by Employee1_.NAME

I set fetch=join in hbm files of both Employee and Task but still it uses inner join only.
I am using hibernate3.0.

Please somebody suggest.

Thanks in Advance.


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Tue Feb 15, 2011 11:35 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Here is an important thing to know about join fetching:

fetch="join" uses outer join for nullable many-to-one foreign keys and collections
fetch="join" uses inner for not-null many-to-one foreign keys


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Tue Feb 15, 2011 1:26 pm 
Newbie

Joined: Mon Feb 14, 2011 6:17 pm
Posts: 5
Hi,

Thanks for response.

I did not define "not-null = true" at both relation that means those are nullable as default.

And still it returns result according to inner join way.

It does not return as same result as I get result by running same query at db side with outer join.

Please suggest how to tell hibernate to write outer join insted of inner join when I use any field from association in query.

Thanks


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Tue Feb 15, 2011 1:39 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

The first time I didn't realize you were using HQL Queries to retrieve data ...
HQL queries do not respect any fetch="join" defined in the mapping document!
The fetch="join" is only used when retrieving data through get() or load().
If you want an HQL Query to use left outer join, you have to write it explicitly in your query!


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Tue Feb 15, 2011 3:05 pm 
Newbie

Joined: Mon Feb 14, 2011 6:17 pm
Posts: 5
Ohh is it?

Then my task would become really tougher as my hibernate query is generated on fly as per the meta data we store
at database level. Our user store columns name in database to be displayed at UI for each table so if they define any column which is part of association type of column, suppose for Tast table user define Id, Name, Employee.Name then
my code generate HQL on fly to fetch only these columns but i get only inner join data.

I might have to change my implementaion if HQL does not care about fetch="join".

Thanks a lot for making me clear about this.


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Wed Feb 16, 2011 5:14 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You can take a look at this article:

http://community.jboss.org/wiki/AShortP ... Strategies

It was very instructive for me when I first read it!


Top
 Profile  
 
 Post subject: Re: Want Hibernate to use outer join as default in HQL
PostPosted: Wed Feb 16, 2011 5:10 pm 
Newbie

Joined: Mon Feb 14, 2011 6:17 pm
Posts: 5
Yes it is really instructive me too.

I think this might be the limitation that HQL does not respect fetch="join" and so I might have to change my implementation by using criteria query.

Thanks for all your help and quick responses.


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.