• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sessions being closed prematurely

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm missing some trivial aspect of Hibernate, but I'm having extreme problems with LazyInitializationExceptions. I've been haing trouble in two places. The first is in a rather simple test case of the service layer. The second is in view code (and yes, I'm using open session in view filters).

My test code:


findById is identical to the code in the Java Persistence with Hibernate book:


If I pass in a lock=true value, I don't get the lazy initialization exception, if I pass in false I get the error. From my understanding, either lock moe should work.

I'm using standard JPA annotations to inject EntityManagers into the services.

Can anyone help me know what I'm missing?

Thanks.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your filter/interceptor look like?

Mark
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in this case the filter isn't even involved. Its a small little unit test, I'm not doing anything in a view to have to worry about keeping the session open.... Or so I thought....
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using get instead of load in that example.

I always get which one creates a full bean and which one creates a proxy, but I am guessing pretty good that load is returning a proxy object and why you get that LazyInitializationException.

Mark
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out there were two problems:

a) I was missing the @Transactional annotation on one of the involved classes
b) I forgot to reattach the User object from the Web Session back to a persistence context.

The app works now, but I'm not really happy with how I fixed b)... Going to need to see if I can push that down into my Authentication interceptors, etc, instead of handling it at the action level.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic