Is there a clean way to make Hibernate work with lazy initialization in Swing?
Here is a description of what I want to do in a Swing application.
So, imagine I have an object Client which inside has some collections. At the application start, I want to load only the object Client and display its basic attributes (like name, age, etc) in a table. At a later time, when the user wants to, I want to load the full Client (the collections).
When I tried to use Hibernate to lazy load the Client, I was getting some problems because of trying to load these objects outside the original session that loaded Client.
I was searching online for a clean/easy way to do this but in the end I ended up doing a hack that I'm not very proud: Basically I refactored the basic attributes out of the Client and created a class ClientDetails. In the Client I now have a ClientDetails. So at startup I load only ClientDetails and then when necessary I load Client, which will load again a ClientDetails. This will make me have to update the reference of the original ClientDetails that was loaded the first time (this is messy).
So, my question, is there a better/cleaner way to do this by using lazy 开发者_高级运维loading?
Hope this was not too confusing :)
Thanks!
You should load the Client
object, close the Session
that loaded it, and then when you want to display the collection, open another Session
and reattach the Client
object to it.
精彩评论