What's wrong with my annotations for the relevant associations (or if that's not the problem, then what is)?

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 on Kiwi and Watermelon. You can encapsulate this by disallowing setKiwis and instead add a addKiwi method which will take care of setting the basket field before adding to the kiwis list. You can do the same thing in the setWatermelon method.
  • flush and clear the EntityManager to get rid of the objects that you didn’t setup properly, or close and create a new EntityManager.
1 Like