What You Can Do With JPQL: Select Where Group by Having Order
What You Can Do With JPQL: Select Where Group by Having Order
List<BookPublisherValue> bookPublisherValues =
em.createQuery(“SELECT new
org.thoughts.on.java.model.BookPublisherValue(b.title,
b.publisher.name) FROM Book b”,BookPublisherValue.class)
.getResultList();
em.createQuery(
“SELECT b.title, b.publisher.name FROM Book b”)
.getResultList();
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
or you define an explicit join in the FROM clause:
em.createQuery(
“SELECT b.title, p.name FROM Book b JOIN
b.publisher p”)
.getResultList();
em.createQuery(
“SELECT p.firstName, p.lastName, n.phoneNumber
FROM Person p JOIN PhoneBookEntry n ON p.firstName =
n.firstName AND p.lastName = n.lastName“)
.getResultList();
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
4. Use conditional expressions in the WHERE and HAVING clause
JPQL supports a standard set of conditional expressions in the
WHERE and HAVING clauses. You can use them to limit the result set
to all Authors with an id equal to the given bind parameter value.
Query q = em.createQuery(
“SELECT a FROM Author a WHERE a.id = :id”);
Query q = em.createQuery(
“SELECT a FROM Author a WHERE a.id IN (SELECT
s.authorId FROM SpecialAuthors s)”);
em.createQuery(
“SELECT a, count(b) FROM Author a JOIN a.books b
GROUP BY a”)
.getResultList();
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
7. Order the query results with ORDER BY
ORDER BY is another JPQL clause that you know from SQL. You can
use it to order the result of a query, and you should, of course, use it
instead of ordering the result set in your Java code.
em.createQuery(
“SELECT a FROM Author a ORDER BY a.lastName”)
.getResultList();
em.createQuery(
“SELECT a, count(b) FROM Author a JOIN a.books b
GROUP BY a”)
.getResultList();
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
10. Use non-standard and database specific functions
SQL supports more functions than JPQL and in addition to that, most
databases provide a huge set of proprietary functions. Hibernate’s
database-specific dialects offer proprietary support for some of these
functions and since JPA 2.1 you can call all functions supported by
your database with a call of the function function.
em.createQuery(
“SELECT a FROM Author a WHERE a.id =
function(‘calculate’, 1, 2)“, Author.class)
.getSingleResult();
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
What you can’t do with JPQL
As you’ve seen, JPQL supports a set of features that allows you to
create queries up to a certain complexity. In my experience, these
queries are good enough for most use cases. But if you want to
implement reporting queries or have to rely on database-specific
features, you will miss a lot of advanced SQL features. Here are a few
of them that I miss on a regular basis.
www.thoughts-on-java.org
Is your query too complex for JPA and Hibernate?
5. Use window functions
If you don’t know about window functions in SQL, you have to watch
one of Lukas Eder’s SQL talks or read some of his posts on the jOOQ
blog. As soon as you understand this nifty SQL feature, you can do
amazing things, like running total calculations or analyzing data
series, with a relatively simple SQL statement.
www.thoughts-on-java.org