gpt4 book ai didi

java - 如何为 AppEngine 实体添加 creationTime

转载 作者:行者123 更新时间:2023-11-30 11:45:29 25 4
gpt4 key购买 nike

我在 AppEngine 上使用 Objectify 持久化实体。

在几个实体中,我想存储 creationTime 和 lastUpdateTime。

我的意图是在第一次创建实体时将 creationTime 添加到实体中。而 LastUpdate 时间应该在实体更新时更新。

对于 lastUpdateTime,我可以轻松地使用 @PrePersist 附加一个钩子(Hook)来修改 lastUpdateTime。

但是每当我对实体进行更新时,我都想保留 creationTime。

现在一种解决方案是获取 creationTime,然后将其添加到我的实体中。

有什么方法可以让我不必在每次保存之前获取 creationTime?

我尝试了 @NotSaved(IfNull) 的组合,然后将 creationTime 设置为 null,希望 Objectify 会忽略它,但实际行为是它会在数据存储上设置 creationTime=null。

作为引用,我的实体如下所示:

public class TestEntity {
@Id
Long id;
Date creationTime;
Date lastUpdateTime;
String mutableField;
...
// Constructor1
public TestEntity(Long id, Date date, String mutableField) {
this.id = id;
creationTime = date;
lastUpdateTime = date;
this.mutableField = mutableField;
}

// Constructor2
public TestEntity(Long id, String mutableField) {
this.id = id;
lastUpdateTime = new Date();
this.mutableField = mutableField;
}
}

编辑1:
对不起。我认为我的问题不完整。我修改了我的 TestEntity 定义。让我们假设此 TestEntity 包含两个不可变字段(id、creationTime)和两个可变字段(lastUpdateTime、mutableField2)。

现在根据上下文,有可能将这五个字段都发送到客户端,然后客户端更新 mutableFields 并发送请求以更新可变字段。

为简化讨论,让我们假设使用 session 和其他方式(例如 NameSpace),我们可以确保允许用户修改此实体。

现在我打算做以下事情:
* 实体第一次创建时,使用constructor1。
* 当客户端请求这个实体时,服务器发送(id 和 mutablefield)。
* 客户端发送请求以使用 mutableField 的新值和 Id 更新实体。
* 现在在服务器端,我想在不丢失 creationTime 的情况下更新可变字段。

现在这可以通过首先从 DataStore 中获取实体,然后更新 mutableField 然后再次持久化来完成。我想知道是否有更好的解决方案。

最佳答案

使用你的构造函数。

public class TestEntity {
public TestEntity(...your normal init params...) {
creationTime = new Date();
}
}

关于java - 如何为 AppEngine 实体添加 creationTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10328540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com