Infinite Recursion with Jackson JSON and Hibernate JPA issue

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

    The point is to place the @JsonIgnore in the setter method as follow. in my case.

    Township.java

    @Access(AccessType.PROPERTY)
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="townshipId", nullable=false ,insertable=false, updatable=false)
    public List<Village> getVillages() {
        return villages;
    }
    
    @JsonIgnore
    @Access(AccessType.PROPERTY)
    public void setVillages(List<Village> villages) {
        this.villages = villages;
    }
    

    Village.java

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "townshipId", insertable=false, updatable=false)
    Township township;
    
    @Column(name = "townshipId", nullable=false)
    Long townshipId;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 07:38

    Also, Jackson 1.6 has support for handling bi-directional references... which seems like what you are looking for (this blog entry also mentions the feature)

    And as of July 2011, there is also "jackson-module-hibernate" which might help in some aspects of dealing with Hibernate objects, although not necessarily this particular one (which does require annotations).

    0 讨论(0)
  • 2020-11-21 07:38

    For some reason, in my case, it wasn't working with Set. I had to change it to List and use @JsonIgnore and @ToString.Exclude to get it working.

    Replace Set with List:

    //before
    @OneToMany(mappedBy="client")
    private Set<address> addressess;
    
    //after
    @OneToMany(mappedBy="client")
    private List<address> addressess;
    

    And add @JsonIgnore and @ToString.Exclude annotations:

    @ManyToOne
    @JoinColumn(name="client_id", nullable = false)
    @JsonIgnore
    @ToString.Exclude
    private Client client;
    
    0 讨论(0)
  • 2020-11-21 07:39

    There's now a Jackson module (for Jackson 2) specifically designed to handle Hibernate lazy initialization problems when serializing.

    https://github.com/FasterXML/jackson-datatype-hibernate

    Just add the dependency (note there are different dependencies for Hibernate 3 and Hibernate 4):

    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-hibernate4</artifactId>
      <version>2.4.0</version>
    </dependency>
    

    and then register the module when intializing Jackson's ObjectMapper:

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    

    Documentation currently isn't great. See the Hibernate4Module code for available options.

    0 讨论(0)
  • 2020-11-21 07:39

    you can use DTO pattern create class TraineeDTO without any anotation hiberbnate and you can use jackson mapper to convert Trainee to TraineeDTO and bingo the error message disapeare :)

    0 讨论(0)
提交回复
热议问题