与同一实体的Spring JPA多重关系 [英] Spring JPA Multiple Relations with same entity

查看:156
本文介绍了与同一实体的Spring JPA多重关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下关系:

我试图在Spring Boot中使用JPA映射这些关系,但是遇到了一些麻烦.这些是类:

I'm trying to map those relations using JPA in Spring Boot, but I'm having some trouble. These are the classes:

人员:

public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // Relations

    @OneToOne
    @JoinColumn(name = "child_id")
    private PersonBirth birth;

    @OneToMany
    @JoinColumn(name = "mother_id")
    private List<PersonBirth> motherOf;

    @OneToMany
    @JoinColumn(name = "father_id")
    private List<PersonBirth> fatherOf;
}

人员出生:

public class Birth {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "birth_date")
    @JsonProperty(value = "birth_date")
    private Long birthDate;

    // Relations

    @OneToOne
    @JoinColumn(name = "child_id")
    private Person child;

    @ManyToOne
    @JoinColumn(name = "mother_id")
    private Person mother;

    @ManyToOne
    @JoinColumn(name = "father_id")
    private Person father;
}

我试图获得一个Pearson和他的Birth数据,包括他的motherfather.而且还能够获得一个Person及其子级由fatherOfmotherOf映射的子级.但是现在的方式是,当我获取母亲的Person时,它会引发stackoverflow,因为它获取的Birth数据包含了child数据(到目前为止,我想要的是),该数据包含了他的母亲. Birth数据,其中包含他的母亲,其中包含她的孩子(依此类推).我不知道使用这种结构是否可以做我想做的事情,或者我是否必须更改它...任何建议都将受到赞赏.

I'm trying to be able to get a Pearson and his Birth data including his mother and father. And also be able so get a Person with its children mapped by fatherOf or motherOf. But the way it is right now, it throws stackoverflow when I fetch a Person who is a mother, because it gets the Birth data, that contains the child data (so far what I want), that contains his Birth data, that contains his mother, that contains her children (and so on). I don't know if what I'm trying to do is possible using this structure or if I'll have to change it... Any suggestions are appreciated.

推荐答案

只需添加@dunni回答的内容:

Just to add what @dunni answered:

为了使Jackson正常工作,不应将关系的两个方面之一进行序列化,以避免引起堆栈溢出错误的烦人的无限递归循环.

In order to allow Jackson works well, one of the two sides of the relationship should not be serialized, in order to avoid the annoying infinite recursive loop that causes stackoverflow error.

Jackson采用了引用的前一部分,例如java类的属性(即User类中的List角色),并将其转换为类似json的存储格式;这就是所谓的编组过程. 然后,Jackson寻找参考的后半部分(即在Role类中列出用户),并保持原样,而不是对其进行序列化.关系的这一部分将在前向引用的反序列化(解组)期间重新构建.

Jackson takes the forward part of the reference, for example an attribute of a java class (i.e. List roles in User class), and converts it in a json-like storage format; this is the so-called marshalling process. Then, Jackson looks for the back part of the reference (i.e. List users in Role class) and leaves it as it is, not serializing it. This part of the relationship will be re-constructed during the deserialization (unmarshalling) of the forward reference.

参考: http://keenformatics.blogspot.com/2013 /08/how-to-solve-json-infinite-recursion.html

这篇关于与同一实体的Spring JPA多重关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆