0

I have 2-way association between NaturalPerson and Client entity and I would like to persist NaturalPerson while Client is persisted and also inversely persist Client while NaturalPerson is persistent. In order to achieve this i created relationship one-to-one between Client and NaturalPerson where NaturalPerson is owning side and client is inverse side. Next I have added on both side cascade attribute with {CascadeType.PERSIST, CascadeType.REMOVE}. And here is the problem i.e. I'm getting such error

 non-transient entity has a null id

This error happens while I am firstly creating NaturalPerson instance and next Client instance and than setting this NaturalPerson on Client (setting owner on inverse side). But when I'm doing this in opposite direction i.e. firstly setting Client on NaturalPerson and than persisting also only client (inverse side) than unit tests of creating this 2 related entities pass well (everything than seems to work ok)

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • I have tested this and it seems that persisting child (inverse side) with CascadeType.PERSIST doesn't persist parent (owner side) and I don't know whether this is impossible or I'm doing something wrong. – Michał Ziobro Jul 16 '15 at 20:40
  • So why not just persist the same (working) 'side' every time? If you always have a reference to both Entities then persist the one that works. – DuncanKinnear Jul 17 '15 at 01:49
  • Ok I will do this persisting on owning side and will not consider this issue anymore... – Michał Ziobro Jul 17 '15 at 14:34

0 Answers0