繁体   English   中英

JPA无法加载延迟获取的集合的问题

[英]Issue with JPA not loading lazy fetched collection

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;

由于某些原因,我无法将主机设置为加载到内存,并且主机始终为空。 我在stackoverflow帖子中找到了以下内容,但它也不起作用:

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

有任何想法吗?

我认为您必须在session.get(object)和tx.commit之间调用getHosts。 或者,您也可以决定使用fetch = FetchType.EAGER(尽管那可能不是一个好主意)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM