1
我是Morphia
的新手,尝试更新对象的现有嵌入式arrayList。这里是我的课:如何更新morphia中的嵌入对象列表?
@Entity
public class Student {
@Embedded private List<Address> address;
private String name;
private Long id;
... getter and setter .. methods
}
@Embedded
public class Address {
private Long customId;
private String name;
... getter and setter .. methods
}
Json
对于上述类:
{
"student":{
"address": [{
"customId": "123456",
"name": "Jack"
}, {
"customId": "78901",
"name": "sam"
}],
"name": "Teacher",
"id" : 1234567890
}
我必须更新address.name
其中address.customId
是78901
。我试图按照Morphia
文档,但找不到任何东西。
我想先删除元素78901
address.customId
然后将数据追加到现有的列表中。为了去除数据我这样做:
UpdateOperations<Student> ops;
Query<Student> updateQuery = datastore.createQuery(Student.class).filter("id", 1234567890);
ops = datastore.createUpdateOperations(Student.class).disableValidation().removeAll("address", new BasicDBObject("customId", 78901));
上面的代码是成功删除预期数据,但我不知道如何将更多的数据添加到现有的列表。任何帮助将是可观的。谢谢
为什么不直接操纵java列表并将实体保存到数据库? – c4k