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.
- Spring Repository Unit Testing
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactAlright. Now that we have a set of repository classes. I believe it’s a good time to introduce unit testing. Remember,...
- Database Initialization Using Liquibase
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactAfter you have finished running some tests using either the code or script base database initialization it’s now...
- Database Initialization Using Code
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactThere are various ways in which we can initialize the database. Mostly for testing purposes I find setting hibernate.ddl-auto...
- Working With Spring Data Repositories
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactJust like what we did with the entities the same approach can be used when creating the repository classes. And that...
- Create A Restful Web Service In Spring
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactTo prepare the project from future development, we will be creating a multi-maven project layout. Unfortunately,...