-->
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: Criterion instances involving subqueries!
PostPosted: Wed Nov 15, 2006 11:54 am 
Newbie

Joined: Wed Nov 15, 2006 10:20 am
Posts: 3
Hello!
I have looked every where for a way to solve my problemt, but the only examples are like

DetachedCriteria weights = DetachedCriteria.forClass(Cat.class)
.setProjection( Property.forName("weight") );
session.createCriteria(Cat.class)
.add( Subqueries.geAll("weight", weights) )
.list();

But what i want it's the equivalent of the following sql example:

SELECT *
..FROM Cat c LEFT OUTER JOIN
....( SELECT pedigreeId, catId
........FROM pedigree
........WHERE category = "big"
....) p on c.Id = p.catId
..ORDER BY c.Id ASC
;

This means that some cats have pedigree, and i want the data of ALL cats,
and the pedigree info of the big category, the rest should appear as null.
Does anyone have an idea on how to do it?

Thank you in advance!
FBrum :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 15, 2006 1:52 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Would something like
Code:
List<Cat> cats = session.createCriteria(Cat.class)
                                        .createAlias("pedigree", "p", CriteriaSpecification.LEFT_JOIN)
                                        .add(Restrictions.like("p.category", "big"))
                                        .list();

do?
The above is the equivalent of the SQL
Code:
SELECT *
FROM Cat c LEFT OUTER JOIN
   ( SELECT pedigreeId, catId
      FROM pedigree
) p on c.Id = p.catId
and  p.category = "big"

which is slightly different than yours but should work ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 6:43 am 
Newbie

Joined: Wed Nov 15, 2006 10:20 am
Posts: 3
Thanks for your reply!

The thing is that the SQL works, but the hibernate criteria doesn't!

The reason it's because, when adding the category restriction, this will restrict the cats and not only the pedigree class.

I believe that we should first do a subquery with its restriction, but the problem its how to connect a new criteria with a detached criteria.

I think that there should be a way to do a join between two criterias giving the path association, that in this case would be:
Code:
on c.Id = p.catId


Maybe this feature it's not yet implemented!

Does any one know something about this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 7:30 am 
Newbie

Joined: Wed Nov 15, 2006 10:20 am
Posts: 3
Hi have found a solutions to this problem.

Code:
List<Cat> cats = session.createCriteria(Cat.class)
                                        .createAlias("pedigree", "p", CriteriaSpecification.LEFT_JOIN)
                                        .add(Restrictions.or(
                                                   Restrictions.like("p.category", "big").
                                                   Restrictions.isNull(p.id))
                                        .list();


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.