我试图在play1.2.4中移植到YABE来播放2.0.4 Java。为此,我创建了一个initial-data.yml并尝试使用Global.java加载它
#Users users: - !!models.User email: bob@gmail.com password: secret fullname: Bob isAdmin: true #Posts posts: - !!models.Post id: 1 title: About the model layer postedAt: 2009-06-14 author: - !!models.User email: bob@gmail.com content: > The model has a central position in a Play! application. Cut....
My Global.java位于
之下public void onStart(Application app) {
InitialData.insert(app);
}
static class InitialData {
public static void insert(Application app) {
if(Ebean.find(User.class).findRowCount() == 0) {
Map<String, List<Object>> all = (Map<String, List<Object>>)Yaml.load("initial-data.yml");
Ebean.save(all.get("users"));
Ebean.save(all.get("posts"));
Ebean.save(all.get("comments"));
}
}
}
加载时我遇到以下错误
ConstructorException:null;无法为标记构造java对象:yaml.org,2002:models.Post; exception =无法为JavaBean = model.Post@1创建property = author;没有为类模型找到3个参数的合适构造函数。用户
org.yaml.snakeyaml.constructor.ConstructorException: null; Can't construct a java object for tag:yaml.org,2002:models.Post; exception=Cannot create property=author for JavaBean=models.Post@1; No suitable constructor with 3 arguments found for class models.User org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:333) org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183) org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:277) org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:248) org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeConstructor.java:440) org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183) org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:326) org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:143) org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:307) org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:459) org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183) org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:142) org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:128) org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480) org.yaml.snakeyaml.Yaml.load(Yaml.java:411)
任何想法为什么会这样?
答案 0 :(得分:4)
我与YAML合作已经有一段时间了,但看起来好像你是在帖子中重新创建一个新用户,而不是引用Bob。
我会尝试这样的事情:
models.User(bob):
email: bob@gmail.com
password: secret
fullname: Bob
isAdmin: true
models.Post(firstPost):
id: 1
title: About the model layer
postedAt: 2009-06-14
author: bob
content: The model has a central position in a Play! application.
答案 1 :(得分:4)
当我稍微修改YAML时,我得到它的工作,我只是删除了“ - ”,它位于“model.class”前面
#Users users: - !!models.User email: bob@gmail.com password: secret fullname: Bob isAdmin: true #Posts posts: - !!models.Post id: 1 title: About the model layer postedAt: 2009-06-14 author: !!models.User email: bob@gmail.com content: > The model has a central position in a Play! application. Cut....
知道“ - ”代表什么?