@NamedEntityGraphs 在 findAll() 上

@NamedEntityGraphs on findAll()

我在我的应用程序中使用@NamedEntityGraph

@NamedEntityGraphs({
    @NamedEntityGraph(name = "graph.Employee.assignments", attributeNodes = @NamedAttributeNode("assignmentProjectEmployeeSet")),
    @NamedEntityGraph(name = "graph.Employee.absences", attributeNodes = @NamedAttributeNode("absences"))
})enter code here`public class Employee {

当我通过

在存储库上使用它时
@EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAll();

我得到了预期的所有结果。但是我需要针对不同情况的多个版本的 findAll() 。像

@EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAssignments();

@EntityGraph(value = "graph.Employee.absences", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAbsences();

但是如果我尝试在存储库中定义新方法名称,我会收到应用程序上下文错误,因为 Spring 无法解析方法名称。

是否有可能得到这样的方法?

谢谢

马蒂亚斯

@Query 添加到您的方法中,这将 select 所有 Employees:

@Query("select e from Employee e")
@EntityGraph(value = "graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAssignments();

@Query("select e from Employee e")
@EntityGraph(value = "graph.Employee.absences", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAbsences();

或者使用方法名称 findAllWithAssignmentsBy findAllWithAbsencesBy 也应该有效