2012-06-07 33 views
0

我調用一個服務來加載一個實體。如果我在集成測試中運行它,這一切都很好,但在服務器上的現實生活中,它會失敗。jpa lazyloading在生產模式下失敗 - 在testmode中工作正常

此實體中的一些屬性是集合。該實體在兩種情況下都加載了bean。當我在「query.getSingleResult()」附近的斷點處停止debugmode中的執行時,我看到集合已加載。但只有在一個testrun ...

我停止在servletcontainer中的相同的代碼,並得到一個InvocationException!?

我的環境look's像:
春季3.1
冬眠3.6.6.Final

東西我曾嘗試:
- 搜索中的context.xml(試生產)
差異 - 使用其他context.xml文件(測試)在productionmode

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: de.hoeso.gwt.platform.server.domain.common.Person.anschrift, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122) at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248) at de.hoeso.sis.server.services.common.impl.UserServiceBeanImpl.login(UserServiceBeanImpl.java:397) at de.hoeso.sis.server.rpc.LoginService.execute(LoginService.java:35)

我發現,在測試模式的HIB ernate會話已連接並打開。在productionmode(在servletcontainer存在通過entitymanager.getDelegate()沒有可用的連接hibernatesession。

+0

您的測試方法是否將兒童加載到事務中? – JMelnik

+0

「,並得到一個InvocationException」爲InvocationException添加堆棧跟蹤的副本可能有助於人們回答你的問題。測試和生產bean配置有什麼不同? – Raedwald

+0

@JMelnik:這是同一筆交易。我加載實體,下一行代碼出現此錯誤。 – ChangeRequest

回答

0

現在我發現我自己的問題與來自JMelnik一點幫助的解決方案。對於後人,我將分享我的解決方案...

後我把一個斷點在靠近代碼,而我加載對象,我看到的主要區別。

return entitymanager.isOpen(); 

(布爾型)//測試模式和productionmode

return ((org.hibernate.Session)entitymanager.getDelegate()).isConnected(); 

(布爾值)在測試模式 (布爾值)真//在productionmode假//

return ((org.hibernate.Session)entitymanager.getDelegate()).isOpen(); 

(布爾值)真//在測試模式 (布爾值)假//在productionmode

return((org.hibernate.Session)entitymanager.getDelegate()).getTransaction().isActive(); 

在生產模式中,這會在testmode中拋出一個異常 ,這將返回:(布爾值)true

結論:代碼在沒有事務的生產模式下運行。在測試模式下,存在一個事務,因爲測試類是從一個超類繼承的,該超類用@Transactional註解。在ServiceBean的層次結構中,我忘記了這一點。

註解被插入,它完全正常工作!

+0

恭喜修復!如果可以,請確保將您的答案標記爲「已接受」,以便其他人能夠從您的成功中學習。乾杯〜 –

相關問題