Hibernate @OneToMany Set 在 API 端点收到时丢失他的订单
Hibernate @OneToMany Set lose his order when received in API endpoint
我无法从 React 前端接收 java API 端点中的有序数据。
特别是它是一个 JSON 数组,其中包含内部递归对象和树结构数组,如下面的示例所示
问题:如何以正确的顺序接收由 Hibernate 在 Set subChildren 中映射的 subChildren 数组?我可能缺少一些休眠注释或其他东西
如果我进入调试模式,我发现在进入方法 savePeopleByDynastyId() 的第一行代码后,peopleList 中的 Set subChildren 丢失了顺序。
(我想为 Person 对象实现的正确顺序:child1、child2、child3)
这是发送带有 ID 的唯一方法吗(我宁愿避免它)?如果是,我应该在 @OneToMany private Set<Person> subChildren
中添加什么注释来实现?我试过 @OrderBy(value = "id")
但没用
感谢任何帮助!
JSON 使用 Postman 模拟 POST 请求发送的数据:
http://localhost:8080/api/dynasty/people
[
{
name: father,
age: 50,
subChildren : [
{
name: child1,
age: 30,
subChildren : []
},
{
name: child2,
age: 28,
subChildren : []
},
{
name: child3,
age: 26,
subChildren : []
},
]
}
]
Person.java:
@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private Integer age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="ID_PARENT")
@JsonIgnore
private Person parent;
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER)
@OrderBy(value = "id")
private Set<Person> subChildren = new LinkedHashSet<Person>();
public Person(){}
//Getter and Setter
}
DynastyController.java:
@RestController
@CrossOrigin
@RequestMapping("/api/dynasty")
public class DynastyController {
@PostMapping("/people")
public ResponseEntity<?> savePeopleByDynastyId(@RequestBody Iterable<Person> peopleList, BindingResult result){
Iterable<Person> newPeopleList = null;
for (Person person : peopleList) {
//intention : cycle through each person of subChildren array recursively and persist each of his person
}
ResponseEntity<Iterable<Person>> responseEntity= new ResponseEntity<>(newPeopleList, HttpStatus.CREATED);
return responseEntity;
}
}
我认为问题出在杰克逊所做的反序列化中。它用普通的 HashSet
替换集合,而不是添加到现有的 LinkedHashSet
。尝试使用 LinkedHashSet
类型作为实体中声明的类型。
我无法从 React 前端接收 java API 端点中的有序数据。
特别是它是一个 JSON 数组,其中包含内部递归对象和树结构数组,如下面的示例所示
问题:如何以正确的顺序接收由 Hibernate 在 Set subChildren 中映射的 subChildren 数组?我可能缺少一些休眠注释或其他东西
如果我进入调试模式,我发现在进入方法 savePeopleByDynastyId() 的第一行代码后,peopleList 中的 Set subChildren 丢失了顺序。
(我想为 Person 对象实现的正确顺序:child1、child2、child3)
这是发送带有 ID 的唯一方法吗(我宁愿避免它)?如果是,我应该在 @OneToMany private Set<Person> subChildren
中添加什么注释来实现?我试过 @OrderBy(value = "id")
但没用
感谢任何帮助!
JSON 使用 Postman 模拟 POST 请求发送的数据: http://localhost:8080/api/dynasty/people
[
{
name: father,
age: 50,
subChildren : [
{
name: child1,
age: 30,
subChildren : []
},
{
name: child2,
age: 28,
subChildren : []
},
{
name: child3,
age: 26,
subChildren : []
},
]
}
]
Person.java:
@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private Integer age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="ID_PARENT")
@JsonIgnore
private Person parent;
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER)
@OrderBy(value = "id")
private Set<Person> subChildren = new LinkedHashSet<Person>();
public Person(){}
//Getter and Setter
}
DynastyController.java:
@RestController
@CrossOrigin
@RequestMapping("/api/dynasty")
public class DynastyController {
@PostMapping("/people")
public ResponseEntity<?> savePeopleByDynastyId(@RequestBody Iterable<Person> peopleList, BindingResult result){
Iterable<Person> newPeopleList = null;
for (Person person : peopleList) {
//intention : cycle through each person of subChildren array recursively and persist each of his person
}
ResponseEntity<Iterable<Person>> responseEntity= new ResponseEntity<>(newPeopleList, HttpStatus.CREATED);
return responseEntity;
}
}
我认为问题出在杰克逊所做的反序列化中。它用普通的 HashSet
替换集合,而不是添加到现有的 LinkedHashSet
。尝试使用 LinkedHashSet
类型作为实体中声明的类型。