Answer the question
In order to leave comments, you need to log in
How to correctly save a child entity with a null parent in Hibernate?
CATEGORY_PARENT_ID must be null as a result of an entity with CATEGORY_ID = 2, which is saved, since it comes in this form (CATEGORY_PARENT_ID = null): {"id":2}
What needs to be changed in annotations or in update / merge to achieve the expected result?
DbUnit setup dataset:
<dataset>
<CATEGORY CATEGORY_ID="1"
<CATEGORY CATEGORY_ID="2"
CATEGORY_PARENT_ID="1"/>
</dataset>
<dataset>
<CATEGORY CATEGORY_ID="1"/>
<CATEGORY CATEGORY_ID="2"/>
</dataset>
@OneToOne( cascade = {CascadeType.ALL})
@JoinColumn(name = "CATEGORY_PARENT_ID")
@NotFound(action = NotFoundAction.IGNORE)
private Category parent;
@OneToMany( orphanRemoval = true, mappedBy = "parent", cascade = {CascadeType.ALL})
@NotFound(action = NotFoundAction.IGNORE)
private List<Category> children;
//...
getHibernateTemplate().merge(entity);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question