Hibernate Criteria Grouping Sort Association

Source: Internet
Author: User
Tags rowcount

You can use the criteria to query and sort the results using order, such as using ODER.ASC () to sort from small to large (and vice versa with Desc ()):

Criteria criteria = Session.createcriteria (User.class);

Criteria.addorder (ORDER.ASC ("Age"));

List users = Criteria.list ();

The Setmaxresults () method can limit the number of pens returned by the query, and if the Setfirstresult () setting returns the location of the first data of the query result, a simple paging can be achieved, such as 50 data after the 51st (if any):

Criteria criteria = Session.createcriteria (User.class);

Criteria.setfirstresult (51);

Criteria.setmaxresult (50);

List users = Criteria.list ();

You can perform statistical actions on the query results by using the projections AVG (), rowcount (), COUNT (), Max (), Min (), countdistinct (), and so on, such as averaging the "age" of the query result:

Criteria criteria = Session.createcriteria (User.class);

Criteria.setprojection (Projections.avg ("Age"));

List users = Criteria.list ();

Iterator iterator = Users.iterator ();

while (Iterator.hasnext ()) {

System.out.println (Iterator.next ());

}

You can also group the results with projections groupproperty (), such as grouping with "age", which means that if there are 20, 20, 25, 30 in the data, then 20, 25, 30 are displayed:

Criteria criteria = Session.createcriteria (User.class);

Criteria.setprojection (Projections.groupproperty ("Age"));

List users = Criteria.list ();

Iterator iterator = Users.iterator ();

while (Iterator.hasnext ()) {

System.out.println (Iterator.next ());

}

If you want to combine the statistics and grouping features, you can use projectionlist, for example, the following program calculates how many individuals each age:

Projectionlist projectionlist = Projections.projectionlist ();

Projectionlist.add (Projections.groupproperty ("Age"));

Projectionlist.add (Projections.rowcount ());

Criteria criteria = Session.createcriteria (User.class);

Criteria.setprojection (projectionlist);

List users = Criteria.list ();

Iterator iterator = Users.iterator ();

while (Iterator.hasnext ()) {

object[] o = (object[]) iterator.next ();

System.out.println (O[0] + "T" + o[1]);

}

If you have a known object, you can use the object as a basis for the query to see if there are any properties similar to the object, for example:

User user = new user ();

User.setage (New Integer (30));

Criteria criteria = Session.createcriteria (User.class);

Criteria.add (example.create (user));

List users = Criteria.list ();

Iterator iterator = Users.iterator ();

SYSTEM.OUT.PRINTLN ("ID \ t name/age");

while (Iterator.hasnext ()) {

User ur = (user) iterator.next ();

System.out.println (Ur.getid () +

"\ T" + ur.getname () +

"/" + ur.getage ());

}

In this example, the user object has a known attribute "age" of 30, and using example automatically filters out the null attribute of the user and uses it as the basis for the query, which is to find the "age" of the same 30 data.

Criteria can be a compound query, that is, in the original query based on the query, for example, in the room to user of a One-to-many association, after querying all the room data, I would like to query users of "age" 30 of user data:

Criteria Roomcriteria = Session.createcriteria (Room.class);

Criteria Usercriteria = Roomcriteria.createcriteria ("users");

Usercriteria.add (Restrictions.eq ("Age", new Integer (30));

List rooms = Roomcriteria.list (); Lists only room with "age" of user in the users attribute (30)

Iterator iterator = Rooms.iterator ();

Hibernte criteria query only one of the fields of a table:

Criteria Criteria=session.createcriteria (User.class);       Projectionlist prolist = projections.projectionlist ()//Set Projection collection Prolist.add (Projections.property ("UserName"));      Prolist.add (Projections.property ("password")); Criteria.setprojection (prolist);
Statistics + grouping + associated query

Criteria C = getsession (). Createcriteria (Infos.class);
C.add (restrictions.in ("InfoType", InfoType));
C.add (Restrictions.eq ("Infostatus", 3));
C.setprojection (Projections.projectionlist ()
. Add (Projections.groupproperty ("InfoType"))
. Add (Projections.max ("Infotime"), "Infotime"));
list<object> L = c.list ();
Object[] Types=new object[l.size ()];
Timestamp[] Times=new timestamp[l.size ()];
int i=0;
for (Object o:l) {
Object[]obj= (object[]) o;
TYPES[I]=OBJ[0];
times[i]= (Timestamp) obj[1];
i++;
}
C=getsession (). Createcriteria (Infos.class);
C.add (restrictions.in ("InfoType", types))
. Add (Restrictions.in ("Infotime", Times));
List<infos> list=c.list ();


Criteria use associated queries

Note: 1 ' pubstationsv is a pubtabtimedate persistent-associated object name, not a persistent class name

2 ' projectionlist can only be used once multiple times useless

3 ' Alias is critical (PV) otherwise the attribute in the admin table cannot find the error

Session session = Pubtabtimedatedao.getcurrentsession ();
        criteria c = Session.createcriteria (Pubtabtimedate.class, "P");
        projectionlist projectionlist=projections.projectionlist ();
        projectionlist.add (projections.distinct (Projections.property) (" P.fstationnum "));
        projectionlist.add (Projections.property ("pv.faddress"));
        c.addorder (Order.desc ("P.fstationnum"))
         .setfirstresult (0)
        .setmaxresults ()
        criteria cc = C.createcriteria ("Pubstationsv", "PV");
        c.setprojection (projectionlist);
        list List = Cc.list ();


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.