aspose file tools*
The moose likes Object Relational Mapping and the fly likes Sessions being closed prematurely Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Sessions being closed prematurely" Watch "Sessions being closed prematurely" New topic
Author

Sessions being closed prematurely

Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

What does your filter/interceptor look like?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Dec 14, 2004
Posts: 194
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sessions being closed prematurely
 
Similar Threads
what is convenience method in java?
Hibernate is returning proxy objects even if no matching row is found on the database
Problem deleting value-type object from Collection in Hibernate
Abstract Generic Class for DAO with spring SessionFactory injection setup help needed.
Unable to locate current JTA transaction error