链接实体(OneToMany)未以Play2 Java形式显示

时间:2013-06-19 09:09:25

标签: java forms playframework playframework-2.1 ebean

我在播放时遇到了麻烦! 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();
    }

}

最后Global对象播种数据库:

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);
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

这是由一个糟糕的Play项目引起的,至少清理了Play安装和项目本身的帮助。