Forums Register Login

Create session automatically

+Pie Number of slices to send: Send
Hello ranchers,

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:


Any help would be appreciated.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Does anyone have an idea about this?
+Pie Number of slices to send: Send
The default flush mode is "AUTO"....and unless you require "ALWAYS", you should use the default only.
see here

I am not very aware about the OpenSessionInViewFilter and if there is any particular requirement of the flush mode in it...
+Pie Number of slices to send: Send
It's working fine now with the default flush mode, thanks Rahul.
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 985 times.
Similar Threads
Dependency injection problem
unsatisfied Dependancy exception
login form with database and spring security
Spring 2.5 with Hibernate 3.0 error when creating dataSource for Hibernate SessionFactory
Migrating from Spring 2.0 and Hibernate 3.0 to Spring 3.1 and Hibernate 4.1
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:52:19.