JPA select with two conditions

Help me please add two conditions for JPA.

I have the entity Subject (some subject of a teacher – math, biology, programming and so one). Subject has a field: boolean deleted.

public class Subject {
    private Long id;
    private String name;
    private boolean deleted;

I have the second entity Teacher. It has Set<Subject> subjects – subjects which are attached to the teacher.

@PrimaryKeyJoinColumn(name = "person_id")
public class Teacher extends Person {
    private boolean deleted;
    Set<Subject> linkedSubjects;

So I need to get list of subjects which are not yet attached to the teacher AND these subjects should not be deleted.

I can do this separately – get not attached or get not deleted. But how get both of them?

    public List<Subject> findSubjectNotInList(List<Subject> teacherSubjects) { // teacherSubjects - List subjects which still attached to a teacher
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Subject> query = cb.createQuery(Subject.class);
        Root<Subject> root = query.from(Subject.class);
     // Get all not attached:
     // query.select(root).where(root.in(teacherSubjects).not());           

     // Get all not deleted         

        List<Subject> subjects = em.createQuery(query).getResultList(); 
        return subjects;

Источник: https://ru.stackoverflow.com/questions/1265089/jpa-select-with-two-conditions

Тебе может это понравится...

Добавить комментарий