ovod
ovod

Reputation: 1178

update only nonEmpty/nonNull fields in hibernate/Spring MVC

I have next situation. I have class User which is entity and binding JSP form for user adding/editing/ deleting - there are 3 different buttons for it. My problem is when I want to edit specific user (I enter username and field which I want to edit in the form), hibernate generates SQL query with ALL fields:

Hibernate: update users set user_age=?, user_country=?, user_email=?, user_login=?, user_name=?, user_password=?, user_status=? where user_id=?

Problem is that others are empty or null. And I get error:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

How can I make hibernate to generate something like this (in case I put new age into the form):

Hibernate: update users set user_age=? where user_id=?

@DynamicUpdate(value=true) - does not work. I think this anotation works only for objects not created after Login Form analisys and binding.

Upvotes: 1

Views: 397

Answers (1)

Josh Chappelle
Josh Chappelle

Reputation: 1588

When the user submits the form you need to pull the User fresh from the database using hibernate and then set the value of the property on that object. Then save it. That way the other values aren't empty or null.

Upvotes: 1

Related Questions