[英]Spring JPA Exposing/Mapping @JsonIgnore fields from an associated Entity using custom query @Query
I have 2 entities (Department, Employee) with a Bi-Directional One-To-Many relationship.我有 2 个具有双向一对多关系的实体(部门、员工)。 1 Department has MANY Employee.
1个部门有很多员工。
Department
owns the Employee
Department
拥有Employee
I annotated department
field with @JsonIgnore
so that Department
information is not serialized when I GET localhost:8080/employee
我用
@JsonIgnore
注释了department
字段,以便在我GET localhost:8080/employee
时不会序列化Department
信息
I created a custom query to expose all information of Employee
including which Department
the employee belongs.我创建了一个自定义查询来公开
Employee
的所有信息,包括员工所属的Department
。
Department Model部门Model
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String departmentName;
@OneToMany(mappedBy = "department")
private Set<Employee> employees;
...
}
Employee Model员工Model
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String middleName;
private String lastName;
@ManyToOne
@JsonIgnore
private Department department;
...
}
Repository存储库
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query(value = "SELECT e.*, d.department_name FROM Employee e INNER JOIN Department d ON e.department_id = d.id WHERE e.id = ?1", nativeQuery = true)
Employee findEmployeeByIdWithDepartment(Long employeeId);
}
The query works fine in H2-Console SQL Editor.该查询在 H2-Console SQL 编辑器中运行良好。
DEPARTMENT_ID
, DEPARTMENT_NAME
information is being included if I do a GET localhost:8080/employee/1
如果我执行
GET localhost:8080/employee/1
,则会包含DEPARTMENT_ID
, DEPARTMENT_NAME
信息
However, in Postman, I only get Employee
information (id,firstname,lastname,middlename) only.但是,在 Postman 中,我只获取
Employee
信息(id、firstname、lastname、middlename)。 The Department
where the Employee
belongs is NOT being mapped/serialized (because I used @JsonIgnored
in Employee
class). Employee
所属的Department
没有被映射/序列化(因为我在Employee
类中使用@JsonIgnored
)。
{
"id": 1,
"firstName": "John",
"middleName": "Doe",
"lastName": "Johnny"
}
If I change the return type of findEmployeeByIdWithDepartment
to Object
, I can get Employee
information along with the Department
where employee belongs.如果我将
Object
findEmployeeByIdWithDepartment
我可以获得Employee
信息以及员工所属的Department
。
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query(value = "SELECT e.*, d.department_name FROM Employee e INNER JOIN Department d ON e.department_id = d.id WHERE e.id = ?1", nativeQuery = true)
Object findEmployeeByIdWithDepartment(Long employeeId);
}
RESULT: (Not the desired JSON format of nesting Department
info)结果:(不是嵌套
Department
信息的所需 JSON 格式)
[
1,
"John",
"Johnny",
"Doe",
1,
"Accounting"
]
I am not sure how to be able to display the Department
information along with Employee
information if I do a GET localhost:8080/employee/1
AND still be able to do a GET localhost:8080/department
without issue.如果我执行
GET localhost:8080/employee/1
并且仍然能够毫无问题地执行GET localhost:8080/department
,我不确定如何能够显示Department
信息和Employee
信息。
Without @JsonIgnore
on department
field IN Employee
class, the Json response will result in StackOverflow or Endless nested serialization issue.如果
department
字段 IN Employee
class 中没有@JsonIgnore
,则 Json 响应将导致 StackOverflow 或 Endless 嵌套序列化问题。
Thank you.谢谢你。
You can write getters for the Depertment-properties in Employee, something like您可以为 Employee 中的 Depertment-properties 编写 getter,例如
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String middleName;
private String lastName;
@ManyToOne
@JsonIgnore
private Department department;
@JsonProperty("departmentId")
public Long getDepartmentId()
{
return department.getId;
}
@JsonProperty("departmentName")
public String getDepartmentName()
{
return department.getName();
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.