JPA高級動態查詢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  @Override
public List<String> findAllByUserAndStatusAndDepartmentIn(User user, String key, Department department) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> query = criteriaBuilder.createQuery(String.class);
Root<User> root = query.from(User.class);
query.select(root.get("id"));
Join<User,Department> join = root.join("departments", JoinType.LEFT);
query.orderBy(criteriaBuilder.desc(root.get("createdDate")));
Predicate restrictions = criteriaBuilder.conjunction();
restrictions = criteriaBuilder.and(join.get("id").in(department.getId()));
query.where(restrictions);
List<String> result = entityManager.createQuery(query).getResultList();
return result;
}
# java
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×