morphia支持自动时间戳吗?

Sad*_*mar 1 mongodb morphia

有没有人知道在创建/修改操作期间,morphia是否支持在mongodb中创建/更新集合中文档的自动时间戳.

我已经知道mongodb中没有这种支持.我想知道是否有任何方法可以获得morphia驱动程序中数据或文档的最后访问/更新时间.

谢谢,悲伤

xer*_*raa 7

我通常使用基本实体,所有其他实体都扩展.它提供ObjectId,创建日期,最后更改日期,禁用标志,...

相关的代码片段如下所示:

protected Date creationDate;
protected Date lastChange;

// Getters and setters or final setters which don't do anything,
// if you only want to allow the entity to update the values

@PrePersist
public void prePersist() {
    creationDate = (creationDate == null) ? new Date() : creationDate;
    lastChange = (lastChange == null) ? creationDate : new Date();
}
Run Code Online (Sandbox Code Playgroud)