D
D
dn230687kav2016-03-18 19:54:02
Java
dn230687kav, 2016-03-18 19:54:02

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>

DbUnit expected dataset:
<dataset>
<CATEGORY CATEGORY_ID="1"/>
<CATEGORY CATEGORY_ID="2"/>
</dataset>

Java code:
@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 question

Ask a Question

731 491 924 answers to any question