-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using projection API to get a count from a criteria
PostPosted: Mon Sep 12, 2005 5:32 am 
Newbie

Joined: Wed Jun 01, 2005 6:18 am
Posts: 9
Hibernate version: 3.0.5

I've written a generic method to perform a count on a Criteria object using the Projections API.

However, although it does return the correct count, when using the criteria again to perform the actual query to get the data, it causes the wrong entity to be returned.

Please can any one advise on how to perform a count on a criteria using projections, without adversely affecting the criteria's ability to perform the normal non projection query.

Here's the method as it stands:

public final int count(Criteria criteria) {
int count = 0;

if(criteria!=null) {
count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
}

// remove the projection so the criteria can be used normally again
criteria.setProjection(null);

return count;
}


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 4:16 pm 
Newbie

Joined: Wed Jun 22, 2005 6:38 pm
Posts: 7
Do you need to set the projection to null? Since setProjection() (and other Criteria operations) return a Criteria object, I assumed they weren't modifying the original Criteria object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 4:44 pm 
Newbie

Joined: Wed Jun 22, 2005 6:38 pm
Posts: 7
I looked in CriteriaImpl.java and I was way off. I did notice that setResultTransformer(PROJECTION); is called an never reverted. You might try:

Code:
ResultTransformer rt = ((CriteriaImpl)crit).getResultTransformer();
//do your stuff
crit.setProjection(null);
crit.setResultTransformer(rt);
[/code]


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