无法修改Morphia中的@Embedded列表

huangapple go评论101阅读模式
英文:

Can't modify @Embedded List in Morphia

问题

我有以下的实体:

  1. @Entity("users")
  2. public class UserModel {
  3. @Id
  4. private ObjectId id;
  5. private String userID;
  6. private String prefix;
  7. private List<TodoList> todoLists;
  8. private List<Reminder> reminders;

TodoList对象如下:

  1. @Embedded
  2. public class TodoList {
  3. private String name;
  4. private List<String> todos;
  5. private List<String> completed;

我尝试的目标是将一个字符串从todos ArrayList 移动到TodoList内的completed ArrayList,下面是我尝试的方法:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. String todo = userData.getTodoLists().get(listIndex).getTodos().remove(todoIndex);
  3. datastore.find(UserModel.class)
  4. .filter(Filters.eq("userID", userData.getId()))
  5. .update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
  6. .execute();
  7. }

但这没有任何作用,我不知道问题出在哪里。即使我简单地修改整个TodoList,将todo从todos ArrayList移动到completed ArrayList,然后使用set UpdateOperator,像这样:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. userData.getTodoLists().get(listIndex).completeTodo(todoIndex);
  3. datastore.find(UserModel.class)
  4. .filter(Filters.eq("userID", userData.getId()))
  5. .update(UpdateOperators.set("todoLists." + listIndex, userData.getTodoLists().get(listIndex)))
  6. .execute();
  7. }

它仍然不起作用,即使我记录了todo变量和userData,它们看起来都正确,我就是不能将其保存到数据库中。

我还尝试了这个方法:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. String todo = userData.getTodoLists().get(listIndex).removeTodo(todoIndex);
  3. System.out.println(todo); // 此处正确记录,所以至少push操作应该有效
  4. datastore.find(UserModel.class)
  5. .filter(Filters.eq("userID", userData.getId()))
  6. .update(UpdateOperators.set("todoLists." + listIndex + ".todos", userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push("todoLists." + listIndex + ".completed", todo))
  7. .execute();
  8. }

其中removeTodo方法如下(在TodoList嵌入类内):

  1. public String removeTodo(int todoIndex) {
  2. return todos.remove(todoIndex);
  3. }
英文:

I have the following Entity:

  1. @Entity(&quot;users&quot;)
  2. public class UserModel {
  3. @Id
  4. private ObjectId id;
  5. private String userID;
  6. private String prefix;
  7. private List&lt;TodoList&gt; todoLists;
  8. private List&lt;Reminder&gt; reminders;

The TodoList Object looks like this:

  1. @Embedded
  2. public class TodoList {
  3. private String name;
  4. private List&lt;String&gt; todos;
  5. private List&lt;String&gt; 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:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. String todo = userData.getTodoLists().get(listIndex).getTodos().remove(todoIndex);
  3. datastore.find(UserModel.class)
  4. .filter(Filters.eq(&quot;userID&quot;, userData.getId()))
  5. .update(UpdateOperators.set(&quot;todoLists.&quot; + listIndex + &quot;.todos&quot;, userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push(&quot;todoLists.&quot; + listIndex + &quot;.completed&quot;, todo))
  6. .execute();
  7. }

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:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. userData.getTodoLists().get(listIndex).completeTodo(todoIndex);
  3. datastore.find(UserModel.class)
  4. .filter(Filters.eq(&quot;userID&quot;, userData.getId()))
  5. .update(UpdateOperators.set(&quot;todoLists.&quot; + listIndex, userData.getTodoLists().get(listIndex)))
  6. .execute();
  7. }

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:

  1. public void completeTodo(int listIndex, int todoIndex, UserModel userData) {
  2. String todo = userData.getTodoLists().get(listIndex).removeTodo(todoIndex);
  3. System.out.println(todo); // This logs correctly, so at least the push operator should work
  4. datastore.find(UserModel.class)
  5. .filter(Filters.eq(&quot;userID&quot;, userData.getId()))
  6. .update(UpdateOperators.set(&quot;todoLists.&quot; + listIndex + &quot;.todos&quot;, userData.getTodoLists().get(listIndex).getTodos()), UpdateOperators.push(&quot;todoLists.&quot; + listIndex + &quot;.completed&quot;, todo))
  7. .execute();
  8. }

where removeTodo is (inside the TodoList Embedded class):

  1. public String removeTodo(int todoIndex) {
  2. return todos.remove(todoIndex);
  3. }

答案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 :/

huangapple
  • 本文由 发表于 2020年10月13日 03:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64324555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定