更改 ManyToMany 关联的补丁请求
Patch request for changing ManyToMany association
需要一些 REST api 的帮助,以便使用 spring 数据 rest
实现员工和角色之间的多对多关系
@Entity
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="employees_id_seq")
@SequenceGenerator(name="employees_id_seq", sequenceName="employees_id_seq", allocationSize=1)
@Column(name="EMPLOYEE_ID")
private Long id;
@NotNull
private String email;
@ManyToMany(cascade={}, fetch=FetchType.EAGER, targetEntity=Role.class)
@JoinTable(
name="employee_roles",
joinColumns=@JoinColumn(name="EMPLOYEE_ID", referencedColumnName="EMPLOYEE_ID"),
inverseJoinColumns = @JoinColumn(name="ROLE_ID", referencedColumnName="ROLE_ID")
)
private List<Role> roles;
public Employee() {
System.out.println("Employee created");
}
public Employee(long id, String firstName, String lastName, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
// Getters , Setter, etc.
}
@Entity
@Table(name="roles")
public class Role {
@Id
@Column(name="ROLE_ID")
private String roleId;
private String roleName;
public Role() {
}
// Getters, Setters, etc
}
Spring 数据存储库:
import org.springframework.data.repository.CrudRepository;
public interface EmployeeRepository extends CrudRepository<Employee, Long>{}
从其余 api:
收到的 json 表示的当前数据库状态
{
"firstName": "First1",
"lastName": "Last1",
"email": "first_last@acme.com",
"roles": [ {"roleName": "User"}, {"roleName": "Admin"}],
"locationId": 43,
"_links": {
"self": {
"href": "http://localhost:8091/employees/1"
},
"employee": {
"href": "http://localhost:8091/employees/1"
}
}
}
我在数据库中有以下角色数据:
select role_id, role_name 来自角色
role_id role_name
------- -------
adm Admin
usr User
我想删除用户角色并保留用户的管理员角色。我正在尝试使用具有以下有效负载的 PATCH 请求
{
"roles": [ {"roleId": "adm"}]
}
不幸的是,它导致错误:
org.hibernate.HibernateException:角色实例的标识符已从 usr 更改为 adm
如果我尝试使用负载删除角色 Admin
{
"roles": [ {"roleId": "usr"}]
}
它工作得很好。
似乎行为取决于角色 ID 的顺序。
如有任何帮助或建议,我们将不胜感激。
更改 SDR 中角色集合的正确机制是发送如下 PATCH 请求:
{
"roles": [
"http://localhost:8091/roles/adm",
"http://localhost:8091/roles/usr"
]
}
但是请参阅 Hopper SR5 中引入的以下回归,并将在 Hooper SR9 中修复。
需要一些 REST api 的帮助,以便使用 spring 数据 rest
实现员工和角色之间的多对多关系@Entity
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="employees_id_seq")
@SequenceGenerator(name="employees_id_seq", sequenceName="employees_id_seq", allocationSize=1)
@Column(name="EMPLOYEE_ID")
private Long id;
@NotNull
private String email;
@ManyToMany(cascade={}, fetch=FetchType.EAGER, targetEntity=Role.class)
@JoinTable(
name="employee_roles",
joinColumns=@JoinColumn(name="EMPLOYEE_ID", referencedColumnName="EMPLOYEE_ID"),
inverseJoinColumns = @JoinColumn(name="ROLE_ID", referencedColumnName="ROLE_ID")
)
private List<Role> roles;
public Employee() {
System.out.println("Employee created");
}
public Employee(long id, String firstName, String lastName, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
// Getters , Setter, etc.
}
@Entity
@Table(name="roles")
public class Role {
@Id
@Column(name="ROLE_ID")
private String roleId;
private String roleName;
public Role() {
}
// Getters, Setters, etc
}
Spring 数据存储库:
import org.springframework.data.repository.CrudRepository;
public interface EmployeeRepository extends CrudRepository<Employee, Long>{}
从其余 api:
收到的 json 表示的当前数据库状态{
"firstName": "First1",
"lastName": "Last1",
"email": "first_last@acme.com",
"roles": [ {"roleName": "User"}, {"roleName": "Admin"}],
"locationId": 43,
"_links": {
"self": {
"href": "http://localhost:8091/employees/1"
},
"employee": {
"href": "http://localhost:8091/employees/1"
}
}
}
我在数据库中有以下角色数据:
select role_id, role_name 来自角色
role_id role_name
------- -------
adm Admin
usr User
我想删除用户角色并保留用户的管理员角色。我正在尝试使用具有以下有效负载的 PATCH 请求
{
"roles": [ {"roleId": "adm"}]
}
不幸的是,它导致错误:
org.hibernate.HibernateException:角色实例的标识符已从 usr 更改为 adm
如果我尝试使用负载删除角色 Admin
{
"roles": [ {"roleId": "usr"}]
}
它工作得很好。
似乎行为取决于角色 ID 的顺序。
如有任何帮助或建议,我们将不胜感激。
更改 SDR 中角色集合的正确机制是发送如下 PATCH 请求:
{
"roles": [
"http://localhost:8091/roles/adm",
"http://localhost:8091/roles/usr"
]
}
但是请参阅 Hopper SR5 中引入的以下回归,并将在 Hooper SR9 中修复。