【问题标题】:Unable to get saved entity using objectify无法使用 objectify 获取保存的实体
【发布时间】:2013-05-18 16:48:06
【问题描述】:

我无法使用 Objectify 可靠地获取我保存的实体。

看起来缓存已损坏。奇怪的是 - 我可以通过管理控制台数据存储查看器正确看到保存的实体。我还编写了一个小程序来使用 RemoteApi 查看实体,我可以正确看到保存的值。

当我使用 servlet 或云端点 rest api 连续查询实体时 - 我的连续查询给出了不同的结果,并且看起来数据存储区/缓存中的某些内容已损坏。

我的实体看起来像这样。

class ContentEntity {
  @Id Long id;
  String html;
  @Index String tag;
  boolean publish;
}

我是这样保存的。

ContentEntity entity = ofy().load.type(ContentEntity.class)
    .filter("tag", "my tag").first().get();

if (null == entity)
    entity = new ContentEntity();

entity.html = "my html";
entity.tag = "my tag";
entity.publish = true;

ofy().save.entity(entity).now();

我是这样检索的。

ContentEntity entity = ofy().load().type(ContentEntity.class).
            filter("tag", "my tag").first().get();

发生的事情如下-

1) 让 ContentEntity.html 的初始值为 "value 1"
2) 保存一个新值 - “值 2”
3)使用管理控制台数据存储查看器,我可以看到“值 2”已正确保存。 (使用远程 api 我也可以看到“值 2”)
3)使用上面粘贴的检索代码通过servlet或rest api查看实体。我看到“价值 2”
4)通过servlet或rest api再次查看实体。我看到“值 1”
5) 再看一遍。我看到“价值 2”
它不断在“值 1”和“值 2”之间切换

在我的开发环境中一切正常,但在 appengine 中却不行。

看起来我做错了什么,没有正确处理最终的一致性。我总是想要强烈一致的结果。我不介意我的查询是否慢一点。我该怎么办?

任何提示/建议/帮助将不胜感激。

问候,

沙迪亚

【问题讨论】:

    标签: google-app-engine google-cloud-datastore objectify


    【解决方案1】:

    原来这是因为我忘记在 web.xml 中添加 objectify 过滤器,正如Objectify wiki page 中提到的那样

    我在 web.xml 中添加了以下内容,问题就解决了。

    <filter>
        <filter-name>ObjectifyFilter</filter-name>
        <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    问候,

    沙迪亚

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2012-10-01
      • 1970-01-01
      • 2011-11-01
      • 2020-08-20
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多