本文介绍了Spring Data JPA @OneToOne注释无限递归错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Husband.java

package com.example.demo.com.example.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;


//@Data
//@NoArgsConstructor
//@EqualsAndHashCode
//@ToString
@Entity
@Table(name = "t_husban")
public class Husband {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    private String job;

    @OneToOne
    @JoinColumn(name = "wife_fk",referencedColumnName = "id")
    private Wife wife;

    //omitted getter/setter
}

Wife.java

package com.example.demo.com.example.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;

//@Data
//@NoArgsConstructor
@EqualsAndHashCode(exclude = "husband",callSuper = false)
@Entity
@Table(name = "t_wife")
public class Wife {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToOne(mappedBy = "wife",cascade = {CascadeType.ALL})
    private Husband husband;

    //omitted getter/setter
}

Service.java

@Service

    public class TestOneToOneEitherSide {

        @Autowired
        private WifeRepository wifeDao;

        @Autowired
        private HusbandRepository husbandDao;
        public Husband testCreate() {

            Husband husband = husbandDao.findByName("Wang");
            return husband;
        }
    }

当我使用spring数据jpa从数据库中查询丈夫时,结果发生无限递归,请参见下图.使用@OneToOne注释时出了什么问题?有人可以给我一些建议吗?否则我会以错误的方式使用注释.

When I query husband from database using spring data jpa,it occurs nfinite recursion in the result,seeing the follow picture.What's something wrong while using @OneToOne annotation?Could anybody give me some advice? Or I use the annotation in wrong way.

图片

推荐答案

这是一个已知问题,当您具有双向关系时,杰克逊将尝试从另一侧序列化一侧的每个引用,以便其逻辑上具有无限递归.

This is a known issue, when you have bidirectional relation jackson will try to serialize each reference of one side from the other side so its logical to have infinite recursion.

解决方案:有很多解决方案,可以在一侧使用@JsonIgnore以避免序列化带注释的引用,从而破坏了无限递归

Solution: There are many solutions to that , you could use @JsonIgnore on one side to avoid serializing the annotated reference hence breaking the infinite recursion

 @EqualsAndHashCode(exclude = "husband",callSuper = false)
 @Entity
@Table(name = "t_wife")
public class Wife {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

@OneToOne(mappedBy = "wife",cascade = {CascadeType.ALL})
@JsonIgnore
private Husband husband;

//omitted getter/setter
}

您还可以使用@ JsonManagedReference/@ JsonBackReference,选中此链接有关如何使用它们的更多信息

you also could use @JsonManagedReference/@JsonBackReference, check this link for more info how to use them

这个答案有一个问题,如果您尝试序列化妻子的方向,您将没有丈夫对象,因为解决方案是避免序列化它.

This answer has one problem , if you try to serialize wife direction you will not have husband object since the solution was to avoid serializing it.

对此有一个很好的解决方案,在链接中提到,其想法是生成对父实体的引用,因此,如果您要序列化丈夫,您将拥有丈夫->妻子-> [对丈夫而不是丈夫的引用],您要做的就是注释您的实体@JsonIdentityInfo

There is a nice solution to this, its mentioned in this link , the idea is to generate a reference to the parent entity, so if you are serializing husband, you will have husband->wife->[reference to husband instead of husband], all you need to do is to annotate your entities with @JsonIdentityInfo

@EqualsAndHashCode(exclude = "husband",callSuper = false)
@Entity
@Table(name = "t_wife")
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")
public class Wife {

@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")
@Entity
@Table(name = "t_husban")
public class Husband {
@Id

这篇关于Spring Data JPA @OneToOne注释无限递归错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:27