@Id 未映射到 Spring-Boot 控制器中的特定实体
@Id Not Mapped For Specific Entity In A Spring-Boot Controller
我有两个实体 Employee
和 Department
,每个实体都有一个 Spring Web @RestController
注释 class 更新方法,即 Http PUT
.
出于某种奇怪的原因(并且可能是一个非常明显的解决方案)每当为 Employee
class 调用 PUT
时,JSON 有效负载中的 ID 是未映射到 Employee
实体的 id
class,但它非常适合 Department
实体。
Employee
class:
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
class:
@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;
}
}
调用时PUT /employees/{id}
:
正在调用 PUT /departments/{id}
:
正如您在调试器的屏幕截图中看到的那样,Department
的 id
字段被填充,而它在 Employee
中为空。我正在使用 Swagger 对此进行测试,并在有效负载中设置 ID。我没有任何特定的 Jackson 配置集,我只是使用 Spring 启动的默认设置,但我无法弄清楚为什么只在 Employee 中从未映射 id
字段。
Employee
正文:
{
"id":1,
"name": "New Name"
}
Department
正文:
{
"id":2,
"name": "chemistry",
"deleted":false
}
问题是由于另一个实体 Project
上的 Jackson 注释 @JsonIdentityInfo
造成的,该实体与 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;
我有两个实体 Employee
和 Department
,每个实体都有一个 Spring Web @RestController
注释 class 更新方法,即 Http PUT
.
出于某种奇怪的原因(并且可能是一个非常明显的解决方案)每当为 Employee
class 调用 PUT
时,JSON 有效负载中的 ID 是未映射到 Employee
实体的 id
class,但它非常适合 Department
实体。
Employee
class:
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
class:
@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;
}
}
调用时PUT /employees/{id}
:
正在调用 PUT /departments/{id}
:
正如您在调试器的屏幕截图中看到的那样,Department
的 id
字段被填充,而它在 Employee
中为空。我正在使用 Swagger 对此进行测试,并在有效负载中设置 ID。我没有任何特定的 Jackson 配置集,我只是使用 Spring 启动的默认设置,但我无法弄清楚为什么只在 Employee 中从未映射 id
字段。
Employee
正文:
{
"id":1,
"name": "New Name"
}
Department
正文:
{
"id":2,
"name": "chemistry",
"deleted":false
}
问题是由于另一个实体 Project
上的 Jackson 注释 @JsonIdentityInfo
造成的,该实体与 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;