13

I am using JPA2.0 with Hibernate.

For example I have JPA entity for below table:

User [userId, userName, userAddress]

I can fetch user entity using find() method:

User user = entityManager.find(User.class , 1L); // 1L is user id

Now, If I change any user's state (let say userName) on same user entity I fetched above:

user.setUserName("Narendra");

And perform merge using merge() method:

entityManager.merge(user);

Merge is performed successfully with below query which is fired by JPA/hibernate while merging:

Hibernate: update User set USERID =?, USERNAME=?, USERADDRESS=? where USERID=?

Here my question is, If I changed only user name state in User entity, I did not change the userId, userAddress etc. JPA should fire below query (just to change userName) rather above query.

 Hibernate: update User set USERNAME=? where USERID=?

Any idea on it?

Narendra Verma
  • 2,372
  • 6
  • 34
  • 61

2 Answers2

12

Look into the dynamic-update property.

dynamic-update (optional - defaults to false): specifies that UPDATE SQL should be generated at runtime and can contain only those columns whose values have changed.

Take into account that dynamic updates can have some impact on performance. There's a little overhead involved in using it, and it might be counterproductive if you don't have a large (maybe legacy) table with lots of columns that are unlikely to be modified. See this related question Hibernate : dynamic-update dynamic-insert - Performance Effects.

Hibernate < 4.0

If you're using Hibernate Annotations, use the Hibernate specific @Entity annotation along with the JPA one:

@org.hibernate.annotations.Entity(dynamicUpdate = true)

If you're using XML mappings:

<class name="User" table="user" dynamic-update="true">

Hibernate >= 4.0

The Hibernate-specific @Entity annotation has been deprecated in Hibernate 4.0, and is scheduled to disappear in 4.1. The proper way to use dynamic updates is to annotate the entity with @DynamicUpdate now (thanks @Tiny for the heads up).

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Thanks for the reply. This solution I tried and worked fine. But, I am concerned about adding hibernate specific properties in all the entites those are defined as JPA entites. Is there any way to configure some thing in persistence.xml so that each entity can be plugged with this configuration? – Narendra Verma Apr 02 '13 at 13:17
  • There is an interesting question related to this very same subject: [Hibernate JPA how to configure dynamic-update in persistence.xml](http://stackoverflow.com/q/10837584/851811). It isn't possible to define this behavior in `persistence.xml`, as it is Hibernate-specific. Maybe you could try using a specific hibernate-config file along with `persistence.xml`, or use some kind of runtime bytecode manipulation in a startup listener to add the Hibernate `@Entity` annotation at runtime to all mapped classes. And remember to accept the correct answers for your questions. – Xavi López Apr 02 '13 at 13:33
  • Yeah, I got the Idea. Last but not least, I accepted your answer. Thanks – Narendra Verma Apr 02 '13 at 13:45
  • @Tiny thanks, added a note on the answer about that (also thanks for telling, I wasn't aware yet :-) – Xavi López May 19 '14 at 07:48
2

Also can be used as annotation:

import org.hibernate.annotations.DynamicUpdate;

@Entity
@DynamicUpdate
@Table(name = "payments")
public class Payment {
    ...
}
Pavel Evstigneev
  • 4,918
  • 31
  • 21