Hibernate just returns you the objects of your persistence context if available, which are in your test case. Since these objects have no basket set, you see a null
.
You have two options, but I’d suggest you go with the first one:
- Set the
basket
onKiwi
andWatermelon
. You can encapsulate this by disallowingsetKiwis
and instead add aaddKiwi
method which will take care of setting thebasket
field before adding to thekiwis
list. You can do the same thing in thesetWatermelon
method. flush
andclear
theEntityManager
to get rid of the objects that you didn’t setup properly, orclose
and create a newEntityManager
.