Hibernate, in addition to working with objects in a query result set, can use the results in the result set as rows and column sets, similar to how the data obtained through JDBC performs a select query. Therefore, hibernate also supports queries such as attributes, statistical functions, and GROUP by.
To use Hibernate's projection statistics function, first obtain the Org.hibernate.criterion.Projection object from the Org.hibernate.criterion.Projections factory class. Similar to the restrictions class, the projections class provides several static factory methods for obtaining projection instances. After you have obtained the projection object, use the Setprojection () method to add it to the criteria object. Note that the returned result set is of type object and requires an appropriate type conversion of the result.
Hibernate's projections factory class contains several commonly used statistical functions:
①avg (String PropertyName): Calculates the average of an attribute field.
②count (String PropertyName): Counts the number of occurrences of an attribute in the result.
③countdistinct (String PropertyName): The Count property contains the number of distinct values.
④max (String PropertyName): Calculates the maximum value of a property value.
⑤min (String PropertyName): Calculates the minimum value of a property value.
⑥sum (String PropertyName): Calculates the sum of the property values.
The following example shows how some statistical functions and projection lists are used:
- GetSession (). BeginTransaction ();
- Criteria crit = GetSession (). Createcriteria (Product. Class);
- Projectionlist projlist = Projections.projectionlist ();
- Projlist.add (Projections.max ("price"));
- Projlist.add (Projections.min ("price"));
- Projlist.add (Projections.avg ("price"));
- Projlist.add (projections.countdistinct ("description"));
- Crit.setprojection (projlist);
- List result = Crit.list ();
- GetSession (). Gettransaction (). commit ();
The above example performs several statistical projections. When multiple statistical projections are executed, a list is obtained and is a list of type object, which in turn contains all the statistical projection results.
One advantage of using projections is that the result is a separate property rather than an entity class. For example, a product table contains many fields, and we want to get the names and descriptions in the product tables without having to load the entire entity into memory.
- Criteria crit = GetSession (). Createcriteria (Product. Class);
- Projectionlist projlist = Projections.projectionlist ();
- Projlist.add (Projections.property ("name"));
- Projlist.add (Projections.property ("description"));
- Crit.setprojection (projlist);
- List result = Crit.list ();
Using this style of query can reduce the amount of network traffic between the application server and the database server. However, if the memory capacity of the client is indeed limited, this query can avoid the stress of handling large datasets for memory. If you are unsure whether you want a complete result set later, you will have to perform another query, which reduces query performance. Hibernate projection can only be used when appropriate.
So how do you get the results that are not duplicated in the result set? the method is:
Distinct (Projection proj): The distinct value of the statistic attribute.
- GetSession (). BeginTransaction ();
- Criteria = GetSession (). Createcriteria (Transaction. Class);
- Projectionlist prolist = Projections.projectionlist ();
- Prolist.add (Projections.distinct (Projections.property ("module" ));
- Criteria.setprojection (prolist);
- Criteria.addorder (ORDER.ASC ("ordersign"));
- List = Criteria.list ();
- GetSession (). Gettransaction (). commit ();
Finally, you can use the Groupproperty projection to group the result set (using the GROUP BY clause of SQL). The following example installs names and prices to group products:
- Criteria crit = GetSession (). Createcriteria (Product. Class);
- Projectionlist projlist = Projections.projectionlist ();
- Projlist.add (Projections.groupproperty ("name"));
- Projlist.add (Projections.groupproperty ("price"));
- Crit.setprojection (projlist);
- List result = Crit.list ();
Hibernate projections (projection, statistics, non-repeating results)