问题描述
我的数据模型中有很多关系。我一直在尝试使用JAX = B来获取数据模型的XML表示,但是我已经读过一对多的关系:
I have a few many to many relationships in my data model. I have been trying to use JAX=B to get XML representations of the data models, however I have read that for one to many relationships an:
@XmlInverseReference
映射的反面需要
。我相信这是为了使用不同的提取类型(即LAZY和EAGER)。我不确定这个注释是如何工作的。它是否使用后向指针来确保在某些字段上指定数据时不会获取数据?我也不知道是否需要用上述注释来注释我的多对多关系。
is needed for the inverse side of the mapping. I believe this is for using different fetch types (ie. LAZY and EAGER). I am unsure of exactly how this annotation works. Does it use back pointers to ensure that data is not fetched when it is specified on certain fields? I also do not know if I need to annotate my many to many relationship with the above annotation or not.
这是与自身有多对多关系的用户类,即。用户可以是许多其他用户的朋友。我应该用@XmlInverseReference注释getter吗?
Here is the user class that has a many to many relationship with itself, ie. a user can be friends with many other users. Should I annotate the getter with an @XmlInverseReference?
@Entity
@Table(name = "users")
public class User implements Serializable {
...
// bi-directional many-to-many association to User
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH })
@JoinTable(name = "friends", joinColumns = { @JoinColumn(name = "uid") }, inverseJoinColumns = { @JoinColumn(name = "frienduId") })
private List<User> friends;
/**
* @return
*
* gets the list of users this user is friends with
*/
public List<User> getFriends() {
return this.friends;
}
/**
* @param friendsList
*
* sets the users friends list
*/
public void setFriends(List<User> friendsList) {
this.friends = friendsList;
}
非常感谢任何帮助或指导。
Any help or guidance is much appreciated.
推荐答案
注意:我是领导和专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
@ XmlInverseReference
是一个EclipseLink JAXB(MOXy)扩展,可以映射双向关系:
@XmlInverseReference
is a EclipseLink JAXB (MOXy) extension that enables you to map bidirectional relationships:
- http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
- http://blog.bdoughan.com/2013/03/moxys-xmlinversereference-is-now-truly.html
@X mlInverseReference
有两个角色:
- 在编组期间,它可以防止发生无限循环。如果
Foo
和Bar
之间存在双向关系,它将编组Foo
然后Bar
然后它会在尝试再次编组Foo
之前停止。 - 在解组时,它会填充后指针。
- During marshalling it prevents an infinite loop from occurring. If a bidirectional relationship exists between
Foo
andBar
, it will marshalFoo
thenBar
and then it will stop before trying to marshalFoo
again. - During unmarshalling it will populate the back pointer.
这篇关于jax-b xml具有多对多关系的反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!