alialdev / projects

Projects
0 stars 1 forks source link

Configure Hibernate DDL Auto #3

Closed dvt32 closed 1 month ago

dvt32 commented 1 month ago

Instead of writing specialized SQL to avoid data duplicates on application startup, use Hibernate's ability to generate tables for you and delete data on shutdown.

See here for more info: https://www.jobyme88.com/?st_ai=spring-jpa-hibernate-ddl-auto

ali-al-fntext commented 1 month ago

In my previous setup, Hibernate was already responsible for creating tables, and I used a data.sql scripts not creating tables but just to import sample data into the database.The problem was by default, the data.sql script executes before Hibernate initialization, and as far as I learn, this is a common problem. To address this, I added "spring.jpa.defer-datasource-initialization=true" to application.properties. This modifies the default Spring Boot behavior and populates the data after the schema is generated by Hibernate.

Additionally, I experimented with different configurations of spring.jpa.hibernate.ddl-auto to manage the data deletion during application shutdown. None of the proposed solutions worked as expected when terminating the application in Eclipse. As a workaround, I adjusted data.sql to truncate all tables during initialization before populating them again.