2012-08-09 30 views
0

我有4個實體,JAXB防止JAXB從一個共享實體序列化

purchaseRequest - 資金 - lineItemFunding purchaseRequest - LINEITEM - lineItemFunding - 資金

我使用JAXB和@XmlTransientlineItemFundingManyToOne關係。 >funding - -

當從purchaseRequest來,我不希望它掃描lineItemFunding,但是從purchaseRequest何時到來 - >lineItem - >lineItemFunding - >Funding。我想要它對Funding進行深層掃描。我碰到的問題是,如果我在lineItemFundinggetFunding()裏面使用@XmlTransient,事情就完美了,但是如果我刪除它,會出現以下錯誤。

Caused by: com.sun.istack.SAXException2: 
A cycle is detected in the object graph. 
This will cause infinitely deep XML: 
[email protected] 
-> [email protected] 
-> [email protected] 

所以我的問題是,我如何防止它試圖從資金實體lineItemFunding做深度掃描。以下是我的來源。

PurchaseRequest

@OneToMany(mappedBy = "purchaseRequest", cascade = CascadeType.ALL, orphanRemoval = true) 
private List<LineItem> lineItems; 

@OneToMany(mappedBy = "purchaseRequest", cascade = CascadeType.ALL, orphanRemoval = true) 
private List<Funding> fundings; 

資金

@OneToMany(mappedBy = "funding", cascade = CascadeType.ALL, orphanRemoval = true) 
private List<LineItemFunding> lineItemFundings; 

@XmlTransient 
@ManyToOne 
@JoinColumn(name = "purchase_request_id", nullable = false) 
private PurchaseRequest purchaseRequest; 

LINEITEM

@OneToMany(mappedBy = "lineItem", cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.EAGER) 
private List<LineItemFunding> lineItemFundings; 

@XmlTransient 
@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name = "purchase_request_id", nullable = false) 
private PurchaseRequest purchaseRequest; 

LineItemFunding

@XmlTransient 
@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name = "line_item_id", nullable = true) 
private LineItem lineItem; 

//需要刪除此xmlTransient以從lineItem方向深入掃描資金實體,但在資助方向上將其分開。因爲lineItemFunding只是對lineItem資金的一個連接,所以資金並不需要深入掃描linItemFunding。

@XmlTransient 
@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name = "funding_id", nullable = true) 
private Funding funding; 

感謝

回答

0

我試圖避免使用實體使用其他別的地方那麼持久層,因爲他們可能會揮手,根據所使用的persitence框架我一直有這個問題。 所以我用DTO的。 (這可能是舊的方式,但它解決了這個問題)

不過最近我看到了這所以你的情況可能會有所幫助 http://wiki.eclipse.org/EclipseLink/Examples/SDO/JPA

+0

@克里斯,謝謝克里斯,但在這種情況下,我不相信這會幫助我。然而,我會在下一次看看DTO。我認爲會有一些註釋可以應用於您的方法,以防止對象被進一步掃描。 – 2012-08-09 18:49:01

0

只是解決自己的問題。我只需從lineItemFunding中的@ManyToOne關係中刪除@XmlTransient註釋,並將註釋應用於Funding中的@OneToMany關係即可。