Hi, I did some research on finding the best approach for database initialization and data management.
SpringBoot recommends any of the following ways to initialize the database,
- JPA, Hibernate, basic SQL scripts
There is no logical difference between these approaches. And the approach that one should use completely depend on the technologies of the project itself.
But however, since the main reason we are trying to use basic SQL scripts is the issue with Cascading, I did some further digging to check if it’s recommended to use the Cascading on the script itself.
And the approach is not recommended because
Hibernate doesn’t know which records the cascading operation on the database removes. It can’t remove any entity that maps one of the removed records from the 1st or 2nd level cache. Due to this, you are at a high risk that your caches contain outdated information (records that no longer exist).
In other words, the SpringBoot database cache can get invalidated and we will be forced not to use the EntityManager
.
So the best approach would be as follows,
- Let the database initialization through JPA Entities without adding Cascading to any of the
to many
relationships. - When a bulk removal is needed(Eg: Removing children of a parent) write native SQL scripts in the Repository to provide the functionality.
This approach will solve the above mentioned caching problem and the following 2 problems,
- When using JPA delete with Cascade for a
to many
relationships, a huge number of SQL scripts will be executed. - When using JPA delete with Cascade for a
many to many
relationships, a chain removal might occur and might end up removing all the data.
References:
i. https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/html/howto.html#howto-database-initialization
ii. https://thorben-janssen.com/avoid-cascadetype-delete-many-assocations/
WDYT about the above approach?