Friday, December 7, 2007

Spring MVC, Data Binding and JPA

A project that I am working on is using Spring MVC. Naturally, we would like to use its data binding facilities to handle data coming in from HttpRequests into our beans. As mentioned by Matt Fleming in this blog post, the Spring docs don't do a great job of explaining how to deal with data binding when the bean in question has a collection as a property. Something like this:

public class Person
{
private String name;
private List<Address> addresses;
}

As Matt mentions, using LazyList found in the Apache Commons Collections is one way to get Spring to bind dynamic collections into your beans (in the example, use a lazy list for the addresses). However, you can run into some issues if your persisting your beans using JPA.

We're using OpenJPA, and when we try to persist an object that has a LazyList in one of its properties, it chokes. The problem is that it can't proxy LazyList because it doesn't have a default constructor. To get around this, we're applying a pretty simple hack. Before Spring does the data binding, we decorate the collection with a LazyList. Before we persist the object with JPA, we copy the collection back to a plain old ArrayList.

An example of doing this can be seen with Spring MVC's SimpleFormController in DocumentLocationCreationController. To help out with the whole process, we've created a LazyListHelper to make the decorating and copying easier.

If you're using Spring MVC with JPA, hopefully this will save you some headaches.

8 comments:

Toha said...

can I download the netbeans project for your post please?

sathiya said...

Can i download this project ..

Legal Herbal Online said...

That is a amazing web page in addition to I am unable to suggest anyone guys Quality Ibeacon ample. Full of reference in addition to excellent page layout super easy about the sight. You should complete sustain this specific excellent perform.

jhon philips said...


I just have a gaze here and appear Brobdingnagian to travel seeking out this journal. flush content composing hand and intensely useful location auto title loan. i might like most of we've got got got a bent to tend to tend to tend to World Health Organization unit to travel making a shot these kinds of things, here we've got got got a bent to tend to ar getting to notice everything. i'm with the content honor associate degreed do esteem him as associate honest supplier. Thanks for your exertions and you too.

steve7876 said...

The first several months of my site there were no comments; just give it time; now they come in like crazy every day! Thanks.Watercolor Paintings for sale

marko said...

Requirements of individuals can be shifted, for which they need money. These needs can fluctuate from purchasing another dress for some event, purchasing another pack, a sudden therapeutic cost or an instructive need and so forth for everything these needs, individual money advance gives the cash. aaa1autotitleloans.com/chicago

marko said...

When you have talked about your advance choices with a couple of moneylenders, you can pick the best and most suitable offer and begin putting something aside for that fantasy excursion. As of now, you ought to be looking into conceivable housing and evaluating carrier tickets and in addition auto rentals. auto title loans montgomery

marko said...

Simple access to client administration - Did you realize that not all the organizations found in your hunt really have a phone that works? It is vital that you have the capacity to contact your loan specialist amid typical business hours. Try not to settle for anything not as much as quality client administration. Somebody who will tune in, answer your inquiries and location your worries without needing to make all needed endorsements. Recollect that, it is their business to offer assistance. Cash Advance