The Criteria API provides a type-safe way to define queries programmatically. Similar to JPQL, you can use it to select a set of entity attributes and map each result set record to a POJO.
The Criteria API supports the same feature set as JPQL, and I show in my new book Hibernate Tips – More than 70 solutions to common Hibernate problems how you can use it to implement a variety of use cases. It’s a Hibernate cookbook with more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries. You can get it this week on Amazon at a special launch price of just $2.99.
Let’s take a look at one of the recipes included in the book. It uses the Criteria API to programmatically define a query that selects a set of database columns and returns them as POJOs. To get the most out of this post, you should already be familiar with Hibernate’s basic concepts.
Continue reading %How to Select POJOs with a CriteriaQuery%