超然的实体通过坚持,把我的生活变成地狱,自从我遇到这个问题已经两天了,我已经在这个网站上尝试了所有的东西,但没有任何效果。所以我有两个表“模块”和“认证”,我有一个模块标题的列表,我正在做的是取这个标题并检索模块,对于每个模块,我想把我已经创建的认证。在我的示例中使用ManyToMany relationship
所以我有以下几门课:
级认证:
@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
}这就是我在我的豆子里使用的方法
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";
}类modulesService,创建方法:
public void create(E object) throws Exception {
em.clear();
em.persist(object);
em.flush();
}发布于 2016-06-21 19:34:19
我通过删除@GeneratedValue注释来解决这个问题。看来我没有别的选择了(汽车或身份)。
因此,我所做的是:检索表的最大id,将其增加一个,然后手动将其设置为我的实体。当我持久化这个对象的时候,它就完美地工作了。
https://stackoverflow.com/questions/37909490
复制相似问题