SSH first Experience series--hibernate--3--single value and paging query

Source: Internet
Author: User

Objective

The query can be divided into 2 categories by result set: single result and array; Where, return array, in this database data volume casually can on hundreds of thousands of of the internet age background, often need to do pagination processing, so here to say the single value and paging, is to the previous "curd" some extension

I. Single-Value queries

For example, we need to query the database, statistics of the current number of registered apps, at this time we know that the return result must be a "single row" of a value, then you can use this single-valued query method;

Using the Uniqueresult () method, it returns a Java.lang.Object object and guarantees that the return must be a single-valued object, otherwise an error will be given, and if 0 objects are returned, the error will also occur; so we need to put it in the Try-catch block.

Session session=NULL; Try{Session=hibernateutil.opensession (); String hql= "SELECT count (*) from Feedback"; LongCount= (Long) Session.createquery (HQL). Uniqueresult (); System.out.println ("Get Data OK"); }Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); }finally{session.close (); }
Two. Paging Query

In this internet age, hundreds of thousands of of the data is also very common things, every time the query load is very unrealistic. Therefore, do paging query, not only save the DB performance, bandwidth, smoothness, but also to improve the user experience.

Hibernate's query class has two ways to help us achieve paged query purposes: setfirstresult (int num) and setmaxresults (int size); The former NUM is the starting record number of the query, such as starting from the 3rd query, that is Setfirstresult (3); The latter is the size of the query result set, such as fetching 10 records, that is Setmaxresults (10);

            // Paging Query            String hql= "from Feedback ORDER by id desc";            List<Feedback> feedbacks=session.createquery (HQL). Setfirstresult (1). Setmaxresults (2). List ();

Where: num of setfirstresult (int num) is still starting from 0;

So, with the Pageindex,pagecount, it is easy to get the paging request code, on the basis of the above code to modify the line:

            // Paging Query            String hql= "from Feedback ORDER by id desc";                         int pageindex=2,pagecount=10;                        List<Feedback> feedbacks=session.createquery (HQL). Setfirstresult ((pageIndex-1) *pagecount). Setmaxresults (PageCount). List ();

  

This article is copyright to the author and the blog Park, Welcome to reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility;

This article is from: Blog Park-Don't ask who

SSH first Experience series--hibernate--3--single value and paging query

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.