*
The moose likes Spring and the fly likes Spring MVC + Hibernate: Update one column? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring MVC + Hibernate: Update one column?" Watch "Spring MVC + Hibernate: Update one column?" New topic
Author

Spring MVC + Hibernate: Update one column?

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
I'm trying to update one column of a table using hibernate but I can not prevent the operation from overriding the entire table record. I have tried different options based on searches I've done on google and I just don't know what I'm missing. Can someone help me?

This is what I have so far:

Here is a snippet of my hibernate config file for the "detail" table:



Here is a snippet of my jsp form:



Here is the snippet of my Controller that processes the form:



So when I test the form it hits the mergeDetail method since the record already exists....




I understand that I could probably just pass the column I'm interested in changing instead of the entire Detail object. However, I'm doing this dynamically in which the jsp form where I define the "path" is not really hardcoded with the "summary" column. Instead I'm actually passing a variable in it's place so I don't have a handful of jsp forms, I can just use the same form and just change the path value.

Any suggestions?

Thank you.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

This is actually unrelated to Spring.

Basically, you have a detached object, and when you merge it back into Hibernate, Hibernate doesn't know what you changed, so it has to assume it is all changed. That is why the dynamic-update isn't working as you expected.

To see what I mean, write a test that creates a Session, loads one record. Change one property of your class, then flush and notice that the update statement only has the changed field.
In another test method. Load one record in a Session. Close the session. Change one property of your class, create a new Session then merge the object into that session then flush and now the update statement will have all the fields.

The only way to not have this is to manually write your update statement as SQL and run that through the Session object. (There is an obscure byte code instrumentation for something else, that will add an update flag to your object, but even that will include all the fields)

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
The way I worked around this was to created an array of the Objects elements and pass them to the form and add the <form:hidden> as needed to retain the data.

It's a really kludgy way of doing it but I spent most of my day trying to figure this out in a more eloquent way without finding anything that matches my scenario.

In the jsp form I did something like this...

 
 
subject: Spring MVC + Hibernate: Update one column?