Hibernate unidirectional OneToMany delete violates constraint ( optional=false at parent side?)

喜欢而已 提交于 2019-12-04 19:29:03

Have you tried

@JoinColumn(name = "Parent_ID", nullable = false)

?

Also, note that attached entities are automatically persistent. You don't need to call saveOrUpdate().

The answer of JB Nizet is working, but with one correction. Since I also have Child.getParentId() method ( not getParent() ), its Column annotation should have nullable=false, insertable=false, updateble=false parameters in addition to nullable=false, updatable=false in Parent.getChildren() association.

With the current configuration Hibernate doesn't know that the Child has to be deleted when you remove it from children collection (it's called orphan removal). You need @OneToMany(orphanRemoval=true) in the parent. org.hibernate.annotations.CascadeType.DELETE only specifies that child should be deleted too when the entire parent is deleted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!