我有3张桌子:
School
school_id, school_name, student_id
Student
student_id, student_name ,subject_id
Subject
subject_id, subject_name
[学校->学生->学科之间存在一对多的关系,而我仅在该关系中创建了实体。
我正在编写specification
查询以获取特定学校的学生:
List<Student> students = schoolRepo.findAll(Specification.where (findStudents(schoolId));
public static Specification<Student> findStudents(long schoolId) {
return (root, query, cbuilder) -> {
root.fetch("students", JoinType.LEFT);
return cbuilder.and(cbuilder.equal(root.get("school"), schoolId));
};
}
尽管我没有在规格查询或代码中的任何地方获取主题(例如,使用get
)。
我仍然可以在生成的查询的日志中看到最终最终获取了主题。
如何更新查询以将查询限制为仅限学校和学生?
请提出建议。
您可以尝试改用EntityGraph。它有助于提高加载实体的性能。您可以这样做[]
@Entity
@NamedEntityGraph (
name="school-students",
attributeNodes = {
@NamedAttributeNode("students")
}
)
public class School implements Serializable { ... }
And then in your repository use it like this :
@Override
@EntityGraph(value = "school-students",
type = EntityGraph.EntityGraphType.FETCH)
@Query( value = "SELECT s FROM School s where s.id =:id")
School findOne();