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: question about DetachedCriteria
PostPosted: Wed Jul 12, 2006 2:23 am 
Newbie

Joined: Wed Jul 12, 2006 1:59 am
Posts: 1
In My Code, I created DetachedCriteria instance once and want to use it twice:

create:
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.add....
dc.addOrder.....
use:
findByDetachedCriteria(dc);
countByDetachedCriteria(dc);

the findByDetachedCriteria() method use getExecutableCriteria(session) to obtain a Criteria instance and then set some limitations on the Criteria instance. findByDetachedCriteria() was executed successfully but the countByDetachedCriteria() failed because countByDetachedCriteria() method also use getExecutableCriteria(session) method and In Hibernate, a DetachedCriteria instance always return the same Criteria instance it created when calling getExecutableCriteria(session) method.

So if you create a DetachedCriteria instance and use it in different methods, you will get confused because when you calling getExecutableCriteria(session) of the DetachedCriteria instance and manipulate the returnning Criteria instance, the next time you call getExecutableCriteria(session) method it will return the changed Criteria instance.

My question is, why Hibernate doesn't return a new Criteria instance each time we calling the getExecutableCriteria(session) method, what's the design consideration on return the same Criteria instance?

Thanks!


Top
 Profile  
 
 Post subject: Re: question about DetachedCriteria
PostPosted: Wed Jul 12, 2006 2:32 pm 
Beginner
Beginner

Joined: Tue Jun 07, 2005 8:24 am
Posts: 33
Location: Cincinnati, OH
I have the same issue. I created a tapestry table model which uses hibernate. To find out how many rows the query would return, I use a rowCount projection. But, to actually return the rows for the current page, I use a regular criteria query. I'd like to reuse the same DetachedCriteria object for each query, but as was pointed out, the Criteria object that's returned the second time (after I've done the rowCount query) has its projection altered. Why not create a brand new criteria each time and not alter the DetachedCriteria?


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.