Spring Data - QueryDSL InnerJoin谓词

时间:2015-10-10 11:32:09

标签: spring spring-data spring-data-jpa querydsl

让我们假设我们有以下域对象(部分完成以减少代码)。

public class Student {
    @OneToMany(mappedBy="student")
    List<Assignment> assignments;
}


public class Assignment {

    @ManyToOne
    Student student;

    @OneToOne
    Implementation implementation;
}

public class Implementation {

    @OneToOne
    Assignment assignment;

    @OneToMany(mappedBy="implementation")
    List<Assessment> assessments;
}

public class Assessment {

    @ManyToOne
    Implementation implementation;

    String grade;
}

所以我想要执行的查询是"Select all students whose assignment implementation has been performed (not null) and has not been assessed at all (List<Assessment>#isEmpty()

所以我使用QueryDSL并尝试使用以下查询来获取未实现作业的学生

    public class MyService {

        @Autowired
        private StudentRepository studentRepository;

        public Iterable<Student> foo() {
            return studentRepository.findAll(
                QStudent.student.assignments.any().implementation.isNotNull()
            );
        } 
    }

并且上面的查询似乎被忽略了。

然后我使用以下内容获取组合(implemented exercises and empty assessments

studentRepository.findAll(
    QStudent.student.assignments.any().implementation.isNotNull() 
      .and(QStudent.student.assignments.any().implementation.assessments.isEmpty())
);

这再次没有区别。知道我做错了什么

1 个答案:

答案 0 :(得分:0)

你试过这个:

QAssignment assignment = QAssignment.assignment;
ListSubQuery<Assignment> subQuery = new JPASubQuery().from(assignment)
    .where(assignment.implementation.isNotNull()))
    .and(assignment.implementation.assessments.isEmpty())
    .list(assignment);

studentRepository.findAll(
    QStudent.student.assignments.contains(subQuery)
);

我不确定为什么你的代码不能正常工作,但我想这是因为 any()只是一个子查询存在的快捷方式。 见https://stackoverflow.com/a/25453708/2672352