Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of LazyInitializationException with Wicket JPA/Hibernate integration (with Spring)

i'm developing an application using Wicket as the view layer and JPA(Hibernate) as ORM. building the UI has been fun (even with ajax) using Wicket. My problem comes from integrating the persistent objects on edit pages (readonly pages are no problem using a LoadadableDetachableModel).

I'm using the OSIV filter from spring to provide an open session for the view. But, as i keep the domain objects (the @Entity mapped classes) in the edit pages, i get the dreaded Lazy loading excetion when i access properties of them in ajax callbacks.

i don't really want to go down the DTO / VO road, as i think it would only bloat the code and requires me to write lots of boiler-plate code.

One idea was to use the model objects in the view, merge the passed in object with the current hibernate session and access all getters to fully initialize the object. after this, the object would be stored in the view (seesion) and become detached. Upon save, i would re-merge it and commit the change.

Would this be an recommended way? Are there better solutions? Strange enough, most books / blogs / howtos completely ignore such issue.

What transaction management would you suggest? Right now i use @Transaction at the service layer. How would that change if i use other ways of accessing storing the data across hibernate sessions?

Any pointers / Links are welcomed as i'm kind of lost here..

thanks in advance

like image 724
bert Avatar asked Jan 18 '10 11:01

bert


People also ask

What is org hibernate LazyInitializationException?

Indicates an attempt to access not-yet-fetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed.


1 Answers

This blog post (that goes into the details of LDM) gave me some good insights especially for edit scenarios:

Building a smart EntityModel

FWIW I had very good results using a custom RequestCycle (as suggested in the comments section of the link above) in PerfBench and you can find the code here. IIRC this is a simplification of the approach (OpenSessionInView / London Wicket) from the link Bozho posted.

like image 116
Peter Thomas Avatar answered Sep 18 '22 11:09

Peter Thomas