我使用的是spring 3.2 spring security和hibernate 3.4.0。有沒有解決方案來避免LazyInitializationException
當我的用戶登錄時,我將用戶存儲在我的自定義主體中。
在應用程序的後面,我想訪問與我的用戶關聯的集合。 所以我做的:
User u = ((MyCustomPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
u.getMyCollection();
和我得到的過程中LazyInitException。 所以我臨時的解決辦法是重新獲取用戶:通過這樣做它的工作原理
User u = ((MyCustomPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
u = userDao.find(u.getId());
u.getMyCollection();
,但我覺得這並不是一個非常優化的解決方案,因爲我的用戶OWS 4分集,所以我就在被迫去取用戶從db多次在不同的地方(在我的自定義身份驗證器,在攔截器,在我的控制器...)。
有沒有解決這個問題像全球交易或什麼的?
感謝
可能重複在Spring使用JPA/Hibernate的,我該如何保持一個會話打開,以避免延遲初始化例外?](http://stackoverflow.com/questions/1139985/in-spring-with-jpa-hibernate-how-do-i-keep-a-session-open-to-avoid-lazy-initial) –