如果只有非时间戳字段被修改,我可以配置Hibernate / JPA更新实体记录吗? [英] Can I configure Hibernate/JPA to update an entity record when only non-timestamp fields have been modified?

查看:152
本文介绍了如果只有非时间戳字段被修改,我可以配置Hibernate / JPA更新实体记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

private static final long serialVersionUID = 2040757598327793105L;

@Id
@Column
private int id;

@Column
私有字符串数据;

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

我发现即使非时间戳字段没有被修改(即 data 字段),调用 merge 仍会更新时间戳。我希望时间戳只在其他数据字段发生更改时才更新。



有没有办法阻止对 merge UPDATE


更新(感谢评论):



由于Hibernate的v4 @ 解决方案已弃用实体批注,并且为了允许动态更新,您应该使用 @DynamicUpdate(true)(与 @SelectBeforeUpdate如果你想防止未经修改的字段被包含在UPDATE查询中,那么你可以使用这些字段,

$ p $ @ org.hibernate.annotations.Entity(dynamicUpdate = true)//只更新已更改的字段
public class ...


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?

解决方案

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 ...

这篇关于如果只有非时间戳字段被修改,我可以配置Hibernate / JPA更新实体记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆