• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We 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 alle 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, 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!

Nico & Alex
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please BeForthrightWhenCrossPostingToOtherSites (or in this case, mailing list). Thanks!
 
Nicolas Kopp
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course, you are right.

I also posted this question in the official Hibernate-Forum here: Hibernate Topic
because, as said, we're not sure whether this is a hibernate or struts2-related problem.

We're also on the struts user-mailing-list and I will post any resolutions form there in both forums.

cheers!
Nico
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've almost always solved this issue with a DTO that I use with a BeanUtils-style property copier (deep or shallow, depending). One reason is that this way the data exposed to/received from the view layer is exactly what's necessary, which can save headaches later on. It *is* a pain, though.
 
Nicolas Kopp
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally we discovered several solutions for this problem. I will list them here for future users.

First of all, what we finally did and worked for us:

in the affected class, we implemented preparable like this:




and added an override like this:



which then initialized the Object as desired and directly worked with the database Object. More Information: Click here

Other solutions:

Use BeanUtils.CopyProperties for simply gaining and setting all the properties: Link here

Just another solution, which has the same concept, but is more flexible, is to write a wrapper like this one:



Another Solution would be this one:



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.




(Solution by Rahul, thanks)

I hope it helps someone out there!

Cheers.


 
Look ma! I'm selling my stuff!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic