-->
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.  [ 8 posts ] 
Author Message
 Post subject: criteria with subselect
PostPosted: Wed Mar 01, 2006 3:25 pm 
Newbie

Joined: Mon Feb 20, 2006 1:21 pm
Posts: 12
i'm not sure exactly how to do this but i'm sure it must be simple. here is the sql:

select * from table1
where id =
(select min(id) from table1 where > 'some_number')

more curious how to implement subselect

thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 3:37 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
From the docs:
Quote:
For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded
by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that
refer to an alias in the outer query) are allowed.

See section 14.12 of reference.pdf
http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 3:47 pm 
Newbie

Joined: Mon Feb 20, 2006 1:21 pm
Posts: 12
thanks but I meant with criteria not hql


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:01 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
session.createCriteria(Student.class)
.add( Subqueries.propertyEqAll("name", dc) )
.list();

check out <hibernate-3.1>\test\org\hibernate\test\criteria\CriteriaQueryTest.java

for more details.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 5:34 pm 
Newbie

Joined: Mon Feb 20, 2006 1:21 pm
Posts: 12
getting closer i think but:

Criteria criteria = session.createCriteria(ClassName.class);

DetachedCriteria dc = DetachedCriteria.forClass(ClassName.class)
.add(Restrictions.gt("id", new Integer(id)))
.setProjection(Projections.min("id"));

criteria.add(Subqueries.eq("id",dc));

still not working. sql comes out to :
select this_.TITLE_ID as TITLE1_0_0_, this_.TITLE as TITLE0_0_, this_.DATE_STRING as DATE3_0_0_
, this_.LIST_DATE as LIST4_0_0_
from top_ten_title this_ where ? =
(select min(this0__.TITLE_ID) as y0_ from top_ten_title this0__ where this0__.TITLE_ID>?)

i'm curious about the line with 'where ? = ...."

why is there a question mark there


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 5:54 pm 
Newbie

Joined: Mon Feb 20, 2006 1:21 pm
Posts: 12
i got it working by using
criteria.add(Subqueries.propertyEq("id",dc));
instead of
criteria.add(Subqueries.eq("id",dc));

these methods had no comments or documentation so i was clueless what the difference was. (just a remark, i never complain about a free product:)

can i give credits to myself:)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 6:01 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
can't give credits to yourself... but reminder that I did help by getting you pointed in the right direction... ;)

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 6:05 pm 
Newbie

Joined: Mon Feb 20, 2006 1:21 pm
Posts: 12
you got it jt. thanks


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