Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create session automatically

 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have an idea about this?
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's working fine now with the default flush mode, thanks Rahul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic