File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes JAXB+Hibernate=LazyInitializationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB+Hibernate=LazyInitializationException" Watch "JAXB+Hibernate=LazyInitializationException" New topic


stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
I have mapped class that has lazy-fields. I retrieve object of this class from DB and close hibernate session. But I also use JAXB, that serializes object and tries to access the lazy collection. So I get LazyInitializationException.
How can I solve this problem?
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
Possible approaches:
1. Use JAXB to serialize BEFORE you close the Hibernate session.
2. Make sure that Hibernate has loaded the entire object graph that is to be serialized by JAXB BEFORE closing the Hibernate session.
Best wishes!
stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
Thanks, but both approaches are all about loading entire objects and I cannot do that - it is very expensive operation.
michael cheung

Joined: Apr 13, 2009
Posts: 3
You can also use Beanlib ( to convert the hibernate persistence pojo back to your own dto.
I agree. Here's the link:
subject: JAXB+Hibernate=LazyInitializationException
jQuery in Action, 3rd edition