no

Database Initialization Using Code

Go Back to Course Outline Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/tera...


Go Back to Course Outline

Repositories
There are various ways in which we can initialize the database. Mostly for testing purposes I find setting hibernate.ddl-auto to create-drop with CommandLineRunner bean to save entities the most convenient. For this exercise let us use the H2 database, which we already had when we created the project.
[open pom.xml]

Let us run the app and open the URL localhost:8080/h2-console. Here we can see that our entities were auto-generated. This is accessible because we have set spring.h2.console.enabled to true (which is the default) in the application.properties file.
[open application.properties file]

How do we populate the database?
[open LoadDatabase class]

First, the approach we will discuss is by using the CommandLineRunner bean and here is how it looks like. It accepts repository classes as parameters and returns a Lambda function where we define and save the entities.
Let’s take a look again at the h2-console and execute a query.
[open h2-console]



Related

coding 4099952293289713969

Post a Comment Default Comments

item