我有一个包含迴圈的物件圖.我如何让JAXB處理呢? 我尝試使用
@XmlTransient
子類中的註解,但JAXB編組器仍檢測到週期。
@Entity
@XmlRootElement
public class Contact {
@Id
private Long contactId;
@OneToMany(mappedBy = "contact")
private List<ContactAddress> addresses;
...
}
@Entity
@XmlRootElement
public class ContactAddress {
@Id
private Long contactAddressId;
@ManyToOne
@JoinColumn(name = "contact_id")
private Contact contact;
private String address;
...
}
最新回復
- 5月前1 #
- 5月前2 #
"非正式JAXB指南"中的此頁面提供了三種處理迴圈的策略.它们是(摘要):
Mark one of the reference attributes that form the cycle as @XmlTransient.
Use @XmlID and @XmlIDREF so that the references are represented using XML ids arather than by containment.
Use the CycleRecoverable interface to deal with cycles programmatically.
XMLTransient几乎总是可以迴圈使用.您可能在欄位級別具有XMLTransient,但未將XMLAccessorType指定為XmlAccessType.Field.如果未指定任何內容,則預設值為XmlAccessType.Property-或您的吸气剂.我经歷過Jaxb从我錯過了訪問器型別註釋的類中的吸气剂中選擇xml元素,但仍然可以正常工作。
- 5月前3 #
仅看一下本教程:jaxb將迴圈引用對映到XML
我使用它並且效果很好:)
- 5月前4 #
我们也可以使用XStream庫,我在一个專案中尝試過它,其中JAXB给出了迴圈錯誤,但是XStream成功地處理了它
相似問題
- java:Hibernate / JPA-註釋bean方法与欄位javahibernateormjpa2021-01-11 11:28
- java:Spring-Data-Jpa儲存庫-實體列名稱下划線javajpaspringdatarepositoryspringdatajpa2021-01-11 07:57
- java:註釋以過濾@OneToMany關聯的結果javahibernatejpajointable2021-01-11 07:26
使用JAXB的好處是它是具有多種實現的標準執行時(就像JPA)。
如果使用EclipseLink JAXB(MOXy),則可以使用许多擴充套件来處理JPA實體,包括双向關係.這是使用MOXy @XmlInverseReference批註完成的.它的作用類似於元帅上的@XmlTransient,並在元帅上填充目標与源的關係。
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA/Relationships
還可以使用其他擴充套件,包括對複合键和嵌入式键類的支援。
要指定EcliseLink MOXy JAXB實現,您需要在模型類(即Contract)中包含jaxb.properties檔案,並帶有以下條目: