cos*_*lev 5 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;
@ManyToOne public RestrictionTemplate restrictionTemplate;
}
@Entity public class RestrictionTemplate extends Model {
@Id private Integer id;
}
Run Code Online (Sandbox Code Playgroud)
基本上这个想法是这样的:每个Aspect都有一组限制.每个限制本身都依赖于RestrictionTemplate.
我希望Aspect创建表单是这样的:用户可以选择一些RestrictionTemplates,并在表单上提交新的限制应该创建并与新的Aspect相关联.
让我再次解释一下:在表单提交上,我想根据提供的RestrictionTemplate ids 创建Aspect和相关限制.
表格中的字段应该具有哪些名称才能使这种绑定成为可能?
适用于直接关系的命名:
restrictions[0].restrictionTemplate.id
restrictions[1].restrictionTemplate.id
Run Code Online (Sandbox Code Playgroud)
在这里不起作用(在DB中创建Aspect条目,但没有限制条目).
我认为您只需为此编写一些代码,在其中搜索与RestrictionTemplates传递的 ID 相对应的内容,然后将它们分配给 的新实例Aspect:
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);
Run Code Online (Sandbox Code Playgroud)
PS:据我了解,可能存在不属于某个方面的限制。如果情况并非如此,那么您应该使方面限制关系成为双边关系,Restriction成为拥有方:
@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |