问题描述
我有一个包含循环的对象图.如何让 jaxb 处理这个问题?我尝试在子类中使用 @xmltransient 注释,但 jaxb 编组器仍然检测到循环.
i have an object graph that contains a cycle. how do i get jaxb to handle this? i tried using the @xmltransient annotation in the child class but the jaxb marshaller still detects the cycle.
@entity @xmlrootelement public class contact { @id private long contactid; @onetomany(mappedby = "contact") private listaddresses; ... } @entity @xmlrootelement public class contactaddress { @id private long contactaddressid; @manytoone @joincolumn(name = "contact_id") private contact contact; private string address; ... }
推荐答案
使用 jaxb 的好处是它是一个具有多种实现的标准运行时(就像 jpa 一样).
the good thing about using jaxb is that it is a standard runtime with multiple implementations (just like jpa).
如果您使用 eclipselink jaxb (moxy),那么您可以使用许多扩展来处理 jpa 实体,包括双向关系.这是使用 moxy @xmlinversereference 注释完成的.它的作用类似于 marshal 上的 @xmltransient 并在 unmarshal 上填充目标到源的关系.
if you use eclipselink jaxb (moxy) then you have many extensions available to you for handling jpa entities including bi-directional relationships. this is done using the moxy @xmlinversereference annotation. it acts similar to @xmltransient on the marshal and populates the target-to-source relationship on the unmarshal.
http://wiki.eclipse.org/eclipselink/examples/moxy/jpa/关系
@entity @xmlrootelement public class contact { @id private long contactid; @onetomany(mappedby = "contact") private listaddresses; ... } @entity @xmlrootelement public class contactaddress { @id private long contactaddressid; @manytoone @joincolumn(name = "contact_id") @xmlinversereference(mappedby="addresses") private contact contact; private string address; ... }
其他扩展可用,包括支持复合键和嵌入式键类.
other extensions are available including support for composite keys & embedded key classes.
要指定 ecliselink moxy jaxb 实现,您需要在模型类(即合同)中包含一个 jaxb.properties 文件,其中包含以下条目:
to specify the ecliselink moxy jaxb implementation you need to include a jaxb.properties file in with your model classes (i.e. contract) with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.jaxbcontextfactory