JPA criteria查询,相比使用HQL或者JPQL,类型安全,更加的面向对象。本文只关注使用,不包含概念等需要深入、比较晦涩的内容。
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<BoxGraphAnalysis> criteriaQuery = criteriaBuilder.createQuery(BoxGraphAnalysis.class);
Root<BoxGraphAnalysis> root = criteriaQuery.from(BoxGraphAnalysis.class);
criteriaQuery.select(root);
Predicate predicate = criteriaBuilder.conjunction();
CriteriaQuery<School> criteriaQuery = criteriaBuilder.createQuery(School.class);
可创建Root,查询的开始
predicate = criteriaBuilder.and(predicate, root.get("subjectId").isNull());
predicate = criteriaBuilder.or(predicate, root.get("subjectId").isNull());
predicate = criteriaBuilder.and(predicate, root.get("code").in(codes));
predicate = criteriaBuilder.and(predicate, root.get("code").in(codes).not());
Predicate predicate = criteriaBuilder.conjunction();