Hibernate无法删除父/子自我

我试图删除父/子自联接实体,但无法这样做,这是我的映射

@Entity
public class FolderNode  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "hibernate_sequence")
    @SequenceGenerator(name = "hibernate_sequence", sequenceName = "hibernate_sequence")
    @Column(name="folder_id")
    private long folderId;


    @ManyToOne
    @Cascade(CascadeType.DELETE)
    @JoinColumn(name="parent_id")
    @OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
    private FolderNode parent;

}

对于正确的父母/孩子关系建模,你应该建立一个关系的很多部分,请找到一个例子:

    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="parent_id")
    private Menu parent;
    @OneToMany(mappedBy="parent",orphanRemoval=true)
    private List<Menu> children = new ArrayList<Menu>();

这是一个单向链接,因此关系的所有者将是关系的父方。 现在当你发出一个EM.delete(parent)session.delete(parent)delete将被级联链接并且孩子也将被删除(通常称为孤儿,默认情况下,hibernate不会在孤儿身上发出删除语句)这就是配置orphanRemoval = true的原因。

链接地址: http://www.djcxy.com/p/37039.html

上一篇: Hibernate unable to delete parent/child self

下一篇: 1:M relationship in Hibernate and cascading operations