Java JPA未加载延迟抓取集合的问题

Java JPA未加载延迟抓取集合的问题,java,sql-server-2008,jpa,websphere,Java,Sql Server 2008,Jpa,Websphere,我遇到一个问题,JPA拒绝获取设置为获取惰性的集合。以下是实体的定义方式: 报告实体 //bi-directional many-to-one association to Host @OneToMany(mappedBy="report", cascade=CascadeType.ALL, fetch=FetchType.LAZY) private Set<Host> hosts; 由于某些原因,我无法将主机设置为加载到内存中,并且主机总是空的。我在stackoverflow p

我遇到一个问题,JPA拒绝获取设置为获取惰性的集合。以下是实体的定义方式:

报告实体

//bi-directional many-to-one association to Host
@OneToMany(mappedBy="report", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Host> hosts;
由于某些原因,我无法将主机设置为加载到内存中,并且主机总是空的。我在stackoverflow post上发现以下内容,但它也不起作用:

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();
}

有什么想法吗?

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

在本例中,我正在从数据库中读取现有报告。使用这些实体将报告和相关主机写入数据库。我试着调用report.getHosts,并在上面代码行中这样做:initializeCollectionreport.getHosts;很抱歉,我错过了那一行。fetchtype.eager仍然有用吗?它确实可以像eager一样工作,但有几个页面或数据表不需要访问主机数据。只是试图通过不加载不需要的内容来有效利用内存。在本例中,我正在从数据库中读取一个现有的报告。使用这些实体将报告和相关主机写入数据库。我试着调用report.getHosts,并在上面代码行中这样做:initializeCollectionreport.getHosts;很抱歉,我错过了那一行。fetchtype.eager仍然有用吗?它确实可以像eager一样工作,但有几个页面或数据表不需要访问主机数据。只是试图通过不加载不需要的内容来有效利用内存。
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