Hibernate is designed in different databases to achieve transparency, generalized paging implementation mechanisms. The Hibernate height portability is achieved by different Diagect (Database dialect).
You can set the page range via criteria.setfirstresult and criteria.setfetchsize method
EG.
Criteria criteria = session.createcriteria (tuser.class); criteria.add (Expression.eq ("AGE", "20")); // Get 20 records of the 20 records starting from the search result from the search result Criteria.SetFirstresult ( 100); criteria.setfetchsize (20);
In Hibernate, abstract class net.sf.hibernate.DiaLect Specifies the external unified interface of all underlying databases. By providing the corresponding Diagect implementation for different databases, the difference between the database is eliminated, thereby providing a transparent, database-independent storage layer basis for the upper mechanism.
For paging mechanisms, a getLimitstring method is defined in Diagecture, which is used to return a qualified clause to the record according to each database itself, according to each database itself, according to each database itself. For example, the corresponding record qualification clause in mysql is LIMIT, and Oracle can be implemented via the ROWNUM clause.
Let's see the getLimitstring implementation in mysqldiact:
Public String getLimitstring (String SQL, Boolean Hasoffset) {Return New StringBuffer (Sql.Length () 20) .Append (SQL) .append (HASOFFSET? ",?": "limit?"). Tostring (); }
As can be seen from the above, the implementation of the mysqldiaalect.getlimitstring method is actually implemented in addition to the proprietary SQL clause Limit provided by MySQL after a given SELECT statement.
Partial reading of data can be implemented by Oracle's unique ROWNUM clauses.
public String getLimitString (String sql, boolean hasOffset) {StringBuffer pagingSelect = new StringBuffer (sql.length () 100); if (hasOffset) {pagingSelect.append ( ". select * from (select row _ *, rownum rownum_ from (" Else {PAGINGSELECT.APpend ("Select * from (");} Pagingselect.Append (SQL); if (HASOFFSET) {PagingSelect.Append (") Row_ Where Rownum <=?) Where rownum_>?"); } else {pagingselect.Append (") where rownum <=?");} return pagingselect.toString ();
Most mainstream databases provide data partial reading mechanisms, and Hibernate also implements paging through other channels for some databases that do not provide the corresponding mechanism, such as through scrollable resultt, if JDBC does not support scrollable resultset, Hibernate will also Automatically log positioning through the ResultSet's next method. In this way, Hibernate has a good package of paging mechanisms through the underlying mechanism such that developers do not need to care about the details of data paging, separating data logic and storage logic, while improving production efficiency, also greatly strengthened the system in different database platforms. Portability of portability. Simple example:
Query Query = _Session.createQuery (Hql.toString ());
Query.setMaxResults (limited) .SetFirstResult (row) .list ();
This is a common method.
Information: Hibernate Development Guide