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

时间:2012-09-05 05:55:57

标签: hibernate jpa-2.0 entitymanager

我有三个实体'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。在数据库中保存数据没有问题并且真正有效。)

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

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

“Hibernate从数据库加载多少以及它将使用多少SQL SELECT?这取决于获取策略。”

访问Chapter 10: Working with Objects

了解更多详情