我使用initial-data.yml将一组示例数据导入到在Play 2.0.1上运行的应用程序中。我正在尝试加载成员,计划和成员资格。成员和方案正在导入OK,但成员资格(具有成员和方案的外键)只会导入列表中的最后一个条目。
这是我的initial-data.yml文件:
# Members
members:
- !!models.Member
email: mark@zucker.com
name: Mark Zucker
created: null
last_updated: null
- !!models.Member
email: vickysmith@email.com
name: Vicky Smith
created: null
last_updated: null
- !!models.Member
email: sjones@yahoo.com
name: Simon Jones
created: null
last_updated: null
- !!models.Member
email: steve@james.com
name: Steve James
created: null
last_updated: null
# Schemes
schemes:
- !!models.Scheme
id: 1
name: Walmart
- !!models.Scheme
id: 2
name: Amazon
- !!models.Scheme
id: 3
name: Outdoors Living
# Memberships
memberships:
- !!models.Membership
membership_id: 1234567890
accrued: 2000
balance: 1000
joined_by: !!models.Member
id: 1
scheme: !!models.Scheme
id: 1
memberships:
- !!models.Membership
membership_id: 6543210000
accrued: 10000
balance: 10000
joined_by: !!models.Member
id: 2
scheme: !!models.Scheme
id: 1
以下是我的模型定义的相关部分:
package models;
import java.util.*;
import play.db.ebean.*;
import play.data.Form;
import play.data.validation.Constraints.*;
import javax.persistence.*;
import models.Membership;
@Entity
public class Member extends Model {
@Id
public Long id;
@Required
public String email;
public String name;
public Date created;
public Date last_updated;
以下是方案模型的一部分:
@Entity
public class Scheme extends Model {
@Id
public Long id;
@Required
public String name;
以下是会员模式:
@Entity
public class Membership extends Model {
@Id
public Long membership_id;
@Required
@ManyToOne
public Member joined_by;
@Required
@ManyToOne
public Scheme scheme;
public Long accrued;
public Long balance;
这是Global.java中的insert方法:
public static void insert(Application app) {
Map<String,List<Object>> all = (Map<String,List<Object>>)Yaml.load("initial-data.yml");
if(Ebean.find(Member.class).findRowCount() == 0) {
Logger.info("Loading members from initial-data.yml");
Ebean.save(all.get("members"));
}
if(Ebean.find(Scheme.class).findRowCount() == 0) {
Logger.info("Loading schemes from initial-data.yml");
Ebean.save(all.get("schemes"));
}
if(Ebean.find(Membership.class).findRowCount() == 0) {
Logger.info("Loading memberships from initial-data.yml");
Ebean.save(all.get("memberships"));
}
Logger.info("Loaded "+Ebean.find(Member.class).findRowCount()+" members");
Logger.info("Loaded "+Ebean.find(Scheme.class).findRowCount()+" schemes");
Logger.info("Loaded "+Ebean.find(Membership.class).findRowCount()+" memberships");
}
我已经切换登录以计算插入每个表的行数,这是输出:
DEBUG - select count(*)
from member t0
INFO - Loaded 4 members
DEBUG - select count(*)
from scheme t0
INFO - Loaded 3 schemes
DEBUG - select count(*)
from membership t0
INFO - Loaded 1 memberships
如您所见,所有成员和方案都已加载,但只加载了一个成员资格。这始终是列表中的最后一个(因此上面的initial-data.yml文件中的成员资格为6543210000)。如果我交换条目,它仍然是最后一个!
我需要更改什么来加载initial-data.yml中的所有成员资格数据?
答案 0 :(得分:3)
在#Memberships下的initial-data.yml中,你有两个'成员资格:'所以看起来第二个定义是被处理的那个。删除第二个成员资格
答案 1 :(得分:1)
在ebean的yaml引导文件中处理多个关系的正确方法是创建引用。
以下是您要引用的对象:
- !!models.User &ref
mail: b@b.fr
firstName: bbb
lastName: BBB
&amp; ref注释是此处引用的对象的别名。
现在您可以参考它:
- !!models.book
owner: *ref
title: myBook
* ref符号代表&#34;与参考ref&#34;相关联的对象。这本书的主人将是bbb!