-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to create a left-join-with with the Criteria api
PostPosted: Wed Jun 27, 2007 8:21 am 
Newbie

Joined: Wed Jun 27, 2007 6:37 am
Posts: 2
A UserAccout is in an ManyToOne relationship with Person

I have the following HQL

select u from UserAccount u
left join u.person p with p.name like 'P%'
where u.username like 'U%'

works exactly as I expect -generating sql with the restriction on person within the join clause and not on the where clause

I can create almost the same query using the Criteria API but the problem is that the restriction on the joined property is placed in the where clause instead of being a part of the left join

After extensive searching I get the impression that the criteria API doesn't support adding restrictions to a join. I've also taken a look at using filters to accomplish this but filters seem to be static (defined in the mapping or aspects) and I need dynamic filters.

Is there any way of doing the above using the criteria API?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 12:09 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
You can create an alias on the association specifying a left outer join.

criteria.createAlias("person", "personJoin", Criteria.LEFT_JOIN);
criteria.add(Restrictions.like("personJoin.name", "P", MatchMode.START);


Top
 Profile  
 
 Post subject: How to create a left-join-with with the Criteria api
PostPosted: Thu Jun 28, 2007 3:07 am 
Newbie

Joined: Wed Jun 27, 2007 6:37 am
Posts: 2
This is exactly what I'm using but the restriction (person like 'P%' ) is placed on the where clause of the generated SQL and not on the left join clause. (Subtle difference but very important!)

Take the HQL query
from UserAccount u
left outer join u.person p with p.name like 'p%'
where u.username like 'U%'

Transleates correctly into SQL as

select .... from UserAccout u
left outer join u.person p on u.id = p.id and p.name like 'P%'
where
u.username like 'U%'

Using the Criteria API (as you suggested using createCriteria()) the above query translates into SQL as
select .... from UserAccout u
left outer join u.person p on u.id = p.id
where
u.username like 'U%' and p.name like 'P%'

as you can see the predicate p.name like 'P%' is in the wrong place!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:02 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I guess I don't quite understand why you'd want to do that then.

If you put the condition on the outer join it's no different than not having it in the first place, unless i am missing something.

From as far as I can tell you're still going to get the UserAccounts whether or not they have a person record or the name is bob, phil or gladys. What are you actually trying to accomplish with putting the condition on the join?

I could see it if you were trying to select the data from the 2 tables but the criteria API is going to return your root UserAccounts and will load any associated person whether or not their name begins with P.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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