学生 - 课程:OneToMany
JPQL:
从 Student student, Course course where student.name=:STUDENTNAME and (course.courseName=:COURSENAME or course.courseDuration=courseDuration) 中选择 Student
让我们假设一个学生可能有 10 门课程,我只想检索两条包含 Student - Courses(2) 的记录。
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
Root<Student> studentRoot = criteriaQuery.from(Student.class);
studentRoot.fetch("courses", JoinType.LEFT);
Predicate condition = criteriaBuilder.equal(studentRoot.get("studentName"), "someName");
//how to add course condition here.
criteriaQuery.where(condition);