如果只有非时间戳字段被修改,我可以配置Hibernate / JPA更新实体记录吗? [英] Can I configure Hibernate/JPA to update an entity record when only non-timestamp fields have been modified?
问题描述
@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 $ c $如果所有其他数据字段没有被修改,或者我必须在代码中明确检查这个数据,那么创建一个SQL
UPDATE
?
更新(感谢评论):
由于Hibernate的v4 At the moment I have an Hibernate entity class as follows: I've found that even when the non-timestamp fields are not modified (i.e. the Is there anyway I can prevent calls to Update (thanks to comment): Since v4 of Hibernate If you want to prevent unmodified fields to be included in UPDATE queries, add this on your entity:
这篇关于如果只有非时间戳字段被修改,我可以配置Hibernate / JPA更新实体记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! @
批注,并且为了允许动态更新,您应该使用 @DynamicUpdate(true)
(与 @SelectBeforeUpdate如果你想防止未经修改的字段被包含在UPDATE查询中,那么你可以使用这些字段,
$ p $ @ org.hibernate.annotations.Entity(dynamicUpdate = true)//只更新已更改的字段
public class ...
@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;
}
data
field) a call to merge
still updates the timestamp. I would like the timestamp to only update when other data fields have changed.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?@Entity
annotation is deprecated and for allowing dynamic updates you should use @DynamicUpdate(true)
(in conjunction with @SelectBeforeUpdate(true)
)
@org.hibernate.annotations.Entity(dynamicUpdate=true) // update only changed fields
public class ...