Skip to content

Instantly share code, notes, and snippets.

@yannbriancon
Last active May 2, 2020 17:53
Show Gist options
  • Save yannbriancon/66ee53068a24b890cd2603d214e512a4 to your computer and use it in GitHub Desktop.
Save yannbriancon/66ee53068a24b890cd2603d214e512a4 to your computer and use it in GitHub Desktop.
Criteria query with JoinType LEFT
List<Message> getAllBy() {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Message> query = criteriaBuilder.createQuery(Message.class);
Root<Message> message = query.from(Message.class);
// Add fetching of the author field
message.fetch(Message_.author, JoinType.LEFT);
query.select(message);
TypedQuery<Message> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment