Hibernate unable to delete parent/child self
我试图删除父/子自联接实体,但无法这样做,这是我的映射
@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;
}
For correct parent/child relation modeling you should have modeled the one to many part of relation please find an example:
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="parent_id")
private Menu parent;
@OneToMany(mappedBy="parent",orphanRemoval=true)
private List<Menu> children = new ArrayList<Menu>();
This is an unidirectional link so the owner of the relation would be the parent side of the relation. Now when you issue a EM.delete(parent) or session.delete(parent) the delete will be cascaded by the chain and children will be deleted too ( normally called orphans, by default hibernate will not issue a delete statement on orphans ) so that's why orphanRemoval = true is configured.
上一篇: Hibernate Criterion中的OneToMany关系
下一篇: Hibernate无法删除父/子自我
