传递的独立实体将我的生活变成了地狱,自从我遇到这个问题以来已经两天了,我尝试了本网站中的所有内容,没有任何可用。因此,我有两个表"模块"one_answers"证明",并且我有一个模块标题的列表,而我正在做的是拿起该标题并检索模块,对于每个模块,我想提出我已经创建的证明。 在我的示例中,我正在使用Manytomany关系
所以我有以下这些课程:
classe证明:
@Entity
@Table(name = "edep_attestations")
public class Attestation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
...
@ManyToMany(mappedBy="attestations")
private Set<Module> modules = new HashSet<>();
// getters and setters
}
classe模块:
@Entity
@Table(name = "edep_modules")
public class Module implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
private String title;
...
@ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE})
private Set<Attestation> attestations = new HashSet<>();
// getters and setters
}
这是我在bean中使用的方法
public String create(){
Module m = null;
this.attestationsService.create(attestation);
for (String title : moduleTitles) {
m = modulesService.getModulebyTitle(title);
m.getAttestation().add(attestation);
this.modulesService.create(m);
}
return "success";
}
类ModuleService,创建方法:
public void create(E object) throws Exception {
em.clear();
em.persist(object);
em.flush();
}
我通过删除@GeneratedValue
注释解决了这一点。看来没有其他选择对我有用(自动或身份)。
因此,我要做的是:我检索表格的最大ID,将其递增,然后手动将其设置为我的实体。然后,当我坚持对象时,它正常工作。