Can I configure Hibernate/JPA to update an entity record when only non-timestamp fields have been modified?

被刻印的时光 ゝ 提交于 2019-11-30 19:08:48

问题


At the moment I have an Hibernate entity class as follows:

@Entity
@Table(name = "entity")
public class Entity implements Serializable {

    private static final long serialVersionUID = 2040757598327793105L;

    @Id
    @Column
    private int id;

    @Column
    private String data;    

    @Column(name = "last_modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModified;
}

I've found that even when the non-timestamp fields are not modified (i.e. the data field) a call to merge still updates the timestamp. I would like the timestamp to only update when other data fields have changed.

Is there anyway I can prevent calls to merge making a SQL UPDATE when all other data fields are not modified, or do I have to explicitly check for this myself in the code?


回答1:


Update (thanks to comment):

Since v4 of Hibernate @Entity annotation is deprecated and for allowing dynamic updates you should use @DynamicUpdate(true) (in conjunction with @SelectBeforeUpdate(true))


If you want to prevent unmodified fields to be included in UPDATE queries, add this on your entity:

@org.hibernate.annotations.Entity(dynamicUpdate=true) // update only changed fields
public class ...


来源:https://stackoverflow.com/questions/8807609/can-i-configure-hibernate-jpa-to-update-an-entity-record-when-only-non-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!