Learn how to create tables and populate them automatically at application startup.
- [Instructor] In software development many times the best choice when writing code, is to not write the code at all. We are constantly standing on the shoulders of others to advance our own applications. Don't reinvent the wheel for crying out loud! An example of this is how Explore Cali initializes the in-memory database, it instantiates and saves all the tour packages in Explore Cali application. Then it opens a .JSON file and marshals each entry into a tour object and then saves the tour package.
This was done in order to demonstrate the Spring Data Repository Save and Find methods. But there is a better way thanks to Spring Data Repository initialization, let's go to the resources folder, and schema.sql, this is a file that has sql commands in it and it will create the schema. In other words it will actually create the database tables and then data.sql, this is where we insert rows into those tables, and then in application properties we set spring.jpa.hibernate.ddl-auto equal to none.
So with those three settings, Spring Data Repository Initialization will initialize all of our tours and tour packages. So because of that we can go ahead and delete some code. First of all, let's go to our Explore Cali application we no longer need this code within the run method.
We don't need this tours from file private class and so we don't need the run method at all and because of that we don't even need the implement CommandLineRunner or autowire the services. In fact, we can actually delete these services.
We can delete TourService, and we can delete TourPackageService and we can delete the JSON file.
There's something in here that's complaining. Let's build it and see if we've got any other bugs we need to take care of. Ah, so we have a static import we can remove as well. Ah now the compiler's happy, now to make this work, there is one thing we do need to add.
So you see that there is this enum Region and we have the Central_Coast which maps to a String Central Coast as well as Southern_California which maps to Southern California String and Region is a VARCHAR of 20, so I need to have a converter class that when the import of the data from the data.sql is read, we need to convert that String Central Coast into the proper Region enum.
So within the domain folder I'll create a new class let's call it RegionConverter. So RegionConverter implements AttributeConverter and we're going to convert a Region enum to a String, and now that we have implemented that, we need to implement the methods.
So we need to convert to a database column from a region and we need to converToEntityAttribute from a String. So let's implement that code, so we passed in a Region so you want to return_region.getLabel and we're passed in a String we want to say Region findByLabel and then passing in the String and then lastly we need to have a annotation.
So now whenever I run the application, Spring Data will run the schema.sql, import the data.sql and automatically convert the Strings to the enum. So let's rebuild it to make sure that everything is just right, build successfully and now let's run it.
So Tomcat started successfully, that did the mapping and let's go to Tomcat and make sure we can see the tours and we call a get of all the packages and we see all the tour packages and send it again and we got all the tours. So it still works!
- Enhancing a Spring Boot microservice
- Hardening the microservice
- Configuring security with JSON web tokens
- Leveraging Docker for MySQL database access
- Dockerizing your microservice