大家好,我的数据库中的 updated_at
列遇到一些问题。我正在使用 hibernate,当我更新模型时,除了 @preUpdate
方法之外,一切正常。我的所有应用程序模型都扩展了这个基本模型:
@MappedSuperclass
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Access(AccessType.PROPERTY)
public class BaseModel implements Serializable {
// VARIABLES ---------------------------------------------------------------
private Integer globalKey;
private Integer id;
private Date createdAt;
private Date updatedAt;
private Date deletedAt;
private User createUser;
private User updateUser;
private User deleteUser;
// CONSTRUCTORS ------------------------------------------------------------
// GETTERS AND SETTERS -----------------------------------------------------
// [hashCode, equals and toString methods] ---------------------------------
@PrePersist
protected void onCreate() {
createdAt = new Date();
}
@PreUpdate
protected void onUpdate() {
updatedAt = new Date();
}
}
这是 DAO 中用于更新的方法:
public MYOBJECT updateEntity(MYOBJECT entity, User updateUser) throws Exception {
entity.setUpdateUser(updateUser);
sessionFactory.getCurrentSession().update(entity);
return entity;
}
我错过了什么吗?在我的数据库中我看到:
---------------------------------------------
| id | updated_at | id_user_update |
| 1 | NULL | 1 |
---------------------------------------------
最佳答案
我找到了问题的答案 here .
但只需稍微修改一下...这是我的代码:
首先,我从 BaseModel
类中删除了这两个方法:
@PrePersist
protected void onCreate() {
createdAt = new Date();
}
@PreUpdate
protected void onUpdate() {
updatedAt = new Date();
}
然后我创建了一个BaseTimestamps
接口(interface),用于设置我的createdAt和updatedAt变量
public interface BaseTimestamps {
/**
* On create event.
* used to setup a the creation variable.
*
* @param createdAt
*/
void setCreatedAt(Date createdAt);
/**
* On update event.
* used to setup a the update variable.
*
* @param updatedAt
*/
void setUpdatedAt(Date updatedAt);
}
最后,我在 hibernate.cfg.xml
文件中为 hibernate session 工厂 创建了两个监听器。
// SAVE LISTENER
public class SaveListener extends DefaultSaveEventListener {
@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
if (event.getObject() instanceof BaseTimestamps) {
BaseTimestamps record = (BaseTimestamps) event.getObject();
record.setCreatedAt(new Date());
}
super.onSaveOrUpdate(event);
}
}
// UPDATE LISTENER
public class UpdateListener extends DefaultUpdateEventListener {
@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
if (event.getObject() instanceof BaseTimestamps) {
BaseTimestamps record = (BaseTimestamps) event.getObject();
record.setUpdatedAt(new Date());
}
super.onSaveOrUpdate(event);
}
}
这是我的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="my.package.MyModel"/>
// ............................
<mapping class="my.package.OtherModel"/>
<event type="save">
<listener class="my.package.SaveListener"/>
</event>
<event type="update">
<listener class="my.package.UpdateListener"/>
</event>
</session-factory>
</hibernate-configuration>
当我执行创建或更新时,一切正常,我在数据库中看到了正确的值。
关于java - Hibernate预更新不更新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656429/