Infinite Recursion with Jackson JSON and Hibernate JPA issue

前端 未结 25 3167
你的背包
你的背包 2020-11-21 07:31

When trying to convert a JPA object that has a bi-directional association into JSON, I keep getting

org.codehaus.jackson.map.JsonMappingException: Infinite          


        
25条回答
  •  情话喂你
    2020-11-21 07:43

    You can use @JsonIgnore, but this will ignore the json data which can be accessed because of the Foreign Key relationship. Therefore if you reqiure the foreign key data (most of the time we require), then @JsonIgnore will not help you. In such situation please follow the below solution.

    you are getting Infinite recursion, because of the BodyStat class again referring the Trainee object

    BodyStat

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name="trainee_fk")
    private Trainee trainee;
    

    Trainee

    @OneToMany(mappedBy = "trainee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Column(nullable = true)
    private Set bodyStats;
    

    Therefore, you have to comment/omit the above part in Trainee

提交回复
热议问题