Hibernate queries are broadly divided into the following three scenarios, 1. HQL Queries-hibernate query Language (multi-table query, but not complex when used) 2. Criteria query (single-table conditional query) 3. Native SQL queries (complex business queries) next explain three ways to use: 1.HQL queries-hibernate query Language (multi-table query, but not complex) Hibernate exclusive query language, belongs toObject-oriented query language
Note: Tables and columns in SQL do not appear in the HQL language, HQL use JavaBean class names and property names.
1.HQL Basic Query
(1) Query all the basic statements
@Test//hql querying all data Public voidfun1 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL StatementsString hql = "from Cn.qlq.domain.Customer";//From class name full path//3. Create a Query object based on HQLQuery query =session.createquery (HQL); //4. Get query results based on query objectList<customer> customers =query.list (); SYSTEM.OUT.PRINTLN (Customers); }
Results:
Hibernate:Selectcustomer0_.cust_id ascust_id1_0_, Customer0_.cust_name ascust_nam2_0_, Customer0_.cust_source ascust_sou3_0_, Customer0_.cust_industry ascust_ind4_0_, Customer0_.cust_level ascust_lev5_0_, Customer0_.cust_linkman ascust_lin6_0_, Customer0_.cust_phone ascust_pho7_0_, Customer0_.cust_mobile ascust_mob8_0_ fromCst_customer customer0_[Customer [cust_id=1, Cust_name=xxxxxxxxxx], Customer[cust_id=2, cust_name= Lenovo]]
Improved: If the entire project has only one class name can omit the package path, that is, you can write only the class name:
@Test//hql querying all data Public voidfun1 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL Statements//String hql = "from Cn.qlq.domain.Customer";//From class name full pathString hql = "from Customer";//If there is only one class name in the entire project, you can write the name directly//3. Create a Query object based on HQLQuery query =session.createquery (HQL); //4. Get query results based on query objectList<customer> customers =query.list (); SYSTEM.OUT.PRINTLN (Customers); }
(2) Query the individual by the primary key
@Test//hql querying individual data Public voidfun2 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL Statements//String hql = "from Cn.qlq.domain.Customer";//From class name full pathString hql = "from Customer where cust_id = 1";//where is followed by the property name of the customer, not the column name//3. Create a Query object based on HQLQuery query =session.createquery (HQL); //4. Get query results based on query objectCustomer customer =(Customer) Query.uniqueresult (); SYSTEM.OUT.PRINTLN (customer); }
2.HQL condition Query:
(1)-placeholder query
Like a JDBC placeholder, just hibernate? Subscript starts at 0, and JDBC subscript starts at 1, and basically all of the programming indexes start at 0, except that JDBC starts at 1 ....
@Test//HQL's? Placeholder query Public voidFun3 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL Statements//String hql = "from Cn.qlq.domain.Customer";//From class name full pathString hql = "from Customer where cust_id =?";//If there is only one class name in the entire project, you can write the name directly//3. Create a Query object based on HQLQuery query =session.createquery (HQL);//query.setlong (0, 1l);//a JDBC-like placeholder, just JDBC placeholder Poute starting from 0, hibernate starting from 1Query.setparameter (0, 1l);//This kind of writing does not need tube type//4. Get query results based on query objectCustomer customer =(Customer) Query.uniqueresult (); SYSTEM.OUT.PRINTLN (customer); }
(2) Command placeholder: Name format query, fixed format, name casual, habitual start and conditional name
@Test//hql Command placeholder query Public voidFun4 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL Statements//String hql = "from Cn.qlq.domain.Customer";//From class name full pathString hql = "from Customer where cust_id =: cust_id";//: cust_id's name is random, but it's customary to do the same .//3. Create a Query object based on HQLQuery query =session.createquery (HQL);//query.setlong (0, 1l);//a JDBC-like placeholder, just JDBC placeholder Poute starting from 0, hibernate starting from 1Query.setparameter ("cust_id", 1l); //4. Get query results based on query objectCustomer customer =(Customer) Query.uniqueresult (); SYSTEM.OUT.PRINTLN (customer); }
3.HQL Paging Query
The paging query is similar to the Limit keyword for mysql, limit start,pagesize ...
@Test//hql Paging Query Public voidfun5 () {//1 Getting sessionSession session =hibernateutil.opensession (); //2. Writing HQL Statements//String hql = "from Cn.qlq.domain.Customer";//From class name full pathString hql = "from Customer";//: cust_id's name is random, but it's customary to do the same .//3. Create a Query object based on HQLQuery query =session.createquery (HQL); /*** similar to limit start,pagesize; * Assume page size is 2 * page number start value page Size * 1 0 2 * 2 2 2*/ //For example, take the second page of dataQuery.setfirstresult (2); Query.setmaxresults (2); //4. Get query results based on query objectList<customer> customers =query.list (); SYSTEM.OUT.PRINTLN (Customers); }
Results:
Hibernate:Selectcustomer0_.cust_id ascust_id1_0_, Customer0_.cust_name ascust_nam2_0_, Customer0_.cust_source ascust_sou3_0_, Customer0_.cust_industry ascust_ind4_0_, Customer0_.cust_level ascust_lev5_0_, Customer0_.cust_linkman ascust_lin6_0_, Customer0_.cust_phone ascust_pho7_0_, Customer0_.cust_mobile ascust_mob8_0_ fromcst_customer customer0_ limit?,?[Customer [cust_id=3, cust_name= Blog Park], Customer[cust_id=4, Cust_name= still]]
2.Criteria Query (single-table conditional query)
Hibernate bulk Query