2012-01-05 44 views
5

我的一位朋友在開源軟件OscarMcmaster中遇到了一個奇怪的問題。他讓我幫忙,並且能夠解決導致問題的代碼。下面是一個方法:org.hibernate.HibernateException:集合與任何會話都沒有關聯

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

billingServiceDaoSpring容器初始化:

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

BillingServiceDao執行類以下代碼:

public List<BillingService> findBillingCodesByCode(String code, String region) { 
    Query query = entityManager.createQuery("select bs from...."); 
    query.setParameter("code", code + "%"); 
    query.setParameter("region", region); 

    @SuppressWarnings("unchecked") 
    List<BillingService> list = query.getResultList(); 
    return list; 
} 

罪魁禍首是query.getResultList();但我m來自其他宇宙(.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(<generated>) 
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) 
+0

此鏈接可能會幫助你:http://stackoverflow.com/questions/8292820/org-hibernate-lazyinitialization-exception – 2012-01-05 19:00:23

回答

4

默認情況下,Hibernate的填充對象列表 「懶洋洋地」,要做到這一點,你需要一個開放的會議。 Spring正在打開和關閉對DAO調用的Hibernate會話。所以當你去檢查列表時,Hibernate會嘗試爲你填充它,但它發現會話被關閉並拋出錯誤。

您需要將添加的OpenSessionInViewFilter到web.xml(假設你正在編寫一個Web應用程序),一個的OpenSessionInViewInterceptor添加到Spring上下文,或返回之前簡單的提取列表的內容:

return new ArrayList<BillingService>(list); 

另外,如下所示:

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

完全失敗了首先使用Spring的目的。

+0

謝謝親愛的我會測試這個(提取列表之前發送回來)。但第二件事('(BillingServiceDao)SpringUtils.getBean(「billingServiceDao」); ')超出了我:D – TheVillageIdiot 2012-01-06 06:16:38

+0

nope,仍然得到相同的異常。我也在問題中添加了堆棧跟蹤。 – TheVillageIdiot 2012-01-08 06:14:25

+1

然後,您沒有在DAO級別打開會話。您需要實現OpenSessionInView模式(請參閱上面的Nandkumar鏈接)。 – bernerbrau 2012-01-11 16:15:44

0

我看到這個問題,因爲我沒有在@Transactional服務中註釋一個方法。看起來Hibernate會在調用另一個方法時關閉會話(即使在同一個類中),除非調用者被正確註釋。

相關問題