1
大家好我有我的數據庫中的updated_at
列有一些問題。我使用休眠,當我更新模型時,除了@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 |
---------------------------------------------
「@PrePersist」是否正常工作? 「@PrePersist」或「@PreUpdate」都不能使用Session API工作。 EntityManager應該工作得很好。也許你應該參考這個[問題](http://stackoverflow.com/questions/4121628/issues-with-the-usage-of-preupdate/) – Bonifacio
'@ PrePersist'正常工作,當我添加一個新的模型方法'onCreate()'被調用。我一起使用spring和hibernate,所以我不知道EntityManager是否已設置和/或是否具有適當的配置,如果需要,我可以編輯我的帖子,包括spring和休眠配置 – IlGala
如果您使用SessionFactory,那麼很可能該EntityManager根本沒有使用。我最好在這裏嘗試一下使用EntityManager的實現,但不幸的是我不知道如何使用Spring來設置這種類型的東西。 – Bonifacio