0

I am using the get method to retrieve the data for an entity. When the session is closed, since lazy loading is enabled, some of the properties are proxies

At a later point in program I want to retrieve those proxy properties. Now what should I do ? Should I open a session everytime I want to fetch a proxy property ? If this is is the case how do I tell an hibernate that inside the session ? I know that I first need to re-attach the instance to the new session bu I am not sure what to do next.

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

1 Answers1

0

Yes, a session is necessary to initialize proxies.

What you should do is to avoid closing your session until all work has been completed. If you're developing a webforms (asp.net) application, I would recommend you to implement a session-per-request. That is, you should create a session for every web request, then close it at the end of the request. By doing this, you can ensure that your session will remain open for any database work that needs to be done. You can find the implementation details for session-per-request in this answer.

If you're working with a Windows application (e.g. Winforms), then you should probably consider opening a session per form, which is similar to the session-per-request method above. Open a session during Form_Load, and close it in Form_Close.

The official NHIbernate manual also discusses this in Topic 19.1.4: Initializing collections and proxies.

Community
  • 1
  • 1
JW Lim
  • 1,794
  • 3
  • 21
  • 41