英文:
Can't modify @Embedded List in Morphia
问题
我有以下的实体:
@Entity("users")
public class UserModel {
@Id
private ObjectId id;
private String userID;
private String prefix;
private List<TodoList> todoLists;
private List<Reminder> reminders;
TodoList对象如下:
@Embedded
public class TodoList {
private String name;
private List<String> todos;
private List<String> completed;
我尝试的目标是将一个字符串从todos ArrayList 移动到TodoList内的completed ArrayList,下面是我尝试的方法:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
String todo = userData.getTodoLists().get(listIndex).getTodos().remove(todoIndex);
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
.execute();
}
但这没有任何作用,我不知道问题出在哪里。即使我简单地修改整个TodoList,将todo从todos ArrayList移动到completed ArrayList,然后使用set UpdateOperator,像这样:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
userData.getTodoLists().get(listIndex).completeTodo(todoIndex);
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex, userData.getTodoLists().get(listIndex)))
.execute();
}
它仍然不起作用,即使我记录了todo变量和userData,它们看起来都正确,我就是不能将其保存到数据库中。
我还尝试了这个方法:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
String todo = userData.getTodoLists().get(listIndex).removeTodo(todoIndex);
System.out.println(todo); // 此处正确记录,所以至少push操作应该有效
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
.execute();
}
其中removeTodo方法如下(在TodoList嵌入类内):
public String removeTodo(int todoIndex) {
return todos.remove(todoIndex);
}
英文:
I have the following Entity:
@Entity("users")
public class UserModel {
@Id
private ObjectId id;
private String userID;
private String prefix;
private List<TodoList> todoLists;
private List<Reminder> reminders;
The TodoList Object looks like this:
@Embedded
public class TodoList {
private String name;
private List<String> todos;
private List<String> completed;
What I'm trying to do is to move a String from the todos ArrayList to the completed ArrayList inside a TodoList,
here is how I'm trying to do that:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
String todo = userData.getTodoLists().get(listIndex).getTodos().remove(todoIndex);
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
.execute();
}
This doesn't do anything and I have no clue of what could be wrong. Also, if I simply modify the full TodoList, moving the todo from the todos ArrayList to the completed ArrayList, and then using the set UpdateOperator like so:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
userData.getTodoLists().get(listIndex).completeTodo(todoIndex);
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex, userData.getTodoLists().get(listIndex)))
.execute();
}
It still doesn't work, even thought I logged the todo variable and the userData and it all looks correct, I just can't manage to save it into the DB.
I also tried this:
public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
String todo = userData.getTodoLists().get(listIndex).removeTodo(todoIndex);
System.out.println(todo); // This logs correctly, so at least the push operator should work
datastore.find(UserModel.class)
.filter(Filters.eq("userID", userData.getId()))
.update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
.execute();
}
where removeTodo is (inside the TodoList Embedded class):
public String removeTodo(int todoIndex) {
return todos.remove(todoIndex);
}
答案1
得分: 0
我应该检查我的代码两次。看起来 userData.getId()
不是数据库中用户的正确 ID :/
英文:
Well I should have checked my code twice. It appears that userData.getId() wasn't the right ID of the user in the database :/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论