Interpretation of the principle of hibernate paging query

zhaozj2021-02-16  94

Hibernate can achieve paging queries, such as starting from 20,000 items, 100 records Query Q = Session.createQuery ("from cat as c"); Q.SetFirstResult (20000); Q.SetMaxResults (100); List L = Q.List (); How do Hibernate implements 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: IF (UseElimit) SQL = DIALECT.GETLIMITSTRING (SQL); PreparedStatement St = session.getbatcher (). PrepareQueryStatement (SQL, scrollable); If the corresponding database defines a qualified query record SQL statement, then use the SQL statement of a specific database directly. Then look net.sf.hibernate.dialect.MySQLDialect: 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 a dedicated page statement of MySQL, then looks back to net.sf.hibernate.diaalect.racle9DiaAlaforct: 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 the nest 3-layer query statement to integrate ROWNUM, which is the fastest way in Oracle, if only one layer or ROWNUM of the two query statements cannot support Order By. In addition, Interbase, PostgreSQL, HSQL also supports paging SQL statements, in the corresponding DIALECT, everyone's own reference.

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

New Post(0)