我在 Hibernate 中有一个多对多的关系。当我将一个objectA 与两个objectB 关联时,objectA.getObjectB() 成功返回这两个元素(包括数据库),但objectB.getObjectA() 不返回objectA。只有在一个可以工作的新会话之后。
@Entity
public class ObjectA implements java.io.Serializable {
private List<ObjectB> objectsB;
...
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "db_objectA_objectB", joinColumns = {
@JoinColumn(name = "idObjectA", updatable = false)}, inverseJoinColumns = {
@JoinColumn(name = "idObjectB", updatable = false)})
public List<ObjectB> getObjectsB() {
return objectsB;
}
public void setObjectsB(List<ObjectB> objectsB) {
this.objectsB = objectsB;
}
}
@Entity
public class ObjectB implements java.io.Serializable {
private List<ObjectA> objectsA;
...
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "objectsB", cascade = CascadeType.ALL)
public List<ObjectA> getObjectsA() {
return objectsA;
}
public void setObjectsA(List<ObjectA> objectsA) {
this.objectsA= objectsA;
}
}