Ask Your Question
1

What is the correct way to convert the JPQL "join fetch" with a "where" clause into a JPA 2 CriteriaQuery?

asked 2023-06-17 15:20:44 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-17 15:28:02 +0000

plato gravatar image

To convert the JPQL "join fetch" with a "where" clause into a JPA 2 CriteriaQuery, you can follow the below steps:

  1. Create a CriteriaBuilder instance:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  1. Create a CriteriaQuery instance with the desired result type:
CriteriaQuery<Order> query = cb.createQuery(Order.class);
  1. Specify the root entity for the query:
Root<Order> orderRoot = query.from(Order.class);
  1. Add a join fetch to the query using the root entity:
orderRoot.fetch("customer", JoinType.INNER);
  1. Add the where clause to the query using the CriteriaBuilder:
Predicate predicate = cb.equal(orderRoot.get("status"), "PROCESSING");
query.where(predicate);
  1. Execute the query using your entity manager:
List<Order> orders = entityManager.createQuery(query).getResultList();
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-17 15:20:44 +0000

Seen: 8 times

Last updated: Jun 17 '23