【发布时间】:2016-12-24 02:12:50
【问题描述】:
我在 Morphia Entity 类中使用继承的 @Embedded ArrayList 引用。
@Entity
public class First {
@Embedded private List<Second> secondClass;
private String title;
private Long id;
... getter and setter .. methods
}
@Embedded
public class Second {
@Embedded private List<Third> thirdClass;
private String titleSecond;
... getter and setter .. methods
}
@Embedded
public class Third {
private String titleThird;
private String logoUrl
... getter and setter .. methods
}
Json
{
"secondClass": [{
"thirdClass": [{
"titleThird": "Java",
"logoUrl": "http://www.artsfon.com/pic/201510//artsfon.com-72885.jpg",
}, {
"titleThird": "ios",
"logoUrl": "http://www.artsfon.com/pic//1920x1080/artsfon.com-72885.jpg",
}],
"titleSecond": "Developer"
}],
"title": "Software Developer",
"id" : 1234567890
}
问题
1) 如何删除给定值为titleSecond和id的元素?
2) 如何删除给定值为titleThird和id的元素?
首先我尝试了以下实现:
UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).filter("id", Long.parseLong(id.trim()));
ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.titleSecond", "Developer");
datastore.update(updateQuery, ops);
但它会抛出映射错误:
Write failed with error code 16837 and error message 'cannot use the part (secondClass of secondClass.titleSecond) to traverse the element.
如何执行问题 1 和 2 的操作?任何帮助都是不言而喻的。
可能链接的问题:In Morphia how can i update one embedded Object inside an ArrayList
【问题讨论】:
-
只是好奇您最初是如何创建此文档的?你能把记录推到三等吗?单独的问题。二等如何与三等联系在一起?三等舱可以和二等舱一样移动吗?
-
只是好奇你最初是如何创建这个文档的 我传递了上面的
JSON对象并使用datastore.save(firstObject)保存。 *你能把记录推到三等吗*:是的,我可以看到MongoDB中的记录。 *二等与三等的关系如何*:我在Second Class中使用了@Embedded private List<Third> thirdClass;。请查看已发布的 JSON (` "thirdClass": [{...}]`)。 *你能把三等班和二等班一样移动吗*实际上,我不能:(。如果我已经解决了你所有的问题,请告诉我? -
其实不是第一次保存。我的问题是,如果您想在现有对象的第三类中添加新条目怎么办?这是你尝试过的吗?
-
还没有,我必须这样做。如果我运行 update the existing in
secondClass会不会是类似的情况?让我先尝试实施您的解决方案。 -
好的。我认为它不一样。至少我在尝试删除三等舱的条目时无法解决这个问题。我会让你试着挤进三等舱。这部分我没试过。
标签: java mongodb playframework morphia