Hibernate Learning---section tenth: Hibernate QBC, sample queries & offline queries

Source: Internet
Author: User

First, QBC (Query by Criteria) mainly consists of Criteria,criterion,oder,restrictions class

1. The Java code is as follows:

/*** Check All*/@Test Public voidTestquery () {//Get Criteria ObjectCriteria = Session.createcriteria (person.class); List<Person> list =criteria.list ();  for(person p:list) {System.out.println (P); }}/*** Conditional Query*/@Test Public voidTestQuery2 () {//Get Criteria ObjectCriteria = Session.createcriteria (person.class); //Query id attribute is less than or equal to 5 recordsCriteria.add (Restrictions.le ("id", 5)); //record with Query id attribute equal to 5Criteria.add (RESTRICTIONS.EQ ("id", 5)); //Fuzzy Query The Name property contains a record of-8Criteria.add (Restrictions.like ("name", "%-8%")); //querying age attribute records between 21 and 24 years oldCriteria.add (Restrictions.between ("Age", 21, 24)); //Query id attribute equals 2,5,7 recordCriteria.add (restrictions.in ("id",Newobject[]{2,5,7})); //do ascending to the id attributeCriteria.addorder (ORDER.ASC ("id")); //to make the id attribute descendingCriteria.addorder (ORDER.DESC ("id")); List<Person> list =criteria.list ();  for(person p:list) {System.out.println (P); }}/*** Conditional Query * The binding of query conditions through static methods of the property class * Actually, the bottom is the encapsulation of restrictions.*/@Test Public voidTestQuery3 () {//Get Criteria ObjectCriteria = Session.createcriteria (person.class); //Query id attribute is less than or equal to 5 recordsCriteria.add (Property.forname ("id"). Le (5)); //querying age attribute records between 21 and 24 years oldCriteria.add (Property.forname ("id"). Between (3, 6)); List<Person> list =criteria.list ();  for(person p:list) {System.out.println (P); }}

qbc:http://blog.csdn.net/agromach/article/details/1932290

Second, sample query

1. The Java code is as follows:

 /**   */ @ Test  public  void   TestQuery4 () {person P  = new      Person ();    P.setname ( "ldh-8"  131 = Session.createcriteria (Person.  <Person> list = Criteria.list ();  for   (person person:list) {System.out.prin TLN (person); }}

Three, offline query

 /**   * Offline Query *              You can not need a session before binding query data and conditions (open session at the latest) * When you are really using it, pass a current session * * session using the technique: * Latest Open * First close * Do not open  */  @Test  public  void   TestQuery5 () { Detachedcriteria DC  = Detachedcriteria.forclass (Person.    Class   ID, 5, 8 = Dc.getexecutablecriteria (session);    List  <Person> list = Criteria.list ();  for   (person person:list) {System.out.prin    TLN (person); }}

Hibernate Learning---section tenth: Hibernate QBC, sample queries & offline queries

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.