Troubleshoot hibernate paging problem get a small bug with the number of rows in a table

Source: Internet
Author: User
Tags rowcount sessions
I came back this morning to design a little news publishing system around the function, and the realization of the news indexing function.
But at the same time in the list of related news when the trouble. Is originally run a good paging query code, incredibly a bug ran out,
It's very unpleasant. Also ashamed to say, hibernate also spent so long time. The underlying API can be very non-attribute.
Looked up the network, to their own smattering of words out.

The origin of the bug.
Look at the code:
Return (Pagesupport) gethibernatetemplate (). Execute (
New Hibernatecallback () ... {
Public Object Doinhibernate (sessions session)
Throws Hibernateexception ... {
Criteria criteria = Detachedcriteria
. Getexecutablecriteria (session);


Logger.debug ("SQL:" + projections.rowcount ());



Execute Query
int totalcount = ((Integer) criteria.setprojection (Projections.rowcount ()). Uniqueresult ()). Intvalue ();





List items = Criteria.setfirstresult (StartIndex)
. Setmaxresults (pageSize). List ();

Pagesupport PS = new Pagesupport (items, TotalCount,
PageSize, StartIndex);

return PS;
}
}, True);
I believe you are familiar with the above code, the first time the code is not a problem to run.
When you go to get the 2nd page, the prompt says appears nullpointer. The discovery is ((Integer) criteria.setprojection (Projections.rowcount ()). Uniqueresult ()) is null, which means that the total number of tables is not taken at all.
So I thought for a while.
Then read the code of others before beginnings Dawu.
Look at the 2nd code:
Return (Pagesupport) gethibernatetemplate (). Execute (
New Hibernatecallback () ... {
Public Object Doinhibernate (sessions session)
Throws Hibernateexception ... {
Criteria criteria = Detachedcriteria
. Getexecutablecriteria (session);

Criteriaimpl Impl = (Criteriaimpl) criteria;

Take the projection and order by first, and clear both to perform the count operation.
Projection projection = Impl.getprojection ();

Logger.debug ("SQL:" + projections.rowcount ());



Execute Query
int totalcount = ((Integer) criteria.setprojection (Projections.rowcount ()). Uniqueresult ()). Intvalue ();

Reset the projection and the conditions of the previous
Criteria.setprojection (projection);
if (projection = null) ... {
Criteria.setresulttransformer (criteriaspecification.root_entity);
}



List items = Criteria.setfirstresult (StartIndex)
. Setmaxresults (pageSize). List ();

Pagesupport PS = new Pagesupport (items, TotalCount,
PageSize, StartIndex);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.