Objectify 无法保存 @Embedded 字符串列表
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如通过 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.