Various queries in the Hibernate--hibernate

Source: Internet
Author: User

first, through the OID query
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    Customer cs = session.get/load (Customer.class, 1L); Load is, until the use of the time to know    System.out.println (CS);    ----------------------------------------        tx.commit ();        Session.close ();}
Second, hql query1. Basic Inquiry
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql = "from Customer";    Query query = session.createquery (HQL);    list<customer> list = Query.list ();      Get all data    //customer cs = (Customer) query.uniqueresult ();  Get a single piece of data (only one to use)    System.out.println (list);    ----------------------------------------        tx.commit ();        Session.close ();}

2. Conditional query

@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql1 = "from Customer where cust_id=?";      A placeholder    String hql2 = "from Customer where Cust_id=:id";    Named placeholder    query query = session.createquery (HQL2);    Query.setparameter (0,2l);    Query.setparameter ("id", 2L);    list<customer> list = Query.list ();      Get all data    Customer cs = (customer) query.uniqueresult ();  Get a single piece of data (only one to use)    System.out.println (CS);    ----------------------------------------        tx.commit ();        Session.close ();}
3. Sorting
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql1 = "from the Customer Order by cust_id ASC";      Positive sequence    String hql2 = "from Customer ORDER by cust_id Desc";      Inverse row    Query query = session.createquery (HQL1);    list<customer> list = Query.list ();    SYSTEM.OUT.PRINTLN (list);    ----------------------------------------        tx.commit ();        Session.close ();}
4, paging query
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql = "from Customer";    Query query = session.createquery (HQL);    Query.setfirstresult (1);    Query.setmaxresults (3);    List<customer> li = query.list ();    System.out.println (LI);    ----------------------------------------        tx.commit ();        Session.close ();}
5. Aggregate query
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql1 = "SELECT count (*) from Customer";        Returns the number of effects (row)    String hql2 = "Select Max (cust_id) from Customer";    Maximum value    String hql3 = "Select min (cust_id) from Customer";    Minimum value    String hql4 = "Select AVG (cust_id) from Customer";    Average    String hql5 = "Select sum (cust_id) from Customer";    Sum    query query = session.createquery (HQL4);    Number num = (number) query.uniqueresult ();    SYSTEM.OUT.PRINTLN (num);    ----------------------------------------        tx.commit ();        Session.close ();}
6. Projection Query
@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql = "Select Cust_id,cust_name from Customer";    Query query = session.createquery (HQL);    List li = query.list ();    System.out.println (LI);    ----------------------------------------        tx.commit ();        Session.close ();}
7. Multi-Table Query

Multi-table query for SQL

Cross Connect-Cartesian product (preferably not used, will detect duplicate data) Inner Connection  |-implicit inner Connection select * from A, where b.aid=a.id  |-Display inner Connection select * from A inner join B on B.A id=a.id Outer Connection  |-LEFT outer SELECT * from a ieft [outer] join B on b.aid=a.id  | Right outer select * from a OK [outer] join B on B.A Id=a.id

HQL Multi-table query (generally not, rather than using native SQL)

1. Internal connection

@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql = "from Customer C inner join C.linkmens";    Query query = session.createquery (HQL);    List<object[]> li = query.list ();    For (object[] o:li) {        System.out.println (arrays.tostring (o));    }    ----------------------------------------        tx.commit ();        Session.close ();}

[Customer [Cust_id=1, cust_name= xin Brother thesis], linkman{lkm_id=5, lkm_name= ' Tom '}]
[Customer [Cust_id=1, cust_name= xin Brother thesis], linkman{lkm_id=6, lkm_name= ' è?? é????? '}]
[Customer [Cust_id=1, cust_name= xin Brother thesis], linkman{lkm_id=7, lkm_name= ' Ji Zhenyu '}]

2, internal connection urgently

There is a fetch on the query statement that returns the Customer object

public void Func1 () {Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------String hql = "from Customer c inner join fetch c.linkmens";    Query query = session.createquery (HQL);    List<customer> li = query.list ();    System.out.println (LI);        ----------------------------------------Tx.commit (); Session.close ();} 

[Customer{cust_id=1, Cust_name= ' Baidu Company ', linkmens=[linkman{lkm_id=1, lkm_name= ' Yu Jiaxin '}, linkman{lkm_id=6, lkm_name= ' è?? e????? '}]}, customer{cust_id=2, Cust_name= ' Google Inc. ', linkmens=[linkman{lkm_id=3, lkm_name= ' Zhangbao '}], customer{cust_id=3 , Cust_name= ' linkmens=[linkman{lkm_id=4, lkm_name= ' Tom '}]}, customer{cust_id=4, cust_name= ' fast sowing ', linkmens=[ linkman{lkm_id=5, lkm_name= ' Tom '}]}, Customer{cust_id=1, Cust_name= ' Baidu Company ', Linkmens=[linkman{lkm_id=1, Lkm_name= ' Yu Jiaxin '}, linkman{lkm_id=6, lkm_name= ' è?? é????? '}]}]

3. Left/Right outer connection (urgent)

@Testpublic void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    String hql = "from the Customer C left join C.linkmens";    Query query = session.createquery (HQL);    List<object []> li = query.list ();    for (Object [] o:li) {        System.out.println (arrays.tostring (o));    }    ----------------------------------------        tx.commit ();        Session.close ();}
third, criteria query1. Basic Inquiry
public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    criteria = Session.createcriteria (customer.class);    List List = Criteria.list ();    SYSTEM.OUT.PRINTLN (list);    ----------------------------------------        tx.commit ();        Session.close ();}

2. Conditional query

public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    criteria = Session.createcriteria (customer.class);    Criteria.add (Restrictions.eq ("cust_id", 2L));     Add query condition    //list List = Criteria.list ();    Customer result = (customer) criteria.uniqueresult ();    SYSTEM.OUT.PRINTLN (result);    ----------------------------------------        tx.commit ();        Session.close ();}

3, paging query

public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    criteria = Session.createcriteria (customer.class);    Criteria.setfirstresult (2);    Criteria.setmaxresults (3);    list<customer> list = Criteria.list ();    SYSTEM.OUT.PRINTLN (list);    ----------------------------------------        tx.commit ();        Session.close ();}

4. Sorting

public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    criteria = Session.createcriteria (customer.class);    Criteria.addorder (ORDER.ASC ("cust_id"));    Positive sequence    Criteria.addorder (Order.desc ("cust_id"));   Reverse    list<customer> List = Criteria.list ();    SYSTEM.OUT.PRINTLN (list);    ----------------------------------------        tx.commit ();        Session.close ();}

5. Aggregation Operations

public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    criteria = Session.createcriteria (customer.class);    Criteria.setprojection (Projections.rowcount ());     Query sum number    num = (number) criteria.uniqueresult ();    SYSTEM.OUT.PRINTLN (num);    ----------------------------------------        tx.commit ();        Session.close ();}
offline Query

Non-offline

Offline (the DAO layer can not be changed, we construct the query condition in front of it all right)

Code implementation

four, query optimization

Load method, SQL query only when used

In fact, the load method is to turn the customer object into a Super Customer object, query the database, and when called (after querying the database), it becomes the normal customer object

public void Func1 () {    Session session = Hibernateutils.opensession ();    Transaction tx = Session.begintransaction ();    ----------------------------------------    Customer load = session.load (Customer.class, 2L);    SYSTEM.OUT.PRINTLN (load);    ----------------------------------------        tx.commit ();        Session.close ();}

It is actually dependent on the session, so Session.close () will be error-

So we can use filter to solve

Dynamic Proxy: ($ is the proxy object when the object is printed)

The purpose of acting for an object: modification or enhancement of methods

Connection pool:
Purpose: To change the closing method of the Connection object (cannot let the connection really shut down should be put back to the connection pool), the connection agent

Solve Chinese garbled characters:
Agent for Request object, transform Getparametermap () ... method, which becomes no garbled

Class-Level lazy loading:
You can query the database by turning the customer object into a Super customer object

Various queries in the Hibernate--hibernate

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.