Getting Started with Hibernate 26 - Criteria Inquiry

xiaoxiao2021-03-06  45

Getting started 26 - criteria query

Hibernate supports a query API that complies with Java writing habits, using session to create a net.sf.hibernate.criteria, where you do not use SQL or even HQL to perform queries for the database. We have completed the results of the first Hibernate program previously practiced as an example: Getting Started 02 - The first hibernate program If you want to use criteria to query all USER data, you will follow:

Criteria crit = session.createcriteria (user.class);

List users = crit.list ();

For (listiterator itrator = users.listiterator (); item.hasnext ();) {

User User = (user) iterator.next ();

System.out.println ("Name:" User.getName ());

System.out.println ("Age:" User.getage ());

}

If we want to qualify for query, you can set query conditions through net.sf.hibernate.expression.expression, Expression has many conditional query methods, and a practical example description:

Criteria crit = session.createcriteria (user.class);

crit.Add (Expression.ge ("age", new integer (25)));

List users = crit.list ();

The ge () method of Expression is Great-Equal, that is, is greater than or equal to (> =), in the previous example we set the query AGE attribute greater than or equal to 25 USER data. You can also set multiple query conditions, for example:

crit.add (Expression.gt ("age", new integer (20))));

crit.Add (Expression.between ("Weight", New Integer (60), New Integer (80))));

List users = crit.list ();

In the above example we query AGE greater than 20, and weight is between 60 and 80. You can also use logical combinations to query, for example:

crit.add (Expression.or) (EXPRESSION.OR)

Expression.eq ("age", new integer (20)),

Expression.isnull ("age")

));

List users = crit.list ();

If you want to sort the results, you can use Net.sf.Hibernate.Expression.Order, for example:

List cats = session.createcriteria (user.class)

.add (Expression.ge ("Age", New Integer (20));

.addorder (Order.asc ("Name"))

.addorder (ORDER.DESC ("age"))

.SETMAXRESULTS (50)

.list ();

The setMaxResults () method can qualify the number of queries, if the matching setFirstResult () settings the first data of the query result, simple paging, for example: criteria crit = session.createcriteria (user.class) ;

Crit.setfirstResult (51);

Crit.setMaxResult (50);

List users = crit.list ();

The above example will pass the data after the 51st (51 to 100). In fact, criteria, expression, order, etc., is packaged for SQL, allowing the Java programmer to write queries from habitual syntax, without using HQL or SQL (interested, you can set in the configuration file) Show SQL, look at the corresponding SQL), however Criteria is still not perfect in Hibernate, only some simple queries, for queries, Hibernate still encourages the use of HQL as the preferred way of query. The use of criteria is quite simple, only some basic, interested, in Chapter 12 of the Hibernate Reference Manual, has more query.

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

New Post(0)