Chapter 14. Creating and executing queries
In this chapter
- The basic query APIs
- Creating and preparing queries
- Optimizing query execution
If you’ve been using handwritten SQL for a number of years, you may be concerned that ORM will take away some of the expressiveness and flexibility you’re used to. This isn’t the case with Hibernate and Java Persistence.
With Hibernate’s and Java Persistence’s powerful query facilities, you can express almost everything you commonly (or even uncommonly) need to express in SQL, but in object-oriented terms—using classes and properties of classes. Moreover, you can always fall back to SQL strings and let Hibernate do the heavy lifting of handling the query result. For additional SQL resources, consult our reference section.
Major new features in JPA 2
- A type-safe criteria API for the programmatic creation of queries is now available.
- You can now declare up front the type of a query result with the new Typed-Query interface.
- You can programmatically save a Query (JPQL, criteria, or native SQL) for later use as a named query.
- In addition to being able to set query parameters, hints, maximum results, and flush and lock modes, JPA 2 extends the Query API with various getter methods for obtaining the current settings.
- JPA now standardizes several query hints (timeout, cache usage).