-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL Subquery within a Criteria query?
PostPosted: Mon Aug 14, 2006 4:43 pm 
Newbie

Joined: Sun May 22, 2005 4:21 pm
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Is there a way to embed an HQL subquery within a Criteria query? The subquery needs to be in HQL because of the way my table associations are set up, but I want to be able to take advantage of the criteria api for the outer query. It seems like the Subqueries class only allows for subqueries in the criteria format.

One workaround would be something like this:
Code:
String queryString = "SELECT doc.documentId FROM " +
"Document doc, " +
"Archive archive, " +
"Audit audit " +
"where doc.archiveId = archive.archiveId " +
"and archive.archiveId = audit.archive.archiveId " +
"and audit.action.code = :actionCode " +

Query query = getSession().createQuery(queryString);
query.setString("actionCode", Constants.ACTION_SUBMIT);

subQueryList = query.list();

criteria.add(Restrictions.in("documentId", subQueryList));


However, this results in two calls to the database (One when query.list() is called and one when criteria.list() is called. Ideally I would like to execute it as a single query. I looked at the org.hibernate.criterion.Subqueries
class but it seems to only support subqueries that are also built using the criteria api.

Is there any other way to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 5:04 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
If you can organize your code by taking below code snippet as example, I think you can get the solution.

Code:
DetachedCriteria d = DetachedCriteria.forClass( Audit.class, "audt" );
d.add( Restrictions.eq( "audt.actionCode", "actionCode" ) );
d.createAlias( "audt.archive", "arch" );
d.setProjection( arch.documentId );

Criteria c = session.createCriteria( Document.class, "docu" );
c.add( Property.forName( "docu.documentId" ).in( d ) );


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