Upida - intro

Spring and Hibernate are widely used in Java web applications. Everyone agrees that hibernate is a very handy tool. But what Hibernate can do ? Basically - it can track our domain objects' modifications and persist them to database. I can load some records from database into domain objects, do some modifications to them, and call transaction.commit(), and all modifications get persisted right away.

But does it always fit our needs ? For ex. how about web world ? Web applications are usually stateless. We cannot expose domain objects for modifications in web. Instead, in web/MVC world We usually use detached objects or DTO. How it works ?
User makes modifications in browser, then spring MVC automatically transfers those HTML modifications to a customized DTO objects using MVC model binding. Then we do some programming effort to copy modifications from DTO objects to persistent objects and only then we persist them. Of course, instead of DTO we could reuse our domain classes. But the idea remains the same: we still have to implement custom logic that transfers modifications to persistent objects. And this logic is always different, depending on UI.

For example - we have a web form that updates Customer address information, and another form which updates Customer details, and another form that assigns Customer to a Group of customers. All three workflows actually end up in data layer by updating the same Customer record.
The difference between these workflows is different incoming data, and different mapping code. All three workflows accept different data, and each workflow must have different mapping code which can copy fields from incoming data to the persistent Customer object.

Is there a way to avoid writing the code that transfers data from incomig data (DTO or detached object) to a persistent domain object ?
Let us imagine that our web application accepts data in JSON. We can determine based on JSON data which field to copy. We simply must copy only those fields which are present in JSON. It seems pretty straightforward.
Using our smart code we parse JSON into a detached object, while collecting information about which field was present in JSON. Then, our smart code maps fields from the detached object to the persistent one, using the collected information about JSON fields. And we are ready to persist the persistent copy.
The same technique can be applied not only to JSON, but to HTTP POST parameters as well.
Please, Donate ! If you click on the banner in the left side of this page, you will donate around 20 cents to this project. Thank you !


Follow the links below to download source code and examples: