无法在 Morphia 中使用 @Embedded 来处理对象列表。

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

Can't use @Embedded for a list of Objects Morphia

问题

以下是翻译的内容:

这是我拥有的主要文档类型:

@Entity("users")
public class UserModel {
    @Id
    private ObjectId id;
    private String userID;
    private String prefix;
    @Embedded
    private List<TodoList> todoLists;
    @Embedded
    private List<Reminder> reminders;
    // 获取器,设置器,构造函数
}

这是代替的TodoList类:

@Embedded
public class TodoList {
    @Id
    private ObjectId id;
    private String name;
    private List<String> todos;
    private List<String> completed;
}

对于List的@Embedded注解,我得到了以下错误:

UserModel.java:18: 错误: 注解类型不适用于此类型的声明
@Embedded

我真的不明白我做错了什么…

(我正在使用Morphia 2.0.1,甚至将一个简单的TodoList类放在这些类的列表中也会引发该错误)

英文:

This is the main Document type I have

@Entity(&quot;users&quot;)
public class UserModel {
   @Id
   private ObjectId id;
   private String userID;
   private String prefix;
   @Embedded
   private List&lt;TodoList&gt; todoLists;
   @Embedded
   private List&lt;Reminder&gt; reminders;
// Getters, setters, constructor

This is the TodoList class instead:

@Embedded
public class TodoList {
    @Id
    private ObjectId id;
    private String name;
    private List&lt;String&gt; todos;
    private List&lt;String&gt; completed;

I get this error for the @Embedded annotation of the List<TodoList>:

UserModel.java:18: error: annotation type not applicable to this kind of declaration
   @Embedded

I can't really understand what I'm doing wrong...

(I'm using Morphia 2.0.1 and even putting a simple TodoList class instead of a list of those classes throws that error)

答案1

得分: 2

刚刚发现 @Embedded 不能用于字段,只能用于类上。所以我只需将它从 UserModel 中移除,保留在 TodoList 类中。

英文:

Just found out that @Embedded is not usable on fields, but just on classes. So I just had to simply remove it from the UserModel and keep it in the TodoList class.

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

发表评论

匿名网友

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

确定