Objectify 无法保存 @Embedded 字符串列表

发布于 2024-11-02 10:02:51 字数 889 浏览 4 评论 0原文

Objectify (2.2.3) 似乎不想处理 @Embedded 字符串列表,尽管所有文档似乎都说这应该是可能的。 这些字符串的处理方式就像它们是需要转换的自定义对象一样。 最简单的示例:

public class Test {
  @Id public Long id = null;
  @Embedded private List<String> strings = new ArrayList<String>();

  private Test() {}

  public Test(String[] in) {
    for (String s : in) {
      strings.add(s);
  }
}

此类的实例保存为:

  Key: 7
  ID/Name: ahpzY2hlZHVsZS13aXRoLXlvdXItZnJpZW5kc3IKCxIEVGVzdBgHDA   
  strings.hash: [0, 0]

请注意,字符串是通过哈希保存的,它是字符串中唯一的非最终字段

此代码将失败:

ObjectifyService.register(Test.class);
Test t = new Test(new String[] { "aa", "bb" });
Objectify ofy = ObjectifyService.begin();
ofy.put(t);
Test t2 = ofy.get(Test.class, t.id); //<-- fails with IllegalAccessException: Private fields can not be set on JRE classes.

我在这里做错了什么吗?不支持嵌入字符串列表吗?

Objectify (2.2.3) seems to not want to handle @Embedded lists of strings, although all documentation seems to say that it should be possible.
The strings are handled as if they are custom objects that need to be converted.
The minimal example:

public class Test {
  @Id public Long id = null;
  @Embedded private List<String> strings = new ArrayList<String>();

  private Test() {}

  public Test(String[] in) {
    for (String s : in) {
      strings.add(s);
  }
}

An instance of this class gets saved as:

  Key: 7
  ID/Name: ahpzY2hlZHVsZS13aXRoLXlvdXItZnJpZW5kc3IKCxIEVGVzdBgHDA   
  strings.hash: [0, 0]

Notice that the strings are saved by hash, it being the only non-final field in a String

This code will fail:

ObjectifyService.register(Test.class);
Test t = new Test(new String[] { "aa", "bb" });
Objectify ofy = ObjectifyService.begin();
ofy.put(t);
Test t2 = ofy.get(Test.class, t.id); //<-- fails with IllegalAccessException: Private fields can not be set on JRE classes.

Am I doing something wrong here? Are Embedded Lists of Strings not supported?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-11-09 10:02:51

正如通过 objectify-appengine google group 了解到的那样:简单类型列表不应标记为 @Embedded。如果没有该符号,它们将被保留。 @Embedded 仅适用于复杂的用户类型。文档将被更新以使这一点变得清晰。

As learned via the objectify-appengine google group: Lists of simple types should not be marked @Embedded. They will be persisted without that notation. @Embedded is only for complex user types. The documentation will be updated to make that mare clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文