我正在使用 initial-data.yml 将一组示例数据导入到我在 Play 2.0.1 上运行的应用程序中。我正在尝试加载成员、计划和成员资格。成员和方案导入正常,但成员(具有成员和方案的外键)只导入列表中的最后一个条目。
这是我的 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 中的插入方法:
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 中的所有会员数据?