gpt4 book ai didi

java - 表单绑定(bind) : relationship with intermediate entity (nested entity) - indirect Entity creation and binding

转载 作者:搜寻专家 更新时间:2023-10-31 20:28:34 25 4
gpt4 key购买 nike

我的简化模型如下所示:

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

基本上这个想法是这样的:每个 Aspect 都有一组 Restrictions。每个Restriction 本身都依赖于一个RestrictionTemplate

我希望 Aspect 创建表单像这样:用户可以选择一些 RestrictionTemplates 并在表单上提交新的 Restrictions 应该被创建并与新的方面

让我再次解释一下: 在提交表单时,我想创建Aspect 并基于RestrictionTemplate 的 相关Restrictions> 提供了 ID。

为了使这种绑定(bind)成为可能,表单中的字段应该有什么名称?

适用于直接关系的命名:

restrictions[0].restrictionTemplate.id
restrictions[1].restrictionTemplate.id

在这里不起作用(在数据库中创建 Aspect 条目,但没有 Restriction 条目)。

最佳答案

我认为您必须为此编写一些代码,在其中搜索 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);

PS:据我了解,可能存在不属于方面的限制。如果那不是真的,那么你应该使你的 Aspect-Restriction 关系成为双边关系,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;
}

关于java - 表单绑定(bind) : relationship with intermediate entity (nested entity) - indirect Entity creation and binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21717252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com