Java 表单绑定:与中间实体(嵌套实体)的关系-间接实体创建和绑定

Java 表单绑定:与中间实体(嵌套实体)的关系-间接实体创建和绑定,java,jpa,playframework,ebean,playframework-2.2,Java,Jpa,Playframework,Ebean,Playframework 2.2,我的简化模型如下所示: @Entity public class Aspect extends Model { @Id public Long id; @OneToMany(cascade = CascadeType.ALL) public List<Restriction> restrictions; } @Entity public class Restriction extends Model { @Id public Integer id;

我的简化模型如下所示:

@Entity public class Aspect extends Model {
    @Id public Long id;
    @OneToMany(cascade = CascadeType.ALL) public List<Restriction> restrictions;
}

@Entity public class Restriction extends Model {
    @Id public Integer id;
    @ManyToOne public RestrictionTemplate restrictionTemplate;
}

@Entity public class RestrictionTemplate extends Model {
    @Id private Integer id;
}

这里不起作用(在DB中创建方面条目,但没有限制条目)。

我认为您必须为此编写一些代码,在其中搜索与传递的ID对应的
限制模板
,然后将它们分配给
方面
的新实例:

List<RestrictionTemplates> templates = new ArrayList<RestrictionTemplates>();
for (int crtTplId : passedIds) {
    templates.add(entityManager.find(RestrictionTemplates.class, crtTplId));
}


List<Restriction> restrictions = new ArrayList<Restriction>();
for (RestrictionTemplates crtTpl : templates) {
    restrictions.add(new Restriction(crtTpl));
}

Aspect aspect = new Aspect();
aspect.restrictions = restrictions;

entityManager.persist(aspect);

谢谢你的回答!我显然能做到。但当我遵循字段的命名约定时,我希望绑定可以通过播放自动完成。根据这个问题:Play可以从名为“client.customers[0]。address[0]。zipcode”的字段绑定OneToMany->OneToMany嵌套模型。我想知道如何在我的情况下使装订工作(OneToMany->ManyToOne)。
List<RestrictionTemplates> templates = new ArrayList<RestrictionTemplates>();
for (int crtTplId : passedIds) {
    templates.add(entityManager.find(RestrictionTemplates.class, crtTplId));
}


List<Restriction> restrictions = new ArrayList<Restriction>();
for (RestrictionTemplates crtTpl : templates) {
    restrictions.add(new Restriction(crtTpl));
}

Aspect aspect = new Aspect();
aspect.restrictions = restrictions;

entityManager.persist(aspect);
@Entity public class Aspect extends Model {
    @Id public Long id;
    @OneToMany(cascade = CascadeType.ALL, mappedBy="aspect") public List<Restriction> restrictions;
}

@Entity public class Restriction extends Model {
    @Id public Integer id;
    @OneToMany(/*+ add the @JoinColumn for marking the column not nullable*/) public Aspect aspect;
    @ManyToOne public RestrictionTemplate restrictionTemplate;
}