【问题标题】:Multiple back-reference properties with name 'defaultReference'多个名为“defaultReference”的反向引用属性
【发布时间】:2017-08-29 09:31:51
【问题描述】:

我在一个类中有多个反向引用类。因为我为他们使用@JsonBackReference,所以我得到一个错误。我为这些类分配了@JsonIdentityInfo 注释,但我仍然得到同样的错误。

public class X implements Serializable {
  ....
  //bi-directional many-to-one association to Booking
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "xxA", nullable = false)
  @JsonBackReference
  private A a;

  //bi-directional many-to-one association to Client
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "xxB", nullable = false)
  @JsonBackReference
  private B b;
  ...getters setters
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class B implements Serializable {
  ........ 
  //bi-directional many-to-one association to BookedClient
  @OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JsonManagedReference
  private List < X > xxB;
  ........ getters setters
}


@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class A implements Serializable {
  ........
  //bi-directional many-to-one association to BookedClient
  @OneToMany(mappedBy = "a", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JsonManagedReference
  private List < X > xxA;
  ........ getters setters
}

错误:

com.fasterxml.jackson.databind.JsonMappingException:多个名为“defaultReference”的反向引用属性

如何解决此错误?我不能在一个类中使用多个反向引用吗?

【问题讨论】:

    标签: java json spring jackson


    【解决方案1】:

    根据Jackson's javadoc@JsonManagedReference@JsonBackReference 都接受将它们绑定在一起的名称值:

      @JsonBackReference("a")
      private A a;
    
      @JsonManagedReference("a")
      private List < X > xxA;
    

    【讨论】:

    • 我为每个关系添加了一个名称值。我仍然遇到同样的错误。
    • 是的,我也试过@JsonManagedReference(value="a")@JsonBackReference(value="a"),还是一样的错误
    • 我收到此错误:java.lang.IllegalArgumentException: Can not handle managed/back reference 'a': back reference type (java.util.List) not compatible with managed type (A)
    • @Eniss 您必须使用唯一名称命名您的后退/托管引用的所有,因为它们可能与项目中的其他引用冲突。只需给每一对@JasonBackReference ~ @JasoManagedReference 取一个独特、合理的名字。你解决了吗?
    【解决方案2】:

    我也遇到过这个问题,但最后我解决了。

    //This is parent class
    @Entity
    @Table(name = "checklist")
    @JsonIgnoreProperties("inspection")
    public class Checklist implements java.io.Serializable {
    
        @ManyToOne
        @JoinColumn(name = "product_id", referencedColumnName = "id")
        @JsonBackReference
        private Product product;
    
        @OneToMany(mappedBy = "checklists", cascade = CascadeType.ALL)
        @JsonManagedReference
        private Set<Inspection> inspection = new HashSet<Inspection>();
    //Constructor
    //Getter and Setter
    }
    
    //This is child class
    @Entity
    @Table(name = "inspections")
    public class Inspection {
    
        @ManyToOne
        @JoinColumn(name = "chk_id", referencedColumnName = "id")
        private Checklist checklists;
    //Constructor
    //Getter and Setter
    }
    

    通过在 Parent 类中提及 @JsonIgnoreProperties("inspection")@JsonManagedReference

    解决了在同一父类中使用两个@JSONBackRefrence 引发的问题。

    【讨论】:

      【解决方案3】:

      所以这确实花了我一段时间......

      您可以使用 @JsonIdentityReference(alwaysAsId = true) 相应地注释您的参考文献,然后将其从主参考文献中删除

          @JsonIdentityReference(alwaysAsId = true)
          private Set<PackInstructionGroup> groups = new TreeSet<>();
      

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2020-08-04
        • 2013-12-05
        • 1970-01-01
        • 2016-09-18
        • 2018-10-08
        • 1970-01-01
        • 2017-05-09
        • 2017-11-13
        相关资源
        最近更新 更多