-->
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.  [ 1 post ] 
Author Message
 Post subject: Join on Subquery using Projections.
PostPosted: Tue Jun 26, 2012 9:40 am 
Newbie

Joined: Fri May 18, 2007 9:57 am
Posts: 3
How can I generate the following SQL using Critieria?
The key issue is that I need to join on the result of the subquery on submissionid but also need to include the dcn column so that it can be used to sort the result. A submission has many submissionOccurrences and we need to sort on the dcn of the very lastest occurence.

SELECT * from submission sub
JOIN (
select max(so1.occurrence_start_tmstmp), so1.dcn, so1.submission_id
from submission_occurrence as so1 group by so1.dcn, so1.submission_id
) so
ON sub.submission_id = so.submission_id
where sub.submission_id between 32000 and 33000 order by so.dcn asc;

I've gotten close using the following but it never generates a join on the subcriteria like I would expect. It simply generates an equals on submissionId rather than joining and I get an error because the projection includes multiple columns.

DetachedCriteria subCriteria = DetachedCriteria.forClass(SubmissionOccurrence.class, "latestOccurrence");

ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.max("latestOccurrence.occurrenceStartTimestamp"));
projectionList.add(Projections.groupProperty("latestOccurrence.submissionId"));
projectionList.add(Projections.groupProperty("latestOccurrence.dcn"));

subCriteria.setProjection(projectionList);

criteria.add(Subqueries.propertyEq("submissionId", subCriteria));



Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.