无法修改Morphia中的@Embedded列表

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

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(&quot;users&quot;)
public class UserModel {
   @Id
   private ObjectId id;
   private String userID;
   private String prefix;
   private List&lt;TodoList&gt; todoLists;
   private List&lt;Reminder&gt; reminders;

The TodoList Object looks like this:

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

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(&quot;userID&quot;, userData.getId()))
        .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))
        .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(&quot;userID&quot;, userData.getId()))
        .update(UpdateOperators.set(&quot;todoLists.&quot; + 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(&quot;userID&quot;, userData.getId()))
        .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))
        .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 :/

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:

确定