Hibernate Implementation of Paging Query ZZ

xiaoxiao2021-04-09  460

Hibernate can achieve paging queries, such as starting from 100,000 records from 2000

Code: query q = session.createQuery ("from cat as c"); Q.SetFirstResult (20000); Q.SetMaxResults (100); list l = q.list ();

So how do hibernate achieves paging? . . . . . . . . . . . . . . . . . . . . . . . . .

In fact, Hibernate's query is defined in Net.sf.hibernate.Loader.Loader, read the code carefully, you can completely figure out the problem.

Hibernate2.0.3 Loader Source Code Chapter 480 below:

Code: if (userimit) SQL = DIALECT.GETLIMITSTRING (SQL); PreparedStatement St = session.getbatcher (). PrepareQueryStatement (SQL, Scrollable);

If the corresponding database defines the SQL statement that qualifies the query record, then the SQL statement of a specific database is directly used.

Then look at Net.sf.Hibernate.Dialect.MysqldiaLect:

Code: public boolean supportsLimit () {return true;} public String getLimitString (String sql) {StringBuffer pagingSelect = new StringBuffer (100); pagingSelect.append (sql); pagingSelect.append ( "limit,??"); Return pagingSelect .tostring ();

This is my dedicated page statement of MySQL, and then look at Net.sf.Hibernate.Dialect.Orcle9DiaLect:

Code: Public Boolean Supportslimit () {Return True;}

public String getLimitString (String sql) {StringBuffer pagingSelect = new StringBuffer (100); pagingSelect.append ( "select * from (select row _ *, rownum rownum_ from (."); pagingSelect.append (sql); pagingSelect.append ( " ) Row_ where rownum <=?) Where rownum_>? "); return pagingselect.toString ();}

Oracle uses a nesting 3-layer query statement to implement paging, which is the fastest way in Oracle, if only a layer or two layers of query statements cannot support Order By.

In addition, Interbase, PostgreSQL, HSQL also supports paging SQL statements, in the corresponding DIALECT, everyone's own reference.

If the database does not support the SQL statement of the pagination, then according to the default in the configuration file # hibernate.jdbc.use_scrollable_resultset True is true, if you do not specify as false, then Hibernate will use JDBC2.0's scrollable result to implement paging, see Loader 430 lines below:

Code: if (. Session.getFactory () useScrollableResultSets ()) {// we can go straight to the first required row rs.absolute (firstRow);} else {// we need to step through the rows one row at a time (SLOW) for (int m = 0; M} If you support scrollable result, use the ResultSet's Absolute method to move directly to the starting point, if you do not support, use the loop statement, RS.Next's point to move.

It can be seen that use hibernate, which is very flexible in the operation of the query paging, Hibernate will first try to use a specific database SQL, if not, try scrollable, if not, finally use Rset.Next () Move Method.

A great advantage that uses Hibernate in the query pagination code is that both the performance of query pagination, and guarantees the portability of the code between different databases.

Posted by Rhythm At March 28, 2004

转载请注明原文地址:https://www.9cbs.com/read-133014.html

New Post(0)