Infinite Recursion with Jackson JSON and Hibernate JPA issue

前端 未结 25 3214
你的背包
你的背包 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:38

    Also, using Jackson 2.0+ you can use @JsonIdentityInfo. This worked much better for my hibernate classes than @JsonBackReference and @JsonManagedReference, which had problems for me and did not solve the issue. Just add something like:

    @Entity
    @Table(name = "ta_trainee", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@traineeId")
    public class Trainee extends BusinessObject {
    
    @Entity
    @Table(name = "ta_bodystat", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@bodyStatId")
    public class BodyStat extends BusinessObject {
    

    and it should work.

提交回复
热议问题