-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Pagination and Criteria
PostPosted: Mon Feb 21, 2005 8:08 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Hibernate version: 3.0beta4

Hi,

i'm using Criteria to easily search some data. I'm setting firstResult and maxResults to paginate the data, but i'd like too have the 'count' of my request without this pagination.

For now i'm using this way :
Code:
ScrollableResults scrollableResults = criteria.scroll(ScrollMode.FORWARD_ONLY);
request.setAttribute("objects", criteria.setFirstResult(offset).setMaxResults(limit).list());
scrollableResults.last();
request.setAttribute("numberOfObjects", scrollableResults.getRowNumber());


Is there a better way ?

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate3


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 8:42 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Yes, that what i'm using.... but apparently i missed some documentation :-/

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 9:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Have a look at http://javablogs.com/Jump.action?id=198498 i posted some examples for the new criteria api there. Also look at the source in org.hibernate.test.criteria


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 6:01 am 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
michael wrote:
Have a look at http://javablogs.com/Jump.action?id=198498 i posted some examples for the new criteria api there. Also look at the source in org.hibernate.test.criteria


Thanks a lot, i missed Projections... great thing :)

An other question.. is there an easy way to duplicate a criteria (clone doesn't seems implemented according to javadoc) ?

I'd like build a criteria with restriction, then duplicate to have two criteria objects: one to get result list (with pagination) and one to project the rowCount.

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 1:19 pm 
Newbie

Joined: Tue Mar 01, 2005 10:37 am
Posts: 17
Anybody have any ideas? I'm trying to do the same as magic. I have a detached criteria that defines the objects I want returned. Then I want to run several queries based on that defined criteria. One such query would be a count using projection. Another would be doing pagination. Unfortunately, when I set the projection to do the count (even though it seems done on seperate executable criteria), now the projection always applies to anything based on my original criteria. Is there any way around this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 1:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So, reset to a different Projection ... ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 2:10 pm 
Newbie

Joined: Tue Mar 01, 2005 10:37 am
Posts: 17
I can't figure out what other Projection I can set it to. Setting it to null or an empty projectionList() prevents it from knowing what class the criteria was supposed to be selecting (I get back Objects instead of the class I was expecting and hence get ClassCastExceptions).

Is there some sort of empty projection I'm missing that would be the equivalent of removing the count projection?

Thanks for you help.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 9:32 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
I have posted some sample code to assist with solving this problem

http://forum.hibernate.org/viewtopic.ph ... 54#2234254


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 9:36 am 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
jrduncans wrote:
I can't figure out what other Projection I can set it to. Setting it to null or an empty projectionList() prevents it from knowing what class the criteria was supposed to be selecting (I get back Objects instead of the class I was expecting and hence get ClassCastExceptions).

Is there some sort of empty projection I'm missing that would be the equivalent of removing the count projection?

Thanks for you help.


Yes - the default projection is the class that the criteria is created from in the first place.

I found this out the hard way.

i.e.

Criteria c = session.createCritera(Order.class);

Criteria c = c.createCriteria("customer");

Integer count = (Integer)c.setProjection(Projections.rowCount()).uniqueResult()

c.setProjection(null)

will return a List of Object[] {Order, Customer}

I guess you can set it back with

c.setProjection(Projections.someMethod(Order.class));

Cameron.


Top
 Profile  
 
 Post subject: Serialize it?
PostPosted: Mon Mar 14, 2005 10:20 pm 
Newbie

Joined: Wed Feb 23, 2005 11:57 am
Posts: 15
I posted some code at http://forum.hibernate.org/viewtopic.php?t=939781 which clones a detached criteria using serialization. I did this so I could copy the criteria and modify the copy for rowCount, setFirstResult, etc. at different times for pagination. I would be curious to know if this solution is crazier than any others.

:)

-gabe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 27, 2005 3:57 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Guys

You dont need to do the "double criteria" hacking.

After you get the row count, just do this (on the same Criteria):

Code:
crit.setResultTransformer( Criteria.ROOT_ENTITY );
crit.setProjection( null );


.. and hey presto you can re-use the same criteria object.

HTH,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 9:04 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
beniji wrote:
Guys

You dont need to do the "double criteria" hacking.

After you get the row count, just do this (on the same Criteria):

Code:
crit.setResultTransformer( Criteria.ROOT_ENTITY );
crit.setProjection( null );


.. and hey presto you can re-use the same criteria object.

HTH,
Ben


Actually, it's the other way arround:
Code:
crit.setProjection( null );
crit.setResultTransformer( Criteria.ROOT_ENTITY );


setProjection sets the ResultTransformer to PROJECTION.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 5:46 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Uhmm, it seems a good idea to automate this: what about changing the setProjection to reset the result transformer back to default if invoked with null attribute ?

Does it make sense to use a PassThroughResultTransfomer with no projection ?

_________________
--
Marco


Top
 Profile  
 
 Post subject: Be nice if they embraced the criteria.count() method
PostPosted: Fri Jun 17, 2005 12:31 pm 
Newbie

Joined: Fri Jun 17, 2005 12:12 pm
Posts: 1
I was using a 2.x patched version of hibernate because I needed a count method.
I was adding a bunch of Criterion to a criteria, getting the list and getting the count. I thought it was going to be incorporated into version 3 (bug was closed on beta 3) but, I was wrong.
It used to be so simple:
List results = criteria.list();
long total = criteria.list();

Looking at forums related to this feature, it looks like the patch maintainer got fustrated and decided to just use his own custom code.

Now I'm trying to upgrade to Hibernate 3. And the code is looking worse. And type safety is reduced.
So now from what I get from the new docs:
long total = criteria.list();
becomes:
Integer count = (Integer)c.setProjection(Projections.rowCount()).uniqueResult();
int total = count.intValue();
Besides more code to do the same thing, we have to cast it to an Integer (lack of type safety) and the criteria object gets modified in the process. And from the debate in previous posts, its unclear how to restore the criteria to its unmodified previous state.

I understand the concern over interface bloat and that this was never an *official* feature but, it looks like it is a very useful and needed feature. This moving over to *Projections* looks like a lot of neat esoteric stuff that I'll never actually use. Most discussion around it seems to indicate that its being added in response to other vendors rather than the needs of the users.

I apologize for ranting and gettng a bit off topic. I just can't understand being offered a clean method and dropping it for a convoluted one. If we are going to cast all of our objects and have no type safety then we should just start using Perl.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.