Java JsonManagedReference 与 JsonBackReference
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31319358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JsonManagedReference vs JsonBackReference
提问by ozgur
I would like to know the difference between @JsonManagedReference
and @JsonBackReference
in Hymanson?
我想知道Hyman逊@JsonManagedReference
和@JsonBackReference
Hyman逊的区别?
采纳答案by david99world
@JsonManagedReference is the forward part of reference – the one that gets serialized normally. @JsonBackReference is the back part of reference – it will be omitted from serialization.
@JsonManagedReference 是引用的前向部分——正常序列化的部分。@JsonBackReference 是引用的后面部分——它将从序列化中省略。
So they really depend on the direction of your relationship
所以他们真的取决于你们关系的方向
public class User {
public int id;
public String name;
@JsonBackReference
public List<Item> userItems;
}
public class Item {
public int id;
public String itemName;
@JsonManagedReference
public User owner;
}
回答by ozgur
@JsonManagedReference
and @JsonBackReference
are designed to handle this two-way linkage between fields, one for Parent role, the other for Child role.
@JsonManagedReference
并且@JsonBackReference
旨在处理字段之间的这种双向链接,一个用于父角色,另一个用于子角色。
For avoiding the problem, linkage is handled such that the property annotated with @JsonManagedReference annotation is handled normally (serialized normally, no special handling for deserialization) and the property annotated with @JsonBackReference annotation is not serialized; and during deserialization, its value is set to instance that has the "managed" (forward) link.
为避免该问题,对链接进行处理,使得使用@JsonManagedReference 注解的属性正常处理(正常序列化,反序列化没有特殊处理),而不序列化@JsonBackReference 注解的属性;并且在反序列化期间,它的值被设置为具有“托管”(前向)链接的实例。
回答by HopeKing
- @JsonManagedReference -> Manages the forward part of the reference and the fields marked by this annotation are the ones that get Serialised
- @JsonBackReference -> Manages the reverse part of the reference and the fields/collections marked with this annotation are not serialised.
- @JsonManagedReference -> 管理引用的前向部分,这个注解标记的字段是被序列化的
- @JsonBackReference -> 管理引用的反向部分,并且使用此注释标记的字段/集合不会被序列化。
Use case:You have a one-many or many-many relationships in your entities/tables and not using the above would lead to errors like
用例:您的实体/表中有一对多或多对多的关系,不使用上述方法会导致错误,例如
Infinite Recursion and hence stackoverflow - > Could not write content: Infinite recursion (StackOverflowError)
The above errors occurs because Hymanson (or someother similiar) tries to serialise both ends of the relationship and ends up in a recursion.
发生上述错误是因为 Hymanson(或其他类似的人)试图序列化关系的两端并以递归结束。
@JsonIgnore performs similiar functions but the above mentioned annotations are preferable.
@JsonIgnore 执行类似的功能,但上述注释更可取。
回答by Rajat
I prefer@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
where property is the name of primary key field and scope is Type of it
我更喜欢@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
属性是主键字段的名称,范围是它的类型
回答by Andrew Sneck
As write Rajat Verma, his solution works perfectly. Thanks man you saved me lot of time and anger :-)
正如 Rajat Verma 所写,他的解决方案非常有效。谢谢你为我节省了很多时间和愤怒:-)
The important Part:
You need define fields asList
, I had that asSet
before and this solution NOT WORKING (appears as infinite loop)!
重要部分:
您需要将字段定义为List
,我和Set
以前一样,这个解决方案不起作用(显示为无限循环)!
I add my solution:
我添加我的解决方案:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Agent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(mappedBy = "subscribers")
@ApiModelProperty(dataType = "List", example = "[1,2,3]") // for Swagger
@JsonIdentityReference(alwaysAsId = true) // show only id of Topic
private final List<Topic> subscribeTopics = new ArrayList<>()
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "topic_agent",
joinColumns = @JoinColumn(name = "fk_topic_id"),
inverseJoinColumns = @JoinColumn(name = "fk_agent_id"))
@ApiModelProperty(dataType = "List", example = "[1,2,3]")
@JsonIdentityReference(alwaysAsId = true)
private final List<Agent> subscribers = new ArrayList<>();
}