Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate EntityManager refresh()是否导致通过实体多级关系级联同步?_Hibernate_Jpa 2.0_Entitymanager - Fatal编程技术网

Hibernate EntityManager refresh()是否导致通过实体多级关系级联同步?

Hibernate EntityManager refresh()是否导致通过实体多级关系级联同步?,hibernate,jpa-2.0,entitymanager,Hibernate,Jpa 2.0,Entitymanager,我有三个实体‘A’、‘B’、‘C’: 当我更新B.cCollection—添加或删除C对象—然后刷新对象a时,我希望这些更改会影响a.getB().getCCollection()的结果,但这种情况从未发生,cCollection列表也不会更新。我的refresh()操作有错吗 //Adding or removal C objects to/from a.getB().getCCollection() //Persisting changes myEM.refresh(a); (注意:

我有三个实体‘A’、‘B’、‘C’:




当我更新B.cCollection—添加或删除C对象—然后刷新对象a时,我希望这些更改会影响a.getB().getCCollection()的结果,但这种情况从未发生,cCollection列表也不会更新。我的refresh()操作有错吗

//Adding or removal C objects to/from a.getB().getCCollection()
//Persisting changes
myEM.refresh(a);

(注意:我使用hibernate和JPA 2.0。在数据库中持久化数据没有问题,而且工作正常)。

您应该使用合并方法:

可以随时使用refresh()方法重新加载对象及其所有集合。这在使用数据库触发器初始化对象的某些属性时非常有用

“Hibernate从数据库中加载了多少数据,它将使用多少SQL选择?这取决于抓取策略。”


通过访问

了解更多详细信息是的,没有注意到您没有为b.getCCollection()设置级联。您可以在b上设置cascade属性或调用merge。顺便说一句,当您从a.getB()更新C对象时。getCCollection()对象是托管对象吗?从逻辑上讲,如果我刷新“a”,则它的所有基本字段和关系字段都必须刷新!!(如果我错了,请纠正)。因此,当我调用a.getB().getCCollection()时,它必须返回新的c集合(新的持久化集合)。我是否也应该刷新“a”的“b”对象?例如:
a.getB().getCCollection().add(新的C(/*参数*/);myEM.getTransaction().begin();myEM.merge(a.getB());myEM.getTransaction().commit();刷新(a)
refresh只意味着它从数据库而不是会话中检索对象的状态。所以,在调用refresh()之前,您确定您的更改已刷新到数据库中吗?
@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;
    }
}
//Adding or removal C objects to/from a.getB().getCCollection()
//Persisting changes
myEM.refresh(a);
myEM.merge(a);