Query.setfirstresult in Hibernate (), Query.setmaxresults ();

Source: Internet
Author: User

One, Query.scroll () and Query.setfirstresult (), Query.setmaxresults (); Both of these methods can be taken to a certain range of data, used to display data pagination. So what is the difference between the two, and how efficient are they?
A: 1.scroll is implemented with JDBC2.0 's scrollable result set; Query.setmaxresults (); Query.setfirstresult () is a database SQL statement implementation.

2. You say it's good to split the pages in the database. It's better to take the result set to the memory and then page it out. (It should be a bit better in the database, but if in memory paging, it is faster to change pages.) )

3. Paging in the database is the preferred way. Database paging is actually paging through the functionality of the database's own SQL extensions, such as SQL statements such as the MySQL limit 0,50. Not only is it fast, but it also saves memory. However, not every database has this kind of paging-supported SQL, for example SQL Server is not supported. Oracle Paging method refer to Oracle Implementation paging

4.scroll is the use of JDBC2.0 functions to do paging, then it is entirely dependent on the implementation of a specific database JDBC driver. In fact, most JDBC driver take all the result sets once to the memory and then the paging. If the result set is very large, for example tens of thousands of, the program execution speed is slow, and it is easy to cause out of memory. Of course, the individual JDBC driver are implemented using server-side cursors, which does not cause such problems, such as Jtds.

Hibernate can use the Query.setmaxresults method to simply set the maximum result set that needs to be queried.
Hibernate then automatically translates the corresponding SQL statement into the database according to the database dialect that you set. For example, if the database is Oracle,sql server and so on, it translates to SQL statements such as the top 10 such as SELECT ... and if MySQL, it translates to SQL like select ... limit 10.


Third, examples:

Query.setfirstresult (0), Query.setmaxresults (4), equals limit 0, 4 in MySQL;
public void Testquery () {
  session session = NULL;
  try {
    session = Hibernateutils.getsession ();
    Session.begintransaction ();
    Query query = Session.createquery ("from User");
    Query.setfirstresult (0);//start with the first record
    query.setmaxresults (4);//Remove four records
    List userlist = Query.list ();
    For (Iterator iter=userlist.iterator (); Iter.hasnext ();) {
      User user = (user) iter.next ();
      System.out.println (User.getid ());
      System.out.println (User.getname ());
    }
    Session.gettransaction (). commit ();
  } catch (Exception e) {
    e.printstacktrace ();
    Session.gettransaction (). rollback ();
  } finally {
    hibernateutils.closesession (session);
  }
}

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.