我的一个朋友在开源软件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 Listcreatequery("select bs from....");
query.setParameter("code",code + "%");
query.setParameter("region",region);
@SuppressWarnings("unchecked")
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(cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
最佳答案
默认情况下,Hibernate会“懒惰地”填充列表对象,为此,您需要一个打开的会话. Spring正在围绕对DAO的调用打开和关闭Hibernate会话.因此,当您去检查列表时,Hibernate会尝试为您填充它,但它会发现会话已关闭并引发错误.
您需要向web.xml添加OpenSessionInViewFilter(假设您正在编写Web应用程序),将OpenSessionInViewInterceptor添加到spring上下文中,或者在返回之前简单地提取列表内容:
return new ArrayList
另外,这个:
private static BillingServiceDao billingServiceDao =
(BillingServiceDao) SpringUtils.getBean("billingServiceDao");
首先完全违背了使用Spring的目的.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。