I just have a small query on whether flush involves automatically persisting all previous entity relationships. Here is what I mean:
shFood.setCuisines(cuisineSelection);
entityManager.flush();
Why does the entityManager automatically persist my shFood entity as well as the cuisine (shFood has a cascade persist with cuisineSelection) entities even without me specifying persist for my shFood?
Would appreciate some clarification!
In this case your shFood is not the new entity instance – instead it is already managed entity. There is no need to call persist for already managed entities to make them managed. This entity stays managed until entity managed is closed or entity is detached from persistence context, for example via detach or clear.
What is happenening in you case:
shFood. This instance will stay managed until it is detached.shFood.setCuisines(cuisineSelection)makes change in persistence contextentityManager.flush()forces entity manager to flush changes made to entities in persistence context to the database. All changes made to managed entities are flushed.