You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nicolas Kopp <Ni...@empirica.com> on 2010/09/03 11:23:34 UTC

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

AW: Struts2 Dynamic Merge of two webform objects / join two objects in database

Posted by Nicolas Kopp <Ni...@empirica.com>.
Hey,

it seems that using the Scope plugin described by Rahul here fits best for our uses in this case.:


" 

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

"

copyProperties() from the BeanUtils also seems to be nice.

 Big Thanks to all of you for your help! I'll also post the resolutions in the forums where we also posted this question.

cheers!

Nicolas

-----Ursprüngliche Nachricht-----
Von: Allen Lee [mailto:Allen.Lee@asu.edu] 
Gesendet: Freitag, 3. September 2010 21:15
An: Struts Users Mailing List
Betreff: Re: Struts2 Dynamic Merge of two webform objects / join two objects in database

We use the paramsPrepareParams stack for this (see
http://struts.apache.org/2.x/docs/interceptors.html for an example).
Basically the flow is: (1) params get applied first but the primary
use of this is to set the database entity ID that you want to update
(2) the prepare() method is invoked, this is where you load your model
object from the db with the entity ID from step 1 (3) params are
applied again, but this time they will be operating on your loaded
database entity, not a fresh instance.

On Fri, Sep 3, 2010 at 4:31 AM, Robert Taylor <rt...@dtgresults.com> wrote:
> 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"
> <Ni...@empirica.com>
> To: <us...@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
>
>



-- 
Allen Lee
Center for the Study of Institutional Diversity [http://csid.asu.edu]
Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402
Office: 480.727.0401 | Fax: 480.965.7671

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


Re: Struts2 Dynamic Merge of two webform objects / join two objects in database

Posted by Allen Lee <Al...@asu.edu>.
We use the paramsPrepareParams stack for this (see
http://struts.apache.org/2.x/docs/interceptors.html for an example).
Basically the flow is: (1) params get applied first but the primary
use of this is to set the database entity ID that you want to update
(2) the prepare() method is invoked, this is where you load your model
object from the db with the entity ID from step 1 (3) params are
applied again, but this time they will be operating on your loaded
database entity, not a fresh instance.

On Fri, Sep 3, 2010 at 4:31 AM, Robert Taylor <rt...@dtgresults.com> wrote:
> 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"
> <Ni...@empirica.com>
> To: <us...@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
>
>



-- 
Allen Lee
Center for the Study of Institutional Diversity [http://csid.asu.edu]
Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402
Office: 480.727.0401 | Fax: 480.965.7671

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


Re: Struts2 Dynamic Merge of two webform objects / join two objects in database

Posted by Rahul Mohan <ra...@tcs.com>.
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" <rt...@dtgresults.com>
To:
"Struts Users Mailing List" <us...@struts.apache.org>
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" <Ni...@empirica.com>
To: <us...@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



=====-----=====-----=====
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



Re: Struts2 Dynamic Merge of two webform objects / join two objects in database

Posted by Robert Taylor <rt...@dtgresults.com>.
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" <Ni...@empirica.com>
To: <us...@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