Repository – JPA

Spring Data JPA has a compelling feature: the ability to create repository implementations from a repository interface automatically at runtime.

If you create a custom repository that extends the org.springframework.data.repository.CrudRepository interface, you can inherit several methods to work with your entity persistence, including methods for saving, deleting, and finding your entities.

So, let's start to create our custom repository implementation.

Build a new repository interface, named FootballPlayerRepository, under the com.packtpub.springboot.footballplayermicroservice.repository package, and remember to extend the org.springframework.data.repository.CrudRepository interface:

import org.springframework.data.repository.CrudRepository;

/**
* FootballPlayerRepository extends the CrudRepository interface.

* The type of entity and ID that it works with, FootballPlayer and Integer, are

* specified in the generic parameters on CrudRepository.
* By extending CrudRepository, FootballPlayerRepository inherits several

* methods for working with FootballPlayer persistence, including methods for

* saving, deleting, and finding FootballPlayer entities.
*
* @author Mauro Vocale
* @version 30/09/2018
*/
public interface FootballPlayerRepository extends CrudRepository<FootballPlayer, Integer> {

}

To complete managing the database access operations, we need to create the files that are needed to build the database schema and to populate the tables. We will use the Spring Boot database initialization naming convention to do it; thus, we will create the following files under the src/main/resources directory:

  • schema.sql: The file with the table creation SQL commands
  • data.sql: The file with the SQL insert data commands

In this case, we will not configure the persistence.xml file, like we did for the Java EE/Jakarta EE project, since all of the required information is inside the application.properties and *.sql files.

Now run the application again, using the following command:

$ mvn spring-boot:run

You will notice that the SQL creation schema and load data scripts were performed by the embedded Tomcat run by Spring Boot, as shown in the following code snippet:

2018-09-30 00:20:46.424 INFO 7148 --- [ost-startStop-1] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/Users/mvocale/Progetti/Hands_on_Cloud_Native_Microservices_with_Java_EE/appendix-A/football-player-microservice/target/classes/schema.sql]
2018-09-30 00:20:46.433 INFO 7148 --- [ost-startStop-1] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/Users/mvocale/Progetti/Hands_on_Cloud_Native_Microservices_with_Java_EE/appendix-A/football-player-microservice/target/classes/schema.sql] in 9 ms.
2018-09-30 00:20:46.437 INFO 7148 --- [ost-startStop-1] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/Users/mvocale/Progetti/Hands_on_Cloud_Native_Microservices_with_Java_EE/appendix-A/football-player-microservice/target/classes/data.sql]
2018-09-30 00:20:46.467 INFO 7148 --- [ost-startStop-1] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/Users/mvocale/Progetti/Hands_on_Cloud_Native_Microservices_with_Java_EE/appendix-A/football-player-microservice/target/classes/data.sql] in 30 ms.

You can also verify the database status with a simple query—for example, SELECT * FROM football_player, which must return all of the values preloaded with the data.sql file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset