Hibernate projections (projection, statistics, non-repeating results)

Source: Internet
Author: User

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:

    1. GetSession (). BeginTransaction ();
    2. Criteria crit = GetSession (). Createcriteria (Product.  Class);
    3. Projectionlist projlist = Projections.projectionlist ();
    4. Projlist.add (Projections.max ("price"));
    5. Projlist.add (Projections.min ("price"));
    6. Projlist.add (Projections.avg ("price"));
    7. Projlist.add (projections.countdistinct ("description"));
    8. Crit.setprojection (projlist);
    9. List result = Crit.list ();
    10. 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.

    1. Criteria crit = GetSession (). Createcriteria (Product.  Class);
    2. Projectionlist projlist = Projections.projectionlist ();
    3. Projlist.add (Projections.property ("name"));
    4. Projlist.add (Projections.property ("description"));
    5. Crit.setprojection (projlist);
    6. 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.

    1. GetSession (). BeginTransaction ();
    2. Criteria = GetSession (). Createcriteria (Transaction.  Class);
    3. Projectionlist prolist = Projections.projectionlist ();
    4. Prolist.add (Projections.distinct (Projections.property ("module" ));
    5. Criteria.setprojection (prolist);
    6. Criteria.addorder (ORDER.ASC ("ordersign"));
    7. List = Criteria.list ();
    8. 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:

    1. Criteria crit = GetSession (). Createcriteria (Product.  Class);
    2. Projectionlist projlist = Projections.projectionlist ();
    3. Projlist.add (Projections.groupproperty ("name"));
    4. Projlist.add (Projections.groupproperty ("price"));
    5. Crit.setprojection (projlist);
    6. List result = Crit.list ();

Hibernate projections (projection, statistics, non-repeating results)

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.