休眠:更新 many-tomany 中介 table 删除
Hibernate : Update many-tomany intermediary table on Delete
我是 Hibernate 的新手,不完全理解我应该如何继续更新中介 table。
我在两个 table 之间有一个 ManyToMany 关系:会议和出版物
POJO Publication.class :
private List<Conference> conferences;
...
@ManyToMany(targetEntity = Conference.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinTable(name = "publications_conferences", joinColumns = @JoinColumn(name = "Publications_id"), inverseJoinColumns = @JoinColumn(name = "Conferences_id"))
public List<Conference> getConferences() {
return conferences;
}
public void setConferences(List<Conference> conferences) {
this.conferences = conferences;
}
POJO Conference.class :
private List<Publication> publications;
...
@ManyToMany(targetEntity = Publication.class, mappedBy = "conferences")
public List<Publication> getPublications() {
return publications;
}
public void setPublications(List<Publication> publications) {
this.publications = publications;
}
我的 table“会议”包含重复的记录。我的代码检查两个会议 a、b 是否具有相似的标题并删除 a 或 b。现在,我不想删除中介 table 中的引用(以及记录),而是想以这种方式更新它:
删除会议“b”之前:
|Publications_id|Conferences_id
-------------------------------
c | a
d | b
删除会议“d”后:
|Publications_id|Conferences_id
-------------------------------
c | a
d | a <----- update reference
我尝试了以下代码:
if (answer == 2) {
deleteConferenceQ.setParameter("confId", confIdB);
for (Publication pubB : publicationsB) {
publicationsA.add(pubB);
pubB.getConferences().add(a);
session.save(pubB);
}
int result = deleteConferenceQ.executeUpdate();
tx.commit();
}
但是我收到了 org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
。所以我想知道我做的是否正确。
编辑 #1: 我用 :
替换了之前的代码
如果(答案== 2){
Iterator<Publication> pubBIter = publicationsB.iterator();
while (pubBIter.hasNext()) {
Publication pubB = pubBIter.next();
pubBIter.remove();
pubB.getConferences().remove(b);
b.getPublications().remove(pubB);
pubB.getConferences().add(a);
publicationsB.add(pubB);
}
session.save(a);
session.delete(b);
}
我在 session.save(obj)
时仍然有之前的异常
有人可以帮我吗?谢谢
从 JPA/Hibernate 的角度来看,您甚至不应该考虑联接 table。您只需要维护 @ManyToMany
关系的双方并让 Hibernate 管理数据库。在您的情况下,应该归结为删除一行并从连接 table 添加一行。您的代码应如下所示
Publication pub = ...;
Conference confToBeRemoved = ...;
Conference confToBeAdded = ...;
pub.getConferences().remove(confToBeRemoved); // this implies equals() and hashcode() are properly implemented
confToBeRemoved.getPublications().remove(pub); // same here
pub.getConferences().add(confToBeAdded);
confToBeAdded.getPublications().add(pub);
// save all
我是 Hibernate 的新手,不完全理解我应该如何继续更新中介 table。
我在两个 table 之间有一个 ManyToMany 关系:会议和出版物
POJO Publication.class :
private List<Conference> conferences;
...
@ManyToMany(targetEntity = Conference.class, cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
@JoinTable(name = "publications_conferences", joinColumns = @JoinColumn(name = "Publications_id"), inverseJoinColumns = @JoinColumn(name = "Conferences_id"))
public List<Conference> getConferences() {
return conferences;
}
public void setConferences(List<Conference> conferences) {
this.conferences = conferences;
}
POJO Conference.class :
private List<Publication> publications;
...
@ManyToMany(targetEntity = Publication.class, mappedBy = "conferences")
public List<Publication> getPublications() {
return publications;
}
public void setPublications(List<Publication> publications) {
this.publications = publications;
}
我的 table“会议”包含重复的记录。我的代码检查两个会议 a、b 是否具有相似的标题并删除 a 或 b。现在,我不想删除中介 table 中的引用(以及记录),而是想以这种方式更新它:
删除会议“b”之前:
|Publications_id|Conferences_id
-------------------------------
c | a
d | b
删除会议“d”后:
|Publications_id|Conferences_id
-------------------------------
c | a
d | a <----- update reference
我尝试了以下代码:
if (answer == 2) {
deleteConferenceQ.setParameter("confId", confIdB);
for (Publication pubB : publicationsB) {
publicationsA.add(pubB);
pubB.getConferences().add(a);
session.save(pubB);
}
int result = deleteConferenceQ.executeUpdate();
tx.commit();
}
但是我收到了 org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
。所以我想知道我做的是否正确。
编辑 #1: 我用 :
替换了之前的代码如果(答案== 2){
Iterator<Publication> pubBIter = publicationsB.iterator();
while (pubBIter.hasNext()) {
Publication pubB = pubBIter.next();
pubBIter.remove();
pubB.getConferences().remove(b);
b.getPublications().remove(pubB);
pubB.getConferences().add(a);
publicationsB.add(pubB);
}
session.save(a);
session.delete(b);
}
我在 session.save(obj)
有人可以帮我吗?谢谢
从 JPA/Hibernate 的角度来看,您甚至不应该考虑联接 table。您只需要维护 @ManyToMany
关系的双方并让 Hibernate 管理数据库。在您的情况下,应该归结为删除一行并从连接 table 添加一行。您的代码应如下所示
Publication pub = ...;
Conference confToBeRemoved = ...;
Conference confToBeAdded = ...;
pub.getConferences().remove(confToBeRemoved); // this implies equals() and hashcode() are properly implemented
confToBeRemoved.getPublications().remove(pub); // same here
pub.getConferences().add(confToBeAdded);
confToBeAdded.getPublications().add(pub);
// save all