Why criteria in hibernate




















Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. If you think the Criteria representation of the above would be as complex, then have a look at the following:. The only thing to be done is to call the setFetchMode of the Criteria class with two parameters: the name of the class with which the current class has to be joined and mode of the fetching of the data from the associated class.

In the above case, the class name is actually the instance variable provided within the Order class. The mode is Join. So retrieval is done, but there is just one problem. If the data has to be retrieved based on a condition, then what? Then Restriction has to be used. To retrieve data based on certain conditions, Restriction must be used.

Here the Restriction class comes into the picture. All the conditions provided by SQL are available in Criteria. The ones most commonly used are as follows:. Applying the restrictions becomes easy in the case of joins as well. For example, the following query. Through restriction conditions can be imposed on data retrieval, there are situations where the data to be retrieved has to be based on the groups of values of a column.

In such conditions, Aggregation must be used. Criteria provides aggregation functionality through the Projection class itself. Similarly all the aggregate functions can be used as they are provided as static functions. As shown in the above example, each function takes the field name as the argument.

When the aggregation functions are used, the values may have to be grouped according to a particular field. When we add more than one constraint to a criteria query, it is interpreted as an AND, like so:. If we want to have two restrictions that return objects that satisfy either or both of the restrictions, we need to use the or method on the Restrictions class, as follows:. The orExp logical expression that we have created here will be treated like any other criterion. We can therefore add another restriction to the criteria:.

Disjunction object to represent a disjunction. You can obtain this object from the disjunction factory method on the Restrictions class. The disjunction is more convenient than building a tree of OR expressions in code. To represent an AND expression with more than two criteria, you can use the conjunction method, although you can easily just add those to the Criteria object. The conjunction can be more convenient than building a tree of AND expressions in code.

Here is an example that uses the disjunction:. One common application pattern that criteria can address is pagination through the result set of a database query. There are two methods on the Criteria interface for paging, just as there are for Query: setFirstResult and setMaxResults.

The setFirstResult method takes an integer that represents the first row in your result set, starting with row 0. You can tell Hibernate to retrieve a fixed number of objects with the setMaxResults method. Using both of these together, we can construct a paging component in our web or Swing application. As you can see, this makes paging through the result set easy. You can increase the first result you return for example, from 1, to 21, to 41, etc.

Sometimes you know you are going to return only zero or one object from a given query. This could be because you are calculating an aggregate or because your restrictions naturally lead to a unique result. If you want obtain a single Object reference instead of a List, the uniqueResult method on the Criteria object returns an object or null. If there is more than one result, the uniqueResult method throws a HibernateException. The following short example demonstrates having a result set that would have included more than one result, except that it was limited with the setMaxResults method:.

Again, please note that you need to make sure that your query returns only one or zero results if you use the uniqueResult method. If you would like to work with distinct results from a criteria query, Hibernate provides a result transformer for distinct entities, org.

The Criteria API provides the org. You may add more than one Order object to the Criteria object. Hibernate will pass them through to the underlying SQL query. Your results will be sorted by the first order, then any identical matches within the first sort will be sorted by the second order, and so on. The association works when going from either one-to-many or from many-to-one.

Going the other way, we obtain all the products from the supplier MegaInc using many-to-one associations:. Instead of working with objects from the result set, you can treat the results from the result set as a set of rows and columns, also known as a projection of the data.

To use projections, start by getting the org. Projection object you need from the org. It is the way to show in the object-oriented representation a query result set of a Criteria query. There are built-in projections in the Projections factory class. This interface might be implemented to define custom projections. Hibernate Projection is used to get the particular columns from the database table. The Projection class produces projection objects.

This class contains just static methods and they return the projection interface object. Projection usually is used for the Criteria query object. The Projections class contains some in-built functions like sum, max, min, rowCount, etc, therefore you can perform the aggregation operations in Hibernate.

You can add a projection object to the criteria object by calling the criteria. If you need to read multiple columns from the database, then you need to pass the ProjectionList object to the setProjection method: criteria. For our example we will use UserEntity class. This is very useful when you use an aggregate function.

It is valuable to mention that in the new version of Hibernate 5 all createCriteria methods are marked as deprecated. They have all functionality that you had with the combination of Criteria and Projection combination. This is because there is no sense to keep two different APIs, that have the pretty much same functionality.

Your email address will not be published.



0コメント

  • 1000 / 1000