spring-boot - findAll() 上的 @NamedEntityGraphs
问题描述
我在我的应用程序中使用 @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 无法解析 mehtod 名称。
有没有可能得到这样的方法?
谢谢
马蒂亚斯
解决方案
添加@Query
到将选择所有Employee
s 的方法中:
@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
也应该有效
推荐阅读
- arrays - 如何在 matlab 中使用 datetime 数组创建 for - if 循环?
- html - 实现动画边框底部的正确方法是什么?
- prolog - 计算二叉树序言中的出现次数
- linux - 在mac的终端中获取chrome版本
- java - 如何解决这个问题:maven & intelliJ
- azure - IoT Edge 模块之一处于退避状态 Raspberry Pi 4 with Raspbian OS
- javascript - 为什么我的 Javascript 无法使用 Tag Game firebase
- c# - C# .NET Core 3.1 Entity Framework:包括使用 where 条件给出错误输出
- javascript - 无法获取 ajax 表单数据以使用 nodemailer 提交
- vue.js - 如果有人尝试在本地存储数据中编辑它不会反映在我的 UI 中,如何将购物车数据存储在本地存储和应用程序中