多个反向引用属性,名称为“defaultReference” [英] Multiple back-reference properties with name 'defaultReference'

查看:583
本文介绍了多个反向引用属性,名称为“defaultReference”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have multiple back-reference classes in a class. Since I use @JsonBackReference for them, I get an error. I assigned @JsonIdentityInfo annotation for those classes, but I still get the same error.

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'的多个反向引用属性

com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'

如何解决此错误?我可以不在课堂上使用多个反向引用吗?

How can I resolve this error? Can I not use multiple back-reference in a class?

推荐答案

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

According to Jackson's javadoc, both @JsonManagedReference and @JsonBackReference accept a name value that binds them together:

  @JsonBackReference("a")
  private A a;

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

这篇关于多个反向引用属性,名称为“defaultReference”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆