我有4个实体,
purchaseRequest - 资金 - lineItemFunding purchaseRequest - lineItem - lineItemFunding - funding
我在@XmlTransient
关系lineItemFunding
中使用JAXB和ManyToOne
。
来自purchaseRequest
- > funding
- 我不希望它扫描lineItemFunding
,但是来自purchaseRequest
- > lineItem
- > lineItemFunding
- > Funding
。我希望它对Funding
进行深度扫描。我遇到的问题是,如果我在@XmlTransient
内的getFunding()
上使用lineItemFunding
,事情就会完美无缺,但如果删除它,我会收到以下错误。
Caused by: com.sun.istack.SAXException2:
A cycle is detected in the object graph.
This will cause infinitely deep XML:
org.company.com.entities.Funding@2a2
-> org.company.com.entities.LineItemFunding@82
-> org.company.com.entities.Funding@2a2
所以我的问题是,如何阻止它尝试从资金实体对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方向深度扫描资金实体,但在资金方向上打破它。资金不需要深度扫描linItemFunding,因为lineItemFunding只是对lineItem的资金连接。
@XmlTransient
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "funding_id", nullable = true)
private Funding funding;
由于
答案 0 :(得分:0)
我试图避免使用其他地方使用的实体然后使用持久层,因为它们可能会被挥手,并且根据使用的persitence框架,我总是遇到问题。 所以我使用了DTO。 (它可能是旧时尚,但它解决了问题)
但是最近我看到了这一点,所以在你的情况下它可能会有所帮助 http://wiki.eclipse.org/EclipseLink/Examples/SDO/JPA
答案 1 :(得分:0)
刚刚解决了我自己的问题。除了从lineItemFunding中的@ManyToOne关系中删除@XmlTransient注释之外,我只需要将注释应用于资金中的@OneToMany关系。