Infinite Recursion with Jackson JSON and Hibernate JPA issue

前端 未结 25 3190
你的背包
你的背包 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 getVillages() {
        return villages;
    }
    
    @JsonIgnore
    @Access(AccessType.PROPERTY)
    public void setVillages(List 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;
    

提交回复
热议问题