2
我遇到了JPA拒絕獲取設置爲獲取延遲的集合的問題。這裏是如何的實體定義:JPA無法加載延遲獲取的集合
報告實體
//bi-directional many-to-one association to Host
@OneToMany(mappedBy="report", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Host> hosts;
主機實體
//bi-directional many-to-one association to Report
@ManyToOne
@JoinColumn(name="INCIDENT_ID")
private Report report;
出於某種原因,我不能讓主機設置加載到內存和主機總是空。我發現了一個計算器帖子下面,但它不工作之一:從上面的
public void loadHosts(Report report)
{
PersistenceUtil unitUtil = getEntityManager().getEntityManagerFactory().getPersistenceUnitUtil();
System.out.println("isLoaded(report, \"hosts\"): " + unitUtil.isLoaded(report, "hosts"));
initializeCollection(report.getHosts());
System.out.println("isLoaded(report, \"hosts\"): " + unitUtil.isLoaded(report, "hosts"));
}
private void initializeCollection(Collection<?> collection)
{
if(collection == null)
{
return;
}
collection.iterator().hasNext();
}
輸出:
SystemOut O isLoaded(report, "hosts"): false
SystemOut O isLoaded(report, "hosts"): false
任何想法?
在這種情況下,我正在從數據庫中讀取現有報告。報告和相關主機使用這些實體寫入數據庫。我已經嘗試調用report.getHosts(),並在上面的代碼中執行該操作:initializeCollection(report.getHosts()); – bnorwood 2012-04-19 20:25:29
對不起,我錯過了那一行。 – 2012-04-19 20:29:57
fetchtype.eager仍然有幫助嗎? – 2012-04-19 20:32:11