gpt4 book ai didi

java - Spring-JPA - 如何从自连接表输出有限字段

转载 作者:行者123 更新时间:2023-11-30 05:41:34 25 4
gpt4 key购买 nike

我有一个名为员工的实体,它具有 self 连接。该连接代表谁是该员工的经理。当我查询记录时,我得到了员工的整个层次结构。

我希望实现的是仅获取经理的几个字段,并且不希望获取员工的经理的经理。

我尝试使用@NamedEntityGraph执行此操作,但无法获得预期的输出。也使用了投影。

我也尝试了这里提到的方法

  1. JPA Self Join using JoinTable

  2. Self join Spring JPA

注意 - 我不想使用@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"
}

最佳答案

您应该在不想包含的字段中使用@JsonIgnore注释。检查herehere了解更多信息。

关于java - Spring-JPA - 如何从自连接表输出有限字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55532674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com