我在玩 Play 时遇到问题!2 表格,其中包含两个列表(家庭成员和银行账户)的对象(邻里协会成员)被编辑。奇怪的是,表格中正确显示了人员列表,而表格中银行帐户的 textInputs 为空。不过,银行账户的数量是正确的。
我在编辑表单模板中添加了一些调试语句来显示字段的内容。对于与人员列表相关的字段,它显示
BeanList size[2] hasMoreRows[false] list[models.Persoon@1, models.Persoon@2]
而对于它显示的银行账户列表
BeanList deferred
我已经尽可能地减少了应用程序以隔离问题,并将代码推送到git://github.com/janpascal/ledenadmin.git
分支上的 Github () debug-form
。似乎两个列表的创建和处理方式完全相同,但以某种方式显示不同。关系是 Cascade-ALL。只是为了确保我在创建 Person 或 Bankrekening 对象时尝试添加save()
和update()
调用,但这并没有什么不同。
任何帮助是极大的赞赏!
表格:
@(id: Long, myForm: Form[Lid])
@main("Bewerk lid") {
<h1>Bewerk lid</h1>
@myForm
<br>
@myForm.value()
<br>
@myForm.field("personen").value()
<br>
@myForm.field("rekeningnummers").value()
<br>
@helper.form(action = routes.Leden.saveLid(id)) {
<fieldset>
<legend>Lid @id</legend>
@helper.repeat(myForm("personen"), min = 1) { persoonField =>
@helper.inputText(persoonField("name"), '_label -> "Naam" )
}
@helper.inputText(myForm("address"), '_label -> "Adres")
@helper.repeat(myForm("rekeningnummers"), min = 1) { rekeningField =>
@helper.inputText(rekeningField("rekeningnummer"))
}
</fieldset>
<input type="submit" value="Opslaan">
}
}
我的模型:
@Entity
public class Lid extends Model {
@GeneratedValue(strategy=GenerationType.AUTO, generator="lid_seq_gen")
@SequenceGenerator(name="lid_seq_gen", sequenceName="LID_SEQ")
@Id
public Long id;
@OneToMany(cascade=CascadeType.ALL, mappedBy="lid")
public List<Persoon> personen;
public String address;
@OneToMany(cascade=CascadeType.ALL, mappedBy="lid")
public List<Bankrekening> rekeningnummers;
public Lid(Long id, String name, String address, String bankaccount) {
this.id = id;
this.personen = new ArrayList<Persoon>();
this.personen.add(new Persoon(this, name));
this.rekeningnummers = new ArrayList<Bankrekening>();
this.rekeningnummers.add(new Bankrekening(this,bankaccount));
this.address = address;
}
public String toString() {
return "Lid "+id+" ("+getFirstName()+")";
}
public String getFirstName() {
if(personen.size()>=1) return personen.get(0).name;
return "";
}
public Bankrekening addRekening(String rekeningnummer) {
Bankrekening rek = new Bankrekening(this, rekeningnummer);
rekeningnummers.add(rek);
return rek;
}
public static void create(Lid lid) {
lid.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
public static Finder<Long,Lid> find = new Finder<Long, Lid>(
Long.class, Lid.class
);
}
@Entity
public class Persoon extends Model {
@GeneratedValue(strategy=GenerationType.AUTO, generator="persoon_seq_gen")
@SequenceGenerator(name="persoon_seq_gen", sequenceName="PERSOON_SEQ")
@Id
public Long id;
@ManyToOne
public Lid lid;
@Constraints.Required
public String name;
public Persoon(Lid lid, String name) {
this.lid = lid;
this.name = name;
}
public static void create(Persoon p) {
p.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
public static Finder<Long,Persoon> find = new Finder<Long, Persoon>(
Long.class, Persoon.class
);
}
@Entity
public class Bankrekening extends Model {
@GeneratedValue(strategy=GenerationType.AUTO, generator="bankrek_seq_gen")
@SequenceGenerator(name="bankrek_seq_gen", sequenceName="BANKREKENING_SEQ")
@Id
public Long id;
@ManyToOne
public Lid lid;
@Constraints.Required
public String rekeningnummer;
public Bankrekening(Lid lid, String nummer) {
this.lid = lid;
this.rekeningnummer = nummer;
}
public static void create(Bankrekening bankrekening) {
bankrekening.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
public static Finder<Long,Bankrekening> find = new Finder<Long, Bankrekening>(
Long.class, Bankrekening.class
);
}
控制器:
public class Leden extends Controller {
public static Result lijst() {
List<Lid> leden = Lid.find.all();
return ok(ledenlijst.render(leden));
}
public static Result bewerkLid(Long id) {
Form<Lid> myForm = form(Lid.class).fill(Lid.find.byId(id));
Lid lid = Lid.find.byId(id);
System.out.println("Editing "+lid.toString());
System.out.print("Bankrekeningen:");
for(Bankrekening rek: lid.rekeningnummers) {
System.out.print(" "+rek.rekeningnummer);
}
System.out.println();
System.out.println("Form information:");
System.out.println(myForm.value());
System.out.println(myForm);
System.out.println(myForm.data());
return ok(editlid.render(id, myForm));
}
public static Result saveLid(Long id) {
Form<Lid> form = form(Lid.class).bindFromRequest();
if(form.hasErrors()) {
return badRequest(editlid.render(id,form));
}
form.get().update(id);
System.out.println("Form information:");
System.out.println(form.value());
System.out.println(form);
System.out.println(form.data());
Lid lid = form.get();
System.out.println("Updating"+lid.toString());
System.out.print(" Bankrekeningen:");
for(Bankrekening rek: lid.rekeningnummers) {
System.out.print(" "+rek.rekeningnummer);
}
return lijst();
}
}
最后是为数据库播种的全局对象:
public class Global extends GlobalSettings {
@Override
public void onStart(Application app) {
InitialData.insert(app);
}
static class InitialData {
public static void insert(Application app) {
if(Ebean.find(Lid.class).findRowCount() == 0) {
System.out.println("Seeding members");
for(long i=1; i<10; i++) {
Lid lid = new Lid(i, "lid"+i, "Kerkstraat "+i, "Bank account"+i);
//lid.addRekening(new Long(i*5462).toString());
Lid.create(lid);
}
}
}
}
}