CREATING A JPA REPOSITORY Using Spring Data JPA Repository in a Spring Boot Microservice Prepared by Rachit Joshi 1. Introduction A JPA Repository provides a convenient way to access and manage persistent data in a Java application. In Spring Boot applications, Spring Data JPA supplies repository interfaces that reduce database - access code. A repository can retrieve entities and expose query methods derived from method names. 2. What is a JPA Repository? A JPA Repository is an interface used with Spring Data JPA to work with JPA entities. By extending JpaRepository, an application receives common data - access operations such as saving, finding, updating, and deleting entities. The TPointTech example creates an ExchangeValueRepository for the ExchangeValue entity and uses Long as the ID type. 3. Creating the Repository Interface Create an interface and extend JpaRepository with the entity type and ID type: public interface ExchangeValueRepository extends JpaRepository<ExchangeValue, Long> { } ExchangeValue is the managed entity, while Long represents the type of its primary - key field. 4. Injecting the Repository into the Controller The repository can be injected into a Spring controller so the controller can retrieve the required entity from the database. @Autowired private ExchangeValueRepository repository; 5. Creating a Query Method Spring Data JPA can derive a query from a repository method name. The currency - exchange example searches using both the from and to fields. ExchangeValue findByFromAndTo(String from, String to); For a single property, a method can be defined such as: ExchangeValue findByFrom(String from); 6. Using the Repository in the Controller The controller can call the repository instead of constructing an ExchangeValue object with hard - coded values. ExchangeValue exchangeValue = repository.findByFromAndTo(from, to); The returned object represents the matching database record. Other runtime information, such as the local server port, can then be added before the response is returned. 7. Example Controller Flow @GetMapping("/currency - exchange/from/{from}/to/{to}") public ExchangeValue retrieveExchangeValue( @PathVariable String from, @PathVariable String to) { ExchangeValue exchangeValue = repository.findByFromAndTo(from, to); exchangeValue.setPort( Integer.parseInt(environment.getProperty("local.server.port"))); return exchangeValue; } The URL values are mapped to from and to. The repository searches the database for the matching currency pair. 8. How the Query Works When a request such as /currency - exchange/from/USD/to/INR is received, the repository method is invoked with USD and INR. Spring Data JPA translates the method into a database query using the corresponding currency fields. findByFromAndTo("USD", "INR") 9. Benefits of JpaRepository • Provides ready - to - use CRUD operations through a repository interface. • Reduces boilerplate database - access code. • Supports query derivation from method names. • Works naturally with JPA entities and Spring dependency injection. • Makes repository logic easier to organize and reuse. • Fits well with Spring Boot applications and microservice architectures. 10. Common Repository Operations Method Purpose Example save() Stores or updates an entity repository.save(entity) findById() Finds an entity by ID repository.findById(1L) findAll() Retrieves entities repository.findAll() deleteById() Deletes an entity by ID repository.deleteById(1L) count() Counts stored entities repository.count() 11. Database Query Generated by Hibernate When the derived query method is executed, Hibernate can generate SQL that filters the exchange_value table using the currency - from and currency - to values. select ... from exchange_value where currency_from = ? and currency_to = ? 12. Request Example A sample endpoint request is: http://localhost:8000/currency - exchange/from/USD/to/INR Another currency pair can be requested by changing the path values, provided that the corresponding record exists in the database. 13. Key Points to Remember • Extend JpaRepository<Entity, ID> to create the repository interface. • The entity type identifies the JPA entity managed by the repository. • The ID type must match the entity's primary - key type. • Repository methods can be injected into controllers or services. • Methods such as findByFromAndTo can derive database queries. • Repository - based access helps separate persistence logic from request handling. Yes. A method such as findByFrom can derive a query using only the from property. Where is repository logic normally used? Repositories are generally used by service or controller layers to access persistent entity data, depending on the application's architecture. 15. Conclusion Creating a JPA Repository is an important step in connecting a Spring Boot application to persistent data. By extending JpaRepository, the currency - exchange service can retrieve ExchangeValue records without hard - coding response data. Spring Data JPA also supports derived query methods such as findByFromAndTo, making database access concise and readable. This approach is useful when an application needs clean separation between business logic and persistence.