加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – org.hibernate.HibernateException:collection与任何

发布时间:2020-12-15 01:25:48 所属栏目:大数据 来源:网络整理
导读:我的一个朋友在开源软件OscarMcmaster中遇到了一个奇怪的问题.他让我帮忙,我能够解决造成问题的代码.以下是一种方法: public BillingService getBillingCodeByCode(String code){ List list = billingServiceDao.findBillingCodesByCode( code,"BC"); if(li

我的一个朋友在开源软件OscarMcmaster中遇到了一个奇怪的问题.他让我帮忙,我能够解决造成问题的代码.以下是一种方法:

 public BillingService getBillingCodeByCode(String code){
    List list = billingServiceDao.findBillingCodesByCode( code,"BC");
    if(list == null || list.size() ==0 ){
        return null;
    }
    return (BillingService) list.get(0);
  }

billingServiceDao由Spring容器初始化:

private static BillingServiceDao billingServiceDao = 
                  (BillingServiceDao) SpringUtils.getBean("billingServiceDao");

在BillingServiceDao类中执行以下代码:

public List

罪魁祸首是query.getResultList();但我来自其他宇宙(.Net)并且不知道问题的补救措施.

请帮我帮朋友解决这个问题.

编辑: – 堆栈跟踪

SEVERE: Servlet.service() for servlet action threw exception
org.hibernate.HibernateException: collection is not associated with any session
    at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
    at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:797)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:241)
    at org.hibernate.loader.Loader.doList(Loader.java:2220)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
    at org.oscarehr.common.dao.BillingServiceDao.findBillingCodesByCode(BillingServiceDao.java:47)
    at org.oscarehr.common.dao.BillingServiceDao$$FastClassByCGLIB$$f613fb7e.invoke(
最佳答案
默认情况下,Hibernate会“懒惰地”填充列表对象,为此,您需要一个打开的会话. Spring正在围绕对DAO的调用打开和关闭Hibernate会话.因此,当您去检查列表时,Hibernate会尝试为您填充它,但它会发现会话已关闭并引发错误.

您需要向web.xml添加OpenSessionInViewFilter(假设您正在编写Web应用程序),将OpenSessionInViewInterceptor添加到spring上下文中,或者在返回之前简单地提取列表内容:

return new ArrayList

另外,这个:

private static BillingServiceDao billingServiceDao = 
              (BillingServiceDao) SpringUtils.getBean("billingServiceDao");

首先完全违背了使用Spring的目的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读