Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring MVC + Hibernate: Update one column?

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic