我有三个实体'A','B','C':
@Entity
public class A implements Serializable {
...
private B b;
@OneToOne(mappedBy = "a", fetch = FetchType.LAZY)
@Cascade({CascadeType.ALL})
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
...
}
@Entity
public class B implements Serializable {
...
private A a;
@OneToOne(fetch = FetchType.LAZY)
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
...
private Collection<C> cCollection;
@OneToMany(mappedBy = "b", fetch = FetchType.LAZY)
public Collection<C> getCCollection() {
return cCollection;
}
public void setCCollection(Collection<C> cCollection) {
this.cCollection = cCollection;
}
...
}
@Entity
public class C implements Serializable {
...
private B b;
@ManyToOne(optional = false, fetch = FetchType.LAZY )
public B getB() {
return b;
}
public void setB(B b) {
this.b= b;
}
}
当我更新B.cCollection -add或删除C对象 - 然后刷新对象a时,我预计这些更改会影响a.getB()。getCCollection()的结果,但它永远不会发生并且cCollection列表赢了'要更新。我刷新()操作错了吗?
//Adding or removal C objects to/from a.getB().getCCollection()
//Persisting changes
myEM.refresh(a);
(注意:我使用的是hibernate和JPA 2.0。在数据库中保存数据没有问题并且真正有效。)
答案 0 :(得分:0)
您应该使用合并方法:http://docs.oracle.com/javaee/5/api/javax/persistence/EntityManager.html
myEM.merge(a);
答案 1 :(得分:0)
“可以使用refresh()方法随时重新加载对象及其所有集合。当数据库触发器用于初始化对象的某些属性时,这很有用。”
“Hibernate从数据库加载多少以及它将使用多少SQL SELECT?这取决于获取策略。”
了解更多详情