hibernate@onetomany set在api端点中接收时失去其顺序

azpvetkf  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(292)

我在从react前端接收javaapi端点中的有序数据时遇到问题。
特别是一个json数组,它以树结构的形式包含递归对象和数组,如下面的示例所示
问题:如何接收由集subchildren中的hibernateMap的子子级数组,并按正确的顺序排列?我可能丢失了一些hibernate注解或其他东西
如果我进入调试模式,我会发现在输入savepeoplebydynastyid()方法的第一行代码之后,peoplelist中的set subchildren就失去了is order。
(我想为person对象实现的正确顺序:child1、child2、child3)
这是唯一的方法发送他们与身份证已经(我宁愿避免它)?如果是,我应该在 @OneToMany private Set<Person> subChildren 为了达到这个目的?我试过了 @OrderBy(value = "id") 但它不起作用
感谢您的帮助!
使用postman发送的json数据模拟post请求:http://localhost:8080/api/王朝/人

[
     {
      name: father,
      age: 50,
      subChildren : [
        {
            name: child1,
            age: 30,
            subChildren : []
        },
        {
            name: child2,
            age: 28,
            subChildren : []
        },
        {
            name: child3,
            age: 26,
            subChildren : []
        },
      ]
    }
  ]

个人.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;
    }
}
ttvkxqim

ttvkxqim1#

我认为问题在于Jackson所做的反序列化。它用一个简单的 HashSet 而不是添加到现有的 LinkedHashSet . 尝试使用 LinkedHashSet 类型作为实体中声明的类型。

相关问题