Hibernate Criteria Queries - Hibernate

What is Hibernate Criteria Queries?

Hibernate provides exchange ways of manipulating objects and in rotate data accessible in RDBMS tables. One of the methods is Criteria API which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical situation.

The Hibernate Session interface provides create Criteria () method which can be used to form a Criteria object that returns instances of the determination object's class when your application executes a criteria query.

Following is the simplest instance of a criteria query is one which will basically return every entity that corresponds to the Employee class.

Restrictions with Criteria:

You can use add () method obtainable for Criteria object to add control for a criteria query. Following is the instance to add a limitation to return the records with salary is equal to 2000:

Following are the many more example cover different scenarios and can be used as per condition:

You can form AND or conditions using Logical phrase restrictions as follows:

Still all the above situation can be used directly with HQL as explain in preceding tutorial.

Pagination using Criteria:

There are two methods of the Criteria interface for pagination.

S.N.

Method & Description

1

public Criteria setFirstResult(int firstResult)

This method takes an integer that represents the first row in your result set, starting with row 0.

2

public Criteria setMaxResults(int maxResults)

This method tells Hibernate to retrieve a fixed numbermaxResultsof objects.

By above two methods mutually, we can construct a paging constituent in our web or Swing application. Following is the instance which you can enlarge to fetch 10 rows at a time:

Sorting the Results:

The Criteria API provides the org.hibernate.criterion.Order class to sort your result set in either ascending or descending order, according to one of your substance properties. This instance demonstrates how you would use the Order class to sort the result set:

Projections & Aggregations:

The Criteria API provides the org.hibernate.criterion.Projections class which can be used to get average, maximum or minimum of the belongings values. The Projections class is related to the limitations class in that it provides a number of static manufacturing works methods for obtaining Projection instances.

Following are the few instance covering different scenarios and can be used as per prerequisite:

Criteria Queries Example:

Consider the following POJO class:

Let us create the following EMPLOYEE table to store Employee objects:

Following will be mapping file.

Ultimately, we will construct our application class with the main () method to run the application where we will use Criteria queries:

Compilation and Execution:

Here are the steps to compile and run the above mentioned application. Create sure you have set PATH and CLASSPATH correctly before arranged for the compilation and execution.

  • Create hibernate.cfg.xml configuration file as explained in configuration chapter.
  • Create Employee.hbm.xml mapping file as shown above.
  • Create Employee.java source file as shown above and compile it.
  • Create ManageEmployee.java source file as shown above and compile it.
  • Execute Manage Employee binary to run the program.

You would get follow result, and records would be created in EMPLOYEE table.

If you verify your EMPLOYEE table, it should have following files:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Hibernate Topics