我有一个名为employee的实体,该实体具有自我连接的能力。加入将代表谁是该员工的经理。当查询记录时,我将获得员工的整个层次结构。
我希望实现的是仅获取经理的几个领域,而不希望获取员工经理的经理。
我尝试使用@NamedEntityGraph
进行操作,但无法获得预期的输出。
也使用了Projections
。
我也尝试了此处提到的方法
注意-我不希望使用@Query
注释
@Entity
public class Employee {
@Id
@Column(name = "employeeId", nullable = false)
private String employeeId;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "lastName", nullable = false)
private String lastName;
@Column(name = "middleName")
private String middleName;
@Column(name = "dateOfJoining", nullable = false)
private LocalDate dateOfJoining;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "line_manager_id")
private Employee manager;
}
实际结果-
{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Admin",
"lastName":"User",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Admin",
"lastName":"User",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager": null,
"employeeId":"P67"
},
"employeeId":"P68"
},
"employeeId":"P69"
},
"employeeId":"P70"
}
预期结果
{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Super",
"lastName":"Manager",
"employeeId":"P69"
},
"employeeId":"P70"
}