Database Initialization Using Script

Go Back to Course Outline Repositories

Go Back to Course Outline

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.

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.


spring-data 7113592552724847600

Post a Comment Default Comments