有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JAXB阻止JAXB与共享实体序列化

我有4个实体

purchaseRequest-资金-lineItemFunding purchaseRequest-lineItem-lineItemFunding-funding

ManyToOne关系上,我正在lineItemFunding中使用JAXB和@XmlTransient

当来自purchaseRequest->funding-我不想让它扫描lineItemFunding,但当它从purchaseRequest来时->lineItem->lineItemFunding->Funding。我想让它对^{进行深度扫描。我遇到的问题是,如果我在lineItemFundinggetFunding()内部使用@XmlTransient,事情会很好地工作,但是如果我删除它,我会得到以下错误

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进行深度扫描。以下是我的消息来源

采购请求

@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;

行项目

@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;

项目融资

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

//需要删除此xmlTransient,以便从行项目方向深入扫描资金实体,但在资金方向将其打断。资金不需要深入扫描linItemFunding,因为lineItemFunding只是资金与lineItem的连接

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

谢谢


共 (2) 个答案

  1. # 1 楼答案

    刚刚解决了我自己的问题。我只需要从lineItemFunding中的@ManyToOne关系中删除@XmlTransient注释,并将注释应用于Funding中的@OneToMany关系

  2. # 2 楼答案

    我尽量避免使用在持久层之外的其他地方使用的实体,因为它们可能会被挥动,而且根据使用的persistence框架,我在这方面总是有问题。 所以我用了DTO(这可能是旧时尚,但它解决了问题)

    不过最近我看到了这一点,所以对你来说可能会有所帮助 http://wiki.eclipse.org/EclipseLink/Examples/SDO/JPA