I'm trying to figure out how to apply transfer objects in a Hibernate application where there is remote client server communication.
Suppose I have an object persisted in the database, a user modifies it and a transfer object with the modified data is returned to the backend, then its not so simple to map this to a business object.
For example when this Parent TO also has child TO's then the mapping function would have to do a lookup of the existing parent and child BO's, copy properties and persist those objects.
But I would like to create clean mapper classes that map the data of the TO to the BO in hte following way:
Code:
Parent parent = new Parent();
// copy properties from parentTo
for(ChildTo childTo : parentTo.getChildren()) {
Child child = new Child();
// copy all properties and set the parent
}
return parent;
That way I would simply make one call to hibernate, such as a merge or saveOrUpdate (but which one would I best use? because merge works for some things while saveOrUpdate for others)
But the thing is, if I try it out like this I get NonUnique exceptions because there is already a row with that ID :(
In short I would like to know how one should implement the persistance / synchrtonisation of data with hibernate when using transfer objects and mapping them from and to business objects , because I seem to find little usefull info about it :s