I have a small problem in my application. It is developed to work on mobile interfaces, so I need it as lightweight as possible. I'm refactoring my code to reach that, but I came across a problem with the session. My entities were with FetchType.EAGER, so I changed it to lazy. Here is the exception:
I read that it occurs because Hibernate tries to access the property in lazy time with a proxy, but it fails because there is no session. I am using declarative transaction with Spring, so it manages the sessions to me.
Is there any way to make Hibernate (or Spring) create that session automatically? A bean or something? Here is my applicationContext.xml:
Yes. Hibernate has a variety of patterns you can use to attach to delegate session management to a different layer. For example the Open Session In View pattern. I don't know much about Spring but it should provide simmilar solutions.
Thanks Paul, that helped me a lot. I created a class that implements OpenSessionInViewFilter (Spring), and it works fine, except for one problem.
In my method getSession(), The only way for the application to work properly is using FlushMode.ALWAYS. Is this good for SELECT comands? Isn't it better to flush the session only when a persistence command is executed, or am I wrong?