Author Login
Post Reply
Nicolas,
I use the scope plugin (
https://cwiki.apache.org/S2PLUGINS/scope-plugin.html) for this. A bit
heavy on the session memory, but suits my purpose.
Put @InOut annotation on your model with scope set as session. This
outjects the object loaded from DB into session just before forwarding to
the JSP. On submission, the object is injected back into the action class
from session before the params interceptor is invoked, which will update
the same object from http request. So, basically its the same object that
gets updated.
Hope this works for you.
- Rahul
From:
"Robert Taylor" <rtaylor@(protected)>
To:
"Struts Users Mailing List" <user@(protected)>
Date:
03-09-2010 17:01
Subject:
Re: Struts2 Dynamic Merge of two webform objects / join two objects in
database
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@(protected)>
To: <user@(protected)>
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@(protected)
For additional commands, e-mail: user-help@(protected)
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain
confidential or privileged information. If you are
not the intended recipient, any dissemination, use,
review, distribution, printing or copying of the
information contained in this e-mail message
and/or attachments to it are strictly prohibited. If
you have received this communication in error,
please notify us by reply e-mail or telephone and
immediately and permanently delete the message
and any attachments. Thank you