2

我在商业项目中使用 Open JPA,并希望利用级联的 Parent->Child 删除和合并。

我模拟了一个显示问题的工作代码。

我有一个带有一些孩子的持久父对象。我正在消除其中一个孩子的关系并将分离的父母传递给合并。提交事务时,会发出 UPDATE 语句,尝试使用 NULL 外键更新孤儿。

@Entity
public class Parent implements Serializable {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
        private String desc;
    //@ElementDependent
    @OneToMany(mappedBy="parent", 
        cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}/*,orphanRemoval=true, fetch=FetchType.EAGER*/)
        private List<Child> childs = new ArrayList<Child>();

@Entity
public class Child implements Serializable {
    @Id private String desc;

    @ManyToOne
    private Parent parent;


    public class StackOverflowTest extends TestCase {
        private EntityManager em;
        private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("SSICTest", System.getProperties());
        private Parent p;
        private Child c;
        private Child c2;

        public void testRemove() {
            prepareObjects();

            startTr();
            em.persist(p);
            commitTr();

            startTr();
            p = em.find(Parent.class, p.getId());
            em.remove(p);
            commitTr();
        }   
        public void testMerge() {
            prepareObjects();

            startTr();
            em.persist(p);
            commitTr();

            //remove on detached
            Child child = p.getChilds().get(0);
            p.getChilds().remove(child);
            child.setParent(null);
            startTr();

            em.merge(p);
            commitTr();

            startTr();
            p = em.find(Parent.class, p.getId());
            assertEquals(1, p.getChilds().size());
            commitTr();
        }
        protected void prepareObjects() {
            p = new Parent();       
            c = new Child();
            c2 = new Child();
            p.setDesc("desc");
            c.setDesc(Math.random()+"");
            c2.setDesc(Math.random()+"");
            p.getChilds().add(c);
            c.setParent(p);
            p.getChilds().add(c2);
            c2.setParent(p);
        }   
        void commitTr() {
            em.getTransaction().commit();
            em.close();
        }
        void startTr() {
            em = factory.createEntityManager();
            em.getTransaction().begin();
        }
    }

在上面的示例中,testRemove 工作正常,但 testMerge 方法不行,正如我在顶部所描述的那样。

如果我删除对 @ElementDependent 的评论,它的工作方式会有所不同。

testRemove 失败,因为 remove 没有级联到 Child 和 db 抛出的引用完整性异常,并且 testMerge 很好。

orphanRemoval=true, fetch=FetchType.EAGER 或 @ForeignKey(deleteAction=ForeignKeyAction.CASCADE) 对孩子的反比关系也无济于事。

请指教。我真的很感谢你的帮助!!

4

1 回答 1

0
 @OneToMany(mappedBy="parent", 
    cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE},
    orphanRemoval=true, fetch=FetchType.EAGER)
 private List<Child> childs = new ArrayList<Child>();

 @ManyToOne
 private Parent parent;

将 orphanRemoval 设置为 true。查看 orphanRemoval 的目的

于 2012-11-24T08:48:44.713 回答