
Play Store Application link β Hibernate – in 10 steps – Apps on Google Play
The Criteria API in Hibernate provides a programmatic way to construct queries for retrieving entities from the database. Instead of writing HQL or SQL queries as strings, you can use a fluent API to build queries using Java method calls.
Here are some of the benefits of using the Criteria API:
- Type safety: You can build queries using Java method calls, which are checked at compile time for correctness, rather than constructing HQL or SQL queries as strings, which can be prone to syntax errors.
- Dynamic queries: You can construct queries dynamically based on runtime conditions, using conditional expressions and logical operators.
- Composition: You can reuse and compose Criteria objects to build more complex queries.
- Automatic association fetching: You can specify which associations to fetch eagerly or lazily, and the Criteria API will generate the appropriate SQL joins.
- Pagination: You can specify how many results to retrieve at a time, and use the built-in methods to iterate over the results.
Here are some code examples that demonstrate some of the key features of the Criteria API:
1- Associations:
2- Restrictions:
3- Ordering:
4- Projections:
5- Aggregation:
Github Project link – https://github.com/kuldeep101990/CriteriaQueries
[…] Step 9 β Criteria Queries […]