“could not initialize proxy - no session” with an open session available

余生长醉 提交于 2019-12-06 03:39:32

This to me looks like a really nasty bug I found in Mojarra, could be the same in this version of MyFaces. The basics are when it does validation it does a copy of the list, but it uses the concrete type of the collection, using the no-arg constructor to create the collection. In hibernate's case, this new list doesn't have all the init code run on it and it doesn't link back up to the session. It to me a long time debugging and the Mojarra source to figure out what was actually happening.

I found I had to use the collectionType attribute and set it to the java.util interface type. I don't do anything with collections anymore without explicitly telling JSF the collection type to use.

Collections in Hibernate are by default lazily loaded when you receive an object from db that contains a collection, in place of the List<> or Set<> there is a proxy for that list or set. Upon calling the getter method for that collection, it will be fetched. However, a problem would be if the session from which the object originated, is closed or no longer accessible (which should not be in the constraints of your OpenSessionInView filter).

You can try calling session.merge on your objects if they are handled by different sessions. Or you can manually call the getter of your List during fetching which will trigger the proxy. Or you can add FetchType.Eager on your collection, in which case the object will not be a proxy but a real object even at the time of fetching.

But I see you are using Weld for CDI, why not use Seam Persistence module which has support for transaction management? Then you will be able to implement your open session in view approach very quickly.

http://www.seamframework.org/Seam3/PersistenceModule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!