views:

339

answers:

5

Hi, 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

+2  A: 

This is a short presentation on OpenSessionInView with Wicket.

If used properly, the OpenSessionInView approach should guarantee that no LazyInitializationException occurs.

Bozho
+2  A: 

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.

Peter Thomas
A: 

thanks Bozho and peter.

I had a look at the 2 sources referenced (another thanks to peter, i always enjoy reading your detailed blog comparing web frameworks), but as far as i understand, they basically substitute the Spring OSIV filter with a self made solution. Since i already use Spring for DI, i can use the Spring OSIV as well. Or did i oversee something in the 2 solutions?

I already use an IModel developed following igors smart entity model. Perhaps i did not made myself clear enough in the original question:

I 'm looking for a solution that allows me to use the domain/JPA-Models in the UI layer, having the IModel there fully initialized (no Lazy load exceptions), and also have full control over when the changes are stored into the database. Attaching / reattaching the model in each request would mean the changes made in the UI would get stored in the DB at flush time (say, a hibernate query). That is not what i want.

I will strip down what i have right now (due to pressure in the current project, there are to many 'workarounds' to get it somehow working) and do some test if my understanding of the current situation is correct.

thanks again for your input, any comments on my thoughts are highly welcome.

bert
A: 

If you use LoadadableDetachableModel's that you do not pass to a component as the model, then wicket will not call .detatch() on them, and often they are not serialzied either, so they will have old data, and throw the lazy exception.

Make sure to always pass LDM to a component, or detach them yourself.

slckin
A: 

I finally had time to work on that problem again. Don 't know how i could have missed the simple solution ;)

We had developed our own UIFormModel implementation of the Wickets IModel Interface. Since i wanted to keep the user input during http requests, i did nothing in the detach() call, keeping (and serializing) the model object in full state.

All i had to add was a flag that detach() was called and in check that flag in the getObject() method. If the flag was set, i do a EntitManager.merge() and have a reattached model that i can use in the UI components.

Thanks all for your input

bert