how to deserialize one to many relationship with both eager sides without ending up in infinite deserialization loop

我与影子孤独终老i 提交于 2019-12-11 01:09:20

问题


here is my Product class

@Entity
public class Product {

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="listingGroup_id")
    @JsonBackReference
    public ListingGroup listingGroup;

and here is my groupProduct class

@Entity
public class GroupProduct {

    @OneToMany(mappedBy = "listingGroup", fetch = FetchType.EAGER)
    @JsonManagedReference
    Set<Product> products;

GAOL:

  1. When I query for product, I want product with ProductGroup (ProductGroup should not serialize again the products inside)
  2. When I query GroupProduct, I want the products inside (without these list of products each including again the GroupProduct)

ALREADY TRIED

  1. JsonBackReference, JsonManagedReference:

    The GroupProduct get everything fine, but

    Problem : the deserialized products does not contain the group Product : {id: 1, ... groupProduct: null}

  2. JsonIdentityInfo : I am not able anymore to deserialize the objects java.lang.IllegalArgumentException: No converter found for return value of type...

Environment

  • spring boot 1.5.8
  • hibernate 5.0.12
  • 'jackson-annotations', version: '2.8.0'
  • 'jackson-databind', version: '2.8.7'

回答1:


I think you need @JsonIgnoreProperties annotation, like this:

@JsonIgnoreProperties("products")
public ListingGroup listingGroup;

or like this:

@JsonIgnoreProperties("listingGroup")
Set<Product> products;

or both.



来源:https://stackoverflow.com/questions/49530737/how-to-deserialize-one-to-many-relationship-with-both-eager-sides-without-ending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!