-->
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: or-condition for two joins
PostPosted: Tue May 29, 2007 3:09 am 
Newbie

Joined: Tue May 29, 2007 2:50 am
Posts: 12
Hello

I work with criterias. How can I build a or-condition for two joins?

Example:
Code:
Criteria crit = session.createCriteria(City.class);
crit.createCriteria("avenues").add(Expression.eq("id", avenueId);
crit.createCriteria("building").add(Expression.eq("id", buildingId);
crit.list();



How can I create a or statement fort his two conditions?
I’d like to read all cities, which have a avenue, which accord to the avenueId or a building, which accord to the buildingId.

Thanks & sorry for my bad english!

Poffi


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 4:15 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Criteria crit = session.createCriteria(City.class,"c");
.createCriteria("avenues","a")
.createCriteria("building","b")
.add(Restrictions.or(
Restrictions.eq("c.id", "a.avenueId"),
Restrictions.eq("c.id", "b.buildingId")
).list();


Always try to use aliases.

Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 3:13 pm 
Newbie

Joined: Tue May 29, 2007 2:50 am
Posts: 12
thanks for the answer. I couldn't test it allready (tomorrow), but i think this i not that, what I like to have.

Don't I need to write crit before:
Code:
.createCriteria("avenues","a")

Code:
crit.createCriteria("avenues","a")



Is this...
Code:
.add(Restrictions.or(
Restrictions.eq("c.id", "a.avenueId"),
Restrictions.eq("c.id", "b.buildingId")

not a restriction for the city? I need a restriction for the set avenues and the set buildings. The query should read all cities which contains a building with the id buildingId or a avenue with the id anvenueId?


I think that could be the solution:
Code:
Criteria crit = session.createCriteria(City.class,"c");
.createCriteria("avenues","a")
.createCriteria("building","b")
.add(Restrictions.or(
Restrictions.eq("a.id", avenueId),
Restrictions.eq("b.id", buildingId)
).list();


thanks!
poffi


Top
 Profile  
 
 Post subject: Re: or-condition for two joins
PostPosted: Tue Jun 05, 2007 7:08 pm 
Newbie

Joined: Mon Jun 04, 2007 3:17 am
Posts: 4
If you don't care the performance, you can use 'outer join' by calling crit.createCriteria(blabla, Criteria.OUTER_JOIN);

Or you might use the SQL union, however, you will sacrifice code quality;

I got a very similar problem, and finally give up - Neither Bad performance nor bad code quality is accceptable for me .



poffi wrote:
Hello

I work with criterias. How can I build a or-condition for two joins?

Example:
Code:
Criteria crit = session.createCriteria(City.class);
crit.createCriteria("avenues").add(Expression.eq("id", avenueId);
crit.createCriteria("building").add(Expression.eq("id", buildingId);
crit.list();



How can I create a or statement fort his two conditions?
I’d like to read all cities, which have a avenue, which accord to the avenueId or a building, which accord to the buildingId.

Thanks & sorry for my bad english!

Poffi


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.