我正在使用 Google 数据存储和 objectify 4.0.1 来存储消息。每条消息都附有评论列表。
消息 ID 5708313257836544 有 16 条评论。 10:34:08 添加了第 17 条评论。 12:01:46 的 GET 显示该消息有 17 条评论,12:02 的另一个 GET 显示该消息有 16 条评论,而 12:04 的以下命令再次返回 17 条评论。没有评论被删除。
代码如下:
@Entity
public class Message {
@Id private Long id;
private List<Comment> comments;
//getters and setters
//equals and hashcode over-ridden using id
}
@Embed
public class Comment {
private String message;
private Date date;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getDate() {
return date;
}
public int hashCode() {
return date.hashCode() * message.hashCode();
}
public boolean equals(Object obj) {
if (obj==this)
return true;
if ( !(obj instanceof Comment) )
return false;
Comment other = (Comment) obj;
return other.getMessage().equals(message) && other.getDate().equals(date);
}
我尚未使用 @Cache 注释启用这些对象的缓存。
为什么会发生这种不一致?如何防止这种情况发生?
最佳答案
简而言之,除非您要在实体中使用祖先,否则数据将以最终一致性的方式存储。这是设计使然。您可以在Balancing Strong and Eventual Consistency with Google Cloud Datastore上阅读更多相关信息。 .
关于java - 为什么 objectify 返回不一致的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28366254/