我有两个实体Employee
和Department
,每个实体都有一个Spring Web @RestController
注释的类,带有更新方法,即Http PUT
。
由于某些奇怪的原因(可能是一个显而易见的解决方案),每当为PUT
类调用Employee
时,JSON有效负载中的ID均未映射到id
类Employee
实体,但它对于Department
实体来说效果很好。
Employee
类:
Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable, Identity<Long>, Deleted
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "NAME")
private String name;
@ManyToOne
@Where(clause = "is_deleted = false")
private Department department;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "EMP_QUAL", joinColumns = @JoinColumn(name = "EMPLOYEE_ID"), inverseJoinColumns = @JoinColumn(name = "QUALIFICATION_ID"))
@WhereJoinTable(clause = "IS_DELETED = false")
@SQLDelete(sql = "UPDATE `EMP_QUAL` SET IS_DELETED = true where EMPLOYEE_ID = ? and QUALIFICATION_ID = ? and IS_DELETED = False")
private Set<Qualification> qualifications;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "EMP_PROJ", joinColumns = @JoinColumn(name = "emp_id"), inverseJoinColumns = @JoinColumn(name = "proj_id"))
@Where(clause = "is_deleted = false")
private Set<Project> projects;
@JsonIgnore
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false;
@Override
public Long getId()
{
return this.id;
}
@Override
public void setId(final Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public Set<Project> getProjects()
{
return projects;
}
public void setProjects(final Set<Project> projects)
{
this.projects = projects;
}
public Department getDepartment()
{
return department;
}
public void setDepartment(final Department department)
{
this.department = department;
}
public Set<Qualification> getQualifications()
{
return qualifications;
}
public void setQualifications(final Set<Qualification> qualifications)
{
this.qualifications = qualifications;
}
public Boolean isDeleted()
{
return isDeleted;
}
public void setDeleted(final Boolean deleted)
{
isDeleted = deleted;
}
}
Department
类:
@Entity
@Table(name = "DEPARTMENT")
public class Department implements Serializable, Identity<Long>, Deleted
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.EAGER)
@WhereJoinTable(clause = "is_deleted = false")
@JoinTable(name = "DEPARTMENT_EMPLOYEE", joinColumns = {@JoinColumn(name = "department_id")},
inverseJoinColumns = {@JoinColumn(name = "employee_id")})
@SQLDelete(sql = "UPDATE DEPARTMENT_EMPLOYEE SET is_deleted = true where department_id = ? and employee_id = ? and is_deleted = false")
private Set<Employee> departmentMembers;
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted;
@Override
public Long getId()
{
return this.id;
}
@Override
public void setId(final Long id)
{
this.id = id;
}
@Override
public Boolean isDeleted()
{
return this.isDeleted;
}
@Override
public void setDeleted(final Boolean isDeleted)
{
this.isDeleted = isDeleted;
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public Set<Employee> getDepartmentMembers()
{
return departmentMembers;
}
public void setDepartmentMembers(final Set<Employee> departmentMembers)
{
this.departmentMembers = departmentMembers;
}
}
正如您在调试器的屏幕快照中看到的那样,填充了id
的{{1}}字段,而在Department
中则为空。我正在使用Swagger进行测试,并在有效负载中设置了ID。我没有任何特定的Jackson配置设置,我只使用Spring boot的默认设置,但是我无法弄清楚为什么仅在Employee中Employee
字段从未映射过。
id
正文:
Employee
{
"id":1,
"name": "New Name"
}
正文:
Department
答案 0 :(得分:0)
该问题是由于与另一个实体@JsonIdentityInfo
有关系的另一个实体Project
上的Jackson注释Employee
:
@Entity
@Table(name = "PROJECT")
public class Project implements Serializable, Identity<Long>, Deleted
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "EMP_PROJ", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "emp_id"))
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
@Where(clause = "is_deleted = false")
private Set<Employee> employees;