Converting your server to JPA
In this assignment you are going to transition your Freecycle project from JDBC to JPA. To do this you will replace the original Java classes and JDBC repository classes with Entity classes, service classes, and JPA repository classes.
Here are some things to work on as you restructure the application.
- Update your pom.xml to remove the JDBC dependency and replace it with
spring-boot-starter-data-jpa
- Get rid of your original database and replace it with an empty database with the same name. The new version of your application will use JPA to build the database for you automatically from the entity classes you set up.
- Remember to put the line
spring.jpa.hibernate.ddl-auto = create in your application.properties file the first time you run the application to set up the database. After the first run change this option to from create to update.
- Your old JDBC repository classes contained both application logic and code to interact with the database. In the new version you will split this code between service classes that contain the application logic and JPA repository classes to handle the database interaction.
- Your controller classes will have to be modified to interact with the service classes you create.
- In your controller classes watch out for methods that return entities. In some cases you will discover that when you invoke a controller method that returns an entity the generated JSON you get from the controller is going to be a mess. This is most likely to happen for classes that contain relations to other classes. The fix for this problem is to design a simpler DTO class and have your controller return the DTO instead of the entity. To see an example of this, take a look at how my auction project handles returning lists of Auctions.