Calculate data in fields through Hibernate. In JDBC, you use SQL sum and other methods. When using Hibernate, you need to use the Projections object.
First, let’s introduce Projections:
org.hibernate.criterion.Projections is the instance factory of Projection. We use the Projections object to perform a query by calling setProjection().
Let’s get started:
It is also necessary to create a Criteria object. This object is very important. It almost completes most of the query functions in Hibernate. When used with HQL, it is very powerful.
Java code
Criteria criteria = session.createCriteria(AccCasherDaySupply.class);
Criteria criteria = session.createCriteria(AccCasherDaySupply.class);
Create a Criteria object through Session, and the parameter is the class name of the object you want to query (note that it is case-sensitive here).
The setProjection method can be used through the Criteria object, which will perform a Projections query.
Java code
criteria.setProjection(****);
criteria.setProjection(****);
The next step is to perform the actual aggregation operation, using the Projections object to insert into the parameters of ****.
The Projects object has most aggregation methods, basically: rowCount(), avg(), max(), groupProperty(), alias(), property(), sum()...
Taking sum() as an example, if you need to calculate the sum of a certain field data, the sum() method will be used:
Java code
criteria.setProjection(Projections.sum("field name"));
criteria.setProjection(Projections.sum("field name"));
Through the above code, criteria will get the combination of the corresponding field names.
Finally, the final results of the Projections query are obtained through criteria.list().
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/lyr1985/archive/2009/12/30/5105409.aspx
-