I have a spring application and use JPA with a sql db. In my application the data model is very complex, but I will use this example as it shows everything I need: Parent 1:n Child 1:n Grandchildren.
My Goal: Everytime a child or grandchild gets an update, give me a list of all linked parents. To achieve this, I want to load the parent from the database instead of navigating with getters, for performance reasons.
What I already did: I have an EntityListener with a preUpdate method. Within this method I call the repository with: parentRepository.findParentsByChild_Id()/findParentsByGrandchild_Id() (Both with their respective queries)
What happens: Let's say I update a child, so preUpdate with the child entity is called. Now I execute findParentsByChild_Id() and by doing this, preUpdate with the same child entity is called again. So I end up in an endless loop. Same case with updating a grandchild.
At first, I thought the entity has to be saved first so I tried post-update, with the same result.
Now I just can't understand why pre/post-update is called when I use the updated entity id in a findBy operation in a repository of a different entity.