CriteriaBuilder集合值

时间:2019-05-03 08:02:04

标签: java jpa collections predicate criteriaquery

我有一种情况,我必须获取所有没有学生(设置为空)的所有大师的名单,以及状态被取消的学生的名单

@Entity    
@Table(name="Masters")   
public class Master {    

    @Id    
    private String id;

    @One To Many mapping here
    private Set<Student> students = new HashSet<>();
}

@Entity   
@Table(name="Students")   
public class Student {

    @Id
    private String id;

    @Column(name = "Status")
    private String status;
}

I have my code as 

final CriteriaBuilder cb = getEM().getCriteriaBuilder();    
final CriteriaQuery<Tuple> criteriaQuery = cb.createTupleQuery();    
final Root<Master> mvRoot = criteriaQuery.from(Master.class);
Predicate predicate = cb.conjunction();    
this gives me list of masters who dont have any students

predicate = cb.and(predicate, cb.isEmpty(mvRoot.join(Master.STUDENTS)));     

这给出了已取消学生身份的硕士名单

final Join<Master, Set<Student>> studentJoin = mvRoot.join(Master.STUDENTS);    
predicate = cb.and(predicate, cb.equal(studentJoin.get(Student.status), "Cancelled"));    

我无法一次获取两者,并且如果我都加入俱乐部,我将获得取消学生身份的大师,而不是获得没有学生身份的大师

有人可以告诉我吗?

0 个答案:

没有答案