Skip to content
Advertisement

Deleting an entity with one to one relation

My two entities have one to one relation

JavaScript

I tried to delete my user entity by this method

JavaScript

PasswordResetTokenRepository class which method I called in my service method, for deleting user I used regular hibernate method deleteById(Long id)

JavaScript

But when I try to delete by this method I got this error: not-null property references a null or transient value : kpi.diploma.ovcharenko.entity.user.PasswordResetToken.user I read several websites how to delete one to one relation, but their advices didn’t help me. For example, I tried a lot of variants of annotation cascade={CascadeType.ALL}, tried all the variants(CascadeType.REMOVE,CascadeType.PERSIST and so on), all time I got the same error. Help me pls, to understand what I do wrong.

Advertisement

Answer

try this:

@OneToOne(cascade = CascadeType.REMOVE, orphanRemoval = true)

Here is complete explication .

Advertisement