Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sessions being closed prematurely

 
Eric Nielsen
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic