Hibernate mainly has three kinds of query methods:
1.HQL (Hibernate Query Language)
Similar to SQL, it supports features such as paging, joins, grouping, aggregate functions, and subqueries, but HQL is object-oriented, not tables in relational databases. Because the query statement is oriented to the domain object, using HQL can gain cross-platform benefits, and Hibernate will automatically help us translate different SQL statements from different databases. This is very convenient in applications that need to support multiple databases or database migrations.
But get convenient at the same time, because the SQL statement is generated automatically by hibernate, so this is not conducive to the efficiency of SQL statements optimization and debugging, when the data volume is very large may have the problem of efficiency, out of the problem is not easy to troubleshoot and solve.
2.qbc/qbe (Query by Criteria/example)
QBC/QBE is the execution of queries by assembling query conditions or template objects. This is convenient in applications where there is a need to flexibly support the free combination of many query conditions. The same problem is that because query statements are freely assembled, the code that creates a statement can be lengthy and contains many branching conditions that are not easy to optimize and debug.
3.SQL
Hibernate also supports queries that execute SQL directly. This approach at the expense of the advantages of hibernate cross-database, hand-written low-level SQL statements, so as to achieve the best execution efficiency, compared to the first two methods, optimization and debugging a little easier.
Let's look at a simple set of examples.
Package com.cdai.orm.hibernate.query;
Import Java.util.Arrays;
Import java.util.List;
Import Org.hibernate.Criteria;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.AnnotationConfiguration;
Import org.hibernate.criterion.Criterion;
Import Org.hibernate.criterion.Example;
Import org.hibernate.criterion.Expression;
Import Com.cdai.orm.hibernate.annotation.Account; public class Basicquery {public static void main (string[] args) {sessionfactory sessionfactory = new annotationconf
Iguration ().
AddFile ("Hibernate/hibernate.cfg.xml").
Configure ().
Addpackage ("Com.cdai.orm.hibernate.annotation").
Addannotatedclass (Account.class).
Buildsessionfactory ();
Session session = Sessionfactory.opensession ();
1.HQL query query = Session.createquery ("From account as a where a.id=:id");
Query.setlong ("id", 1);
List result = Query.list (); for (ObjectRow:result) {System.out.println (row);
}//2.QBC the criteria criteria = Session.createcriteria (Account.class);
Criteria.add (EXPRESSION.EQ ("id", New Long (2));
result = Criteria.list ();
for (Object Row:result) {System.out.println (row);
//3.QBE Account example= new account ();
Example.setbalance (100);
result = Session.createcriteria (Account.class).
Add (Example.create (Example)).
List ();
for (Object Row:result) {System.out.println (row);
}//4.SQL query = Session.createsqlquery ("SELECT top * to Tb_account ORDER by col_id Desc");
result = Query.list ();
for (Object Row:result) {System.out.println (arrays.tostring (object[) row));
} session.close ();
}
}Hibernate:select account0_.col_id as col1_0_, account0_.col_balance as col2_0_ from Tb_account account0_ where account0_. Col_id=? account [Id=1, balance=100]
Hibernate:select this_.col_id as col1_0_0_, this_.col_balance as col2_0_0_ from Tb_account where This_?
account [id=2, balance=100]
Hibernate:select this_.col_id as col1_0_0_, this_.col_balance as col2_0_0_ from Tb_account This_ where (this_.col_balance =?)
account [Id=1, balance=100]
account [id=2, balance=100]
Hibernate:select Top * from Tb_account ORDER BY col_id Desc
[2, 100]
[1, 100]
From the log can be clearly seen hibernate for the generation of SQL statements control, the specific choice of which query method to see the specific application.