JPA2.0/Hibernate: Why JPA fires query to update all columns value even some states of managed beans are changed?
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).
Also can be used as annotation:
import org.hibernate.annotations.DynamicUpdate;
@Entity
@DynamicUpdate
@Table(name = "payments")
public class Payment {
...
}