ManyToMany-Relation中的空列表(不是表) [英] Empty List (not Table) at ManyToMany-Relation
问题描述
我遇到了与空表相同的问题在jpa中使用多对多关系时.遗憾的是,该帖子没有解决方案.我在JPA/EclipseLink v2.6.3中有一个具有多对多关系的类.这里是角色类:
i faced the same problem as empty tables when using many to many relations in jpa. Sadly this post was without solution. I have a class with a many-to-many relation in JPA/EclipseLink v2.6.3. Here the class ROLE:
@Entity
public class Role implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinTable(name = "ROLE_COMPETENCE", joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "COMPETENCE_ID", referencedColumnName = "ID"))
private List<Competence> competences = new ArrayList<Competence>();
@Override
public String toString() {
return "Role [id=" + id + "]";
}
public void addCompetence(Competence competence) {
if (this.competences != null) {
if (!this.competences.contains(competence)) {
this.competences.add(competence);
competence.addRole(this);
}
}
}
public int getId() {
return id;
}
public Role() {
super();
}
public List<Competence> getCompetences() {
return competences;
}
}
还有班级
@Entity
public class Competence implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(length = 1024, nullable = false)
private String title;
@JsonIgnore
@ManyToMany(mappedBy = "competences", fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
private List<Role> roles;
public int getId() {
return id;
}
public Competence() {
super();
}
public void addRole(Role role) {
if (this.roles != null) {
if (!this.roles.contains(role)) {
this.roles.add(role);
role.addCompetence(this);
}
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Role> getRoles() {
return roles;
}
@Override
public String toString() {
return "Competence [id="+id+", title="+title+"]";
}
}
当我现在使用
for (int i = 0; i < 10; i++) {
Role role = new Role();
RoleService.create(role);
for (int j = 0; j < 3; j++) {
Competence c = new Competence();
c.setTitle("Competence "+i+"."+j);
CompetenceService.create(c);
lastCompetence = c;
role.addCompetence(c);
}
RoleService.update(role);
}
与
public void x(Object object) {
EntityManager em = ...
EntityTransaction tx = em.getTransaction();
tx.begin();
em.merge(object); //for x=update or em.persist(object) for x=create
tx.commit();
em.close();
}
奇怪的是,当我添加或删除对象时,联接表ROLE_COMPETENCE是正确的.当我加载角色时,可以显示它们的能力.但是,当我从数据库中加载能力时,角色"列表为空.
The strange thing is that the join-table ROLE_COMPETENCE is correct when i add or remove objects. When i load ROLE's they have competences an can be shown. But the list "roles" is empty when i load competences from the database.
有什么主意吗?
推荐答案
我发现了我的错误.只是列表没有实例化.使用List<Role> roles = new ArrayList<Role>();
,一切正常.
I found my mistake. It was just that the list was not instantiated. With List<Role> roles = new ArrayList<Role>();
everything works fine.
这篇关于ManyToMany-Relation中的空列表(不是表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!