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