-->
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.  [ 9 posts ] 
Author Message
 Post subject: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Wed Aug 10, 2011 3:32 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I have a pagination method to return a page:

Code:
   @SuppressWarnings("unchecked")
   public Page<T> getPage(final int pageNumber, final int pageSize, final Criteria criteria) {
      int totalSize = 0;
      if (pageSize > 0) {
         // Save the original Projection and ResultTransformer if any
         CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
         Projection originalProjection = criteriaImpl.getProjection();
         ResultTransformer originalResultTransformer = criteriaImpl.getResultTransformer();
         criteria.setProjection(Projections.rowCount());
         // TODO I don't like this intValue on a Long
         // Why is uniqueResult returning a Long and setFirstResult requiring
         // an int ?
         totalSize = ((Long) criteria.uniqueResult()).intValue();
         int startIndex = Page.getStartIndex(pageNumber, pageSize, totalSize);
         criteria.setFirstResult(startIndex);
         criteria.setMaxResults(pageSize);
         criteria.setProjection(originalProjection);
         criteria.setResultTransformer(originalResultTransformer);
      }
      List<T> list = criteria.list();
      return new Page<T>(pageNumber, pageSize, totalSize, list);
   }

As you can see in the method body, there is a:

totalSize = ((Long) criteria.uniqueResult()).intValue();

casting a long to an int.

I had to do this because uniqueResult() returns a long, indeed the size can be really long.

But the method setFirstResult expects an int.

So I had to do the casting.

Why is setFirstResult expecting an int instead of a long ?

Thanks.

Stephane


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Wed Aug 10, 2011 4:06 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
We need to give the index always as int.

_________________
Jana


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Wed Aug 10, 2011 4:16 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I understand Hibernate requires that.

But I do not understand why.

What is the rationale ?

For example in a very big table, what if the index is too large for an int ?

And why is the count being returned as a long then ?


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Wed Aug 10, 2011 11:45 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
Yes. I too dont understand . even JPA has the same api

query.setFirstResult(int startindex).

May be a defect in hibernate.:(

_________________
Jana


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Thu Aug 11, 2011 2:40 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I would think Hibernate has a good reason for that int type, but it escapes me..


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Thu Aug 11, 2011 5:01 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know the reason but some (long) time ago count queries used to return the result as an int. This was changed to long due to some JPA requirements (I think). You can probably find an issue in the JIRA about this. Still leaves the question why JPA has Query.setFirstResult(int) and not Query.setFirstResult(long). But I don't have an answer for that.


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Thu Aug 11, 2011 7:58 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Strange that such a commonly used method would not raise questions over its use of the int type. Thanks for your comment anyway.


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Sat Oct 08, 2011 4:44 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Any new take on this ?


Top
 Profile  
 
 Post subject: Re: Why is setFirstResult expecting an int instead of a long ?
PostPosted: Wed Oct 19, 2011 10:28 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
No one ?


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