-->
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: Unwanted Cross Join
PostPosted: Mon Feb 06, 2012 2:20 pm 
Newbie

Joined: Mon Feb 06, 2012 12:31 pm
Posts: 3
I recently upgraded to Hibernate 3.6.0.Final and (at least) one of my queries is behaving differently. For this HQL:

select ws
from c.s.i.domain.trade.WebSubmission ws
left outer join ws.job as job
left outer join job.opportunity as o
left outer join ws.assignedTo as assignedTo
order by ws.assignedTo.lastName DESC, ws.assignedTo.firstName DESC, ws.lastName DESC, ws.firstName DESC

Hibernate is generating this SQL:

[select omitted, it is correct and long]
from web_submissions websubmiss0_
left outer join Job job1_ on websubmiss0_.job_id=job1_.jobId
left outer join CompanyOpportunity companyopp2_ on job1_.OpportunityId=companyopp2_.opportunityId
left outer join users user3_ on websubmiss0_.assigned_to_id=user3_.userId
cross join users user4_
where websubmiss0_.assigned_to_id=user4_.userId order by user4_.lastName DESC, user4_.firstName DESC, websubmiss0_.last_name DESC, websubmiss0_.first_name DESC

The problem is the cross join user4_. The order by should be on user3_ created by the left outer join I provided.

The mapping on this column is a simple ManyToOne:


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "assigned_to_id")
public User getAssignedTo() {
return assignedTo;
}

public void setAssignedTo(User assignedTo) {
this.assignedTo = assignedTo;
}


Any suggestions would be greatly appreciated.

Tony Nelson
Starpoint Solutions

---- Edit ---- I found a solution, but I'd like to understand why.

By removing the ws. from assignedTo.firstName in the order by clause, Hibernate used the existing join.

select ws
from com.starpoint.instihire.domain.trade.WebSubmission ws
left outer join ws.job as job
left outer join job.opportunity as opportunity
left outer join ws.assignedTo as assignedTo
order by assignedTo.lastName DESC, assignedTo.firstName DESC, ws.lastName DESC, ws.firstName DESC

correctly created:

[select removed]
from web_submissions websubmiss0_
left outer join Job job1_ on websubmiss0_.job_id=job1_.jobId
left outer join CompanyOpportunity companyopp2_ on job1_.OpportunityId=companyopp2_.opportunityId
left outer join users user3_ on websubmiss0_.assigned_to_id=user3_.userId
order by user3_.lastName DESC, user3_.firstName DESC, websubmiss0_.last_name DESC, websubmiss0_.first_name DESC

This definitely is changed behavior between 3.6.0.Final and 3.3.2.GA. I'm hoping I don't have too many more instances of it in my code base.


Top
 Profile  
 
 Post subject: Re: Unwanted Cross Join
PostPosted: Mon Feb 06, 2012 6:06 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The difference is that explict joins (such as left outer join ws.assignedTo as assignedTo) and implicit joins (such as order by ws.assignedTo.lastName) are treated as two separate joins in Hibernate 3.6 but only as a single join in Hibernate 3.3. The old behavior was a bug and was fixed in Hibernate 3.5 (see https://hibernate.onjira.com/browse/HHH-4091)


Top
 Profile  
 
 Post subject: Re: Unwanted Cross Join
PostPosted: Mon Feb 06, 2012 6:42 pm 
Newbie

Joined: Mon Feb 06, 2012 12:31 pm
Posts: 3
Just wanted to thank you very much for your reply. I have a bunch of fixups to apply to my code now, but I can do so confidently knowing that I'm doing the right thing and for the right reason.


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.