Criteria API Tutorial
Introduction to Criteria API
The Criteria API is a powerful feature of Hibernate that allows developers to build queries in a programmatic manner. It provides a type-safe way of querying entities compared to JPQL (Java Persistence Query Language) and is particularly useful for dynamic queries.
Setting Up the Environment
To use the Criteria API, you need to have Hibernate set up in your project. Ensure that you have the necessary dependencies in your project. For Maven, add the following dependencies:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.32.Final</version> </dependency>
Creating a Basic Criteria Query
To create a basic Criteria query, you need to obtain a session from the session factory and then create a CriteriaBuilder instance. Here’s how to create a simple query to retrieve all entities of a certain type:
Session session = sessionFactory.openSession(); CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); CriteriaQuery<EntityName> criteriaQuery = criteriaBuilder.createQuery(EntityName.class); Root<EntityName> root = criteriaQuery.from(EntityName.class); criteriaQuery.select(root); List<EntityName> results = session.createQuery(criteriaQuery).getResultList();
Adding Conditions with Criteria API
You can add conditions to your Criteria queries using predicates. Predicates can be combined using AND, OR, and NOT operations. Here’s an example:
Predicate condition = criteriaBuilder.equal(root.get("attributeName"), "value"); criteriaQuery.where(condition); List<EntityName> filteredResults = session.createQuery(criteriaQuery).getResultList();
Sorting Results
Sorting can be achieved easily with the Criteria API. You can use the orderBy
method to specify your sorting criteria. Here’s how you can sort the results:
criteriaQuery.orderBy(criteriaBuilder.asc(root.get("attributeName"))); List<EntityName> sortedResults = session.createQuery(criteriaQuery).getResultList();
Using Joins
The Criteria API also supports joins between entities. Here’s how you can perform an inner join:
Join<EntityName1, EntityName2> join = root.join("relationshipName"); criteriaQuery.select(root).where(criteriaBuilder.equal(join.get("attributeName"), "value")); List<EntityName1> joinedResults = session.createQuery(criteriaQuery).getResultList();
Criteria API with Subqueries
You can also create subqueries with the Criteria API. Here’s an example of how to use a subquery:
Subquery<String> subquery = criteriaQuery.subquery(String.class); Root<EntityName2> subRoot = subquery.from(EntityName2.class); subquery.select(subRoot.get("attributeName")).where(criteriaBuilder.equal(subRoot.get("someAttribute"), "value")); criteriaQuery.where(criteriaBuilder.in(root.get("attributeName")).value(subquery)); List<EntityName1> finalResults = session.createQuery(criteriaQuery).getResultList();
Conclusion
The Criteria API provides a flexible and type-safe way to create dynamic queries in Hibernate. With its ability to programmatically build queries, add conditions, sort results, and handle relationships, it becomes a powerful tool for developers working with databases. By following the examples provided in this tutorial, you should have a solid foundation for using the Criteria API in your Hibernate applications.