1

I have a scenario where I have to get the list of all masters who have no students (set is empty) and also students whose status is cancelled

@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)));     

This is giving the list of masters with student status cancelled

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

I am not able to fetch both at a time, and if I club both i am just getting masters with students status cancelled and not the masters with no students

Can some one please advise me.

EzLo
  • 13,780
  • 10
  • 33
  • 38

0 Answers0