Database Initialization Using Script


Go Back to Course Outline

Repositories
There are times when we want to use a SQL script to generate the schema and populate the database. Personally, I use the code which we previously discussed as it triggers notifications which you might have set in entity’s create or update method. But sometimes, you might already have a large chunk of sample data from excel exported from another system that you need to do some processing and that is when this method is useful.

Going back to the project we first need to change the value of spring.jpa.hibernate.ddl-auto to validate.
[open application.properties]

This instructs Spring not to auto-generate the database schema from the entity classes, rather use the schemal.sql and data.sql files located in src/main/resources folder.

Let’s take a look at the SQL files.
[open schema.sql]

Here we defined the tables which mapped to the entity classes.
[open data.sql]

Needless to say, this is where we insert records.

Now let’s run the application. As expected the tables are created and 1 row of a category is inserted.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.