我试过了:
@Entity
public class Test {
@Id
public ObjectId id;
public Date createdAt;
public Document data;
}
插入这样的实体工作正常:
Test t = new Test();
t.createdAt = new Date();
t.data = new Document("qwe", 12).append("wer", asList("123", "345", "dsf"));
datastore.save(t);
但有两个问题:
1)在节目开始时有一条温暖的信息:
06:33:47 [main] WARN org.mongodb.morphia.mapping.validation.MappingValidator - MapKeyDifferentFromString complained about test.Test.data : Maps cannot be keyed by Object (Map<Object,?>); Use a parametrized type that is supported (Map<String/Enum/Long/ObjectId/..., ?>)
2)我无法收回这样的实体datastore.createQuery(Test.class).asList())
产生错误:
Exception in thread "main" java.lang.ClassCastException: org.bson.Document cannot be cast to java.util.List
at org.mongodb.morphia.mapping.DefaultCreator.createList(DefaultCreator.java:131)
at org.mongodb.morphia.converters.IterableConverter.createNewCollection(IterableConverter.java:122)
at org.mongodb.morphia.converters.IterableConverter.decode(IterableConverter.java:32)
at org.mongodb.morphia.converters.Converters.decode(Converters.java:85)
如何在Morphia实体中保留任意嵌套文档?