struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Taylor" <rtay...@dtgresults.com>
Subject Re: Struts2 Dynamic Merge of two webform objects / join two objects in database
Date Fri, 03 Sep 2010 11:31:02 GMT
Hi Nicolas,

I'ld also be interested in hearing about an elegant solution to this problem 
as well.

Ideally it would be nice if we all had nice simple models where we could 
reuse our hibernate
objects for both persistence and web binding. However, if have found, this 
is often not the case.

Although maybe not ideal, for the case you mentioned, i have always ended up 
creating/using a separate DTO (data transfer object)
which contained a subset of the Hibernate entity object properties.

If the properties are simple enough, I have been able to use 
BeanUtils.copyProperties(....)
to transfer the appropriate values between the two objects.

Probably not the answer you were looking for, but this has been my approach 
so far.

/robert



----- Original Message ----- 
From: "Nicolas Kopp" <Nicolas.Kopp@empirica.com>
To: <user@struts.apache.org>
Sent: Friday, September 03, 2010 5:23 AM
Subject: Struts2 Dynamic Merge of two webform objects / join two objects in 
database


Hi there,

We (Team of two developers) use Hibernate integrated in a Struts2 web 
application. If we want to perform an update to our objects on the database 
we fetch the data and load the resulting object into a web form where the 
user can edit the data. In this web form not all the object's attributes are 
displayed and editable. Submitting the form data will create a new object 
with the values set in the web form, which is then saved to the database 
again using Hibernate.

Since not all the attributes from this certain object were set in the web 
form, some values result as null, which leads us to the problem that these 
values (even though there are set in the DB) get overwritten with null 
values. Hibernate is set to dynamic-update=true in all involved mappings.

There are (at least) three workarounds that we could think of:

1.            Adding the missing attributes as hidden fields in the web form 
(not very flexible)
2.            Adding the missing attributes in the Java code before saving 
the object to the DB (not very flexible either)
3.            Writing a utility class which merges the data from the 
persistent object with the one created by the web form (flexible but 
cumbersome).

What we do right now is to iterate through all methods of the specific 
object, find out the getterMethods and setterMethods of both the "old" 
database-object and the "new" Hibernate object, compare values and take the 
values which are not null. This is not a very satisfying resolution, because 
some objects contain Other objects with values and so on, sometimes three or 
more layers of depth. Also, using reflection in Java is not a very nice 
resolution.

We are sure there must be other people out there facing similar problems but 
even after quite some research efforts we were not be able to find anything 
of help in the web.

We are even not sure if this is more a Hibernate or a Struts2 problem.

We'd be really grateful if someone could point us in the right direction.
Thanks in advance!

Nicolas & Alex


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message