Hibernate framework study note 11: Detailed description of Criteria query, hibernatecriteria

Source: Internet
Author: User

Hibernate framework study note 11: Detailed description of Criteria query, hibernatecriteria

Create an object class object:

Package domain; import java. util. hashSet; import java. util. set; // Customer entity public class Customer {private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; // use the set Set to express one-to-multiple relationships. private Set <LinkMan> linkMens = new HashSet <LinkMan> (); public set <LinkMan> getLinkMens () {return linkMens;} public void setLinkMens (Set <LinkMan> linkMens) {this. linkMens = linkMens;} public Long getCust_id () {return cust_id;} public void setCust_id (Long cust_id) {this. cust_id = cust_id;} public String getCust_name () {return cust_name;} public void setCust_name (String cust_name) {this. cust_name = cust_name;} public String getCust_source () {return cust_source;} public void setCust_source (String cust_source) {this. cust_source = cust_source;} public String getCust_industry () {return cust_industry;} public void setCust_industry (String cust_industry) {this. cust_industry = cust_industry;} public String getCust_level () {return cust_level;} public void setCust_level (String cust_level) {this. cust_level = cust_level;} public String getCust_linkman () {return cust_linkman;} public void setCust_linkman (String cust_linkman) {this. cust_linkman = cust_linkman;} public String getCust_phone () {return cust_phone;} public void setCust_phone (String cust_phone) {this. cust_phone = cust_phone;} public String getCust_mobile () {return cust_mobile;} public void setCust_mobile (String cust_mobile) {this. cust_mobile = cust_mobile;} @ Override public String toString () {return "Customer [cust_id =" + cust_id + ", cust_name =" + cust_name + "]" ;}}
View Code

 

Custom tool class:

Package utils; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; public class HibernateUtils {private static SessionFactory sf; static {// 1 Creation, call the null parameter to construct Configuration conf = new Configuration (). configure (); // 2 create the SessionFactory object sf = conf Based on the configuration information. buildSessionFactory ();} // get session => get new session public static Session openSession () {// 3 get session Session session = sf. openSession (); return session;} // get session => get the bound session public static Session getCurrentSession () {// 3 get session Session session = sf. getCurrentSession (); return session ;}}
View Code

 

Test example:

Basic Syntax:

Package criteria; import java. util. list; import org. hibernate. criteria; import org. hibernate. session; import org. hibernate. transaction; import org. hibernate. criterion. order; import org. hibernate. criterion. projections; import org. hibernate. criterion. restrictions; import org. junit. test; import domain. customer; import utils. hibernateUtils; // learn the Criteria syntax public class Demo {@ Test // basic syntax public void fun1 () {Sessio N session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Criteria c = session. createCriteria (Customer. class); List <Customer> list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close () ;}@ Test // condition syntax //> gt //> = ge // <lt // <= le // = eq //! = Ne // in // between and between // like // is not null isNotNull // is null isNull // or/and public void fun2 () {Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Criteria c = session. createCriteria (Customer. class); // c. add (Restrictions. idEq (2l); c. add (Restrictions. eq ("cust_id", 2l ); List <Customer> list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close () ;}@ Test // paging syntax-same as HQL public void fun3 () {Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Criteria c = session. createCriteria (Customer. class); // limit ?,? C. setFirstResult (0); c. setMaxResults (2); List <Customer> list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close () ;}@ Test // sort syntax public void fun4 () {Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Criteria c = session. createCriteria (Customer. class); c. addOrder (Order. asc ("cust_id"); // ascending // c. addOrder (Order. desc ("cust_id"); List in descending order <Customer> list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close () ;}@ Test // statistical syntax public void fun5 () {Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Criteria c = session. createCriteria (Customer. class); // set the query target (aggregate function) c. setProjection (Projections. rowCount (); List list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close ();}}

 

 

Offline query:

 

Traditional criteria objects are created based on sessions, and the query conditions transmitted by the front-end pass through the WEB and SERVICE layers to the DAO layer in sequence,

Assemble the query conditions on the DAO layer to criteria and execute the query.

 

The offline criteria object is unrelated to the session and is created out of thin air. You can create a criteria object on the WEB layer and assemble query conditions,

At this time, you can directly pass the criteria object to the DAO layer for query. At this time, a DAO method is implemented for all queries.

 

Package criteria; import java. util. list; import org. hibernate. criteria; import org. hibernate. session; import org. hibernate. transaction; import org. hibernate. criterion. detachedCriteria; import org. hibernate. criterion. order; import org. hibernate. criterion. projections; import org. hibernate. criterion. restrictions; import org. junit. test; import domain. customer; import utils. hibernateUtils; // learn offline Criteriapublic class Demo2 {@ Test public void fun1 () {// Service/web layer DetachedCriteria dc = DetachedCriteria. forClass (Customer. class); dc. add (Restrictions. idEq (5l); // assembly conditions (all consistent with common Criteria) // -------------------------------------------------------- Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ // Criteria c = dc at the Dao layer. getExecutableCriteria (session); List list = c. list (); System. out. println (list); // ------------------------------------------------------ tx. commit (); session. close ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.