Infinite loop with spring-boot in a one to many relation

前端 未结 6 1850
梦谈多话
梦谈多话 2021-02-06 09:52

In a rest application, I use spring boot with jpa.

I have a class Lodger

who have

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, m         


        
相关标签:
6条回答
  • 2021-02-06 09:56
    @JsonIgnoreProperties({"hibernateLazyInitializer","referenceList"}) at class Level
    

    For reference see this article on medium.com.

    enter image description here

    0 讨论(0)
  • 2021-02-06 09:58

    If you primary keys in both tables are same name for example : id.

    Add this

    @Entity
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
    public class User {
        ...
    }
    

    And to Reference class.

    @Entity
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
    public class Reference {
        ...
    }
    
    0 讨论(0)
  • 2021-02-06 10:08

    It happens when you have a cycle in return object and spring tries to serialize it to other type.

    Try to create DTO or Value Object (simple POJO) without cycles from returned model and then return it.

    0 讨论(0)
  • 2021-02-06 10:09

    Lets assume your code looks like below :-

    Lodger.class
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "lodger")
    private List<Reference> referenceList;
    
    public List<Reference> getReferenceList() {
        return referenceList;
    }
    
    public void setReferenceList(List<Reference> referenceList) {
        this.referenceList = referenceList;
    }
    
    @Override
    public String toString() {
        return "Lodger[referenceList=" + referenceList + "]";
    }
    

    Reference.class

    @ManyToOne
    @JoinColumn(name = "lodgerId")
    private Lodger lodger;
    
    public Lodger getLodger() {
        return lodger;
    }
    
    public void setLodger(Lodger lodger) {
        this.lodger = lodger;
    }
    
    @Override
    public String toString() {
        return "Reference[lodger=" + lodger + "]";
    }
    

    When you notice at the toString() method written in both the POJO's, you will see that we are calling toString() of both the classes from either side which results in infinite no. of calls to toString() method from both sides which never terminates. To avoid this situation remove any the reference from toString() of Refernce.class[You may remove from Lodger class also.] So toString() of Reference class will not have lodger property in it.

    So finally your Reference class will look like :-

    Reference.class

    @ManyToOne
    @JoinColumn(name = "lodgerId")
    private Lodger lodger;
    
    public Lodger getLodger() {
        return lodger;
    }
    
    public void setLodger(Lodger lodger) {
        this.lodger = lodger;
    }
    
    @Override
    public String toString() {
        return "Reference[Properties other than lodger=" + properties other than lodger + "]";
    }
    
    0 讨论(0)
  • 2021-02-06 10:14

    Do not return entity with circular dependencies via REST webservice - create new DTO class, map entities fetched from database and return it in webservice.

    More info here: http://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application

    Of course if you want you may use another mapping library, my personal favourite is Orika (http://orika-mapper.github.io/orika-docs/intro.html)

    0 讨论(0)
  • 2021-02-06 10:18

    Solution:

    Use

    @JsonManagedReference annotation for the first objects instantiated

    @JsonBackReference annotation for the second objects instantiated

    First:

    @JsonManagedReference
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "lodger")
        private List<Reference> referenceList;
    

    Second:

    @JsonBackReference
    @ManyToOne
        @JoinColumn(name = "lodgerId")
        private Lodger lodger;
    
    0 讨论(0)
提交回复
热议问题