The projection operation of the Hiberante object mode and the projection of the SQL statement (the hibernate usage of the aggregation function)
Recently used hibernate projection, write a diary to record.
(⊙o⊙) ... Prerequisites: Paired with Hibernate environment. MyClass refers to an entity class that has been mapped well
The following 3 functions are the methods I write to test the projection: These three methods I write in Basedao (base DAO class, do common DAO operations)
1 /**2 * Increase under bin Lin! 3 * <p/>4 *---(⊙o⊙) ...5 * <p/>6 * *7 */8 Public<T> list<t> gettouyinglist_min (String []s, class<t>MyClass)9 {TenCriteria c=getsession (). Createcriteria (MyClass); OneProjectionlist p=projections.projectionlist (); A for(inti=0;i<s.length;i++) - { - p.add (Projections.max (s[i)); the } -List l=c.setprojection (P). list (); - returnl; - } + Public<T> list<t> Gettouyinglist_max (String []s, class<t>MyClass) - { +Criteria c=getsession (). Createcriteria (MyClass); AProjectionlist p=projections.projectionlist (); at for(inti=0;i<s.length;i++) - { - p.add (Projections.max (s[i)); - } -List l=c.setprojection (P). list (); - returnl; in } - to PublicList getsqlquerycolumnlist (String sql) + { -List l=getsession (). createsqlquery (SQL). List (); the returnl; * $}
-----------"Usage:
The first method is obviously the method of calculating the minimum value of a column. The incoming string array represents the column life that you need to calculate, and this method is specifically for the minimum value I wrote. As for the general approach, you can use the third method of SQL statements.
1 Public voidTesttouting ()//Projection Oh2 {3String []s={"Trafficmoney", "Foodmoney"};4 5List L=basedao.gettouyinglist_min (S, Expense.class);6 7Iterator i=l.iterator ();8 while(I.hasnext ())//the projection test detects each projection field and forms an array. There are multiple results that make up an array of objects! 9 {TenObject[] O=(object[]) i.next (); OneSYSTEM.OUT.PRINTLN ("Minimum traffic amount" +o[0]); ASYSTEM.OUT.PRINTLN ("Minimum amount of food" +o[1]); - } - the}
The second method asks for the maximum value.
1 Public voidTesttouting ()//Projection Oh22 {33 String []s={"Foodmoney", "Carmoney"};4455 List L=basedao.gettouyinglist_max (S, Expense.class);6677 Iterator i=l.iterator ();88 while(I.hasnext ())//the projection test detects each projection field and forms an array. There are multiple results that make up an array of objects! 99 {TenTen object[] o=(object[]) i.next (); OneSYSTEM.OUT.PRINTLN ("Maximum amount of food" +o[0]); ASYSTEM.OUT.PRINTLN ("Maximum amount of vehicle use" +o[1]); -13 } -14 the15}
A third method applies:
11 String datesql= "select min (accountdate), Max (accountdate), sum (case-isinvoice=1 then-money else 0 end) as Invoicemo Ney where rigisteruser= ' Wuhuanbin '2 3List listformoney=basedao.getsqlquerycolumnlist (Moneysql);//Get what you deserve list4 5Iterator Moneyiterator=listformoney.iterator ();//Get iterators6Object[] o= (object[]) moneyiterator.next ();//Each result is an array of objects, and there should be only one result, because the aggregation is a single piece. 7System.out.println ("Start date" +o[0]+ "End Date" +o[1]);8 9Totalmoeny=double.parsedouble (o[2].tostring ());Ten OneSYSTEM.OUT.PRINTLN ("Total invoice Amount" +o[3]);
If there is no aggregation here, for example, if you find that the SELECT * result is an array object o[], which is a number of object arrays.
Have any questions and suggestions, welcome to communicate!! Progress together!!
The projection operation of the Hiberante object mode and the projection of the SQL statement (the hibernate usage of the aggregation function)