Javatpoint Logo
Javatpoint Logo

Creating a JPA Repository

In the previous section, we have created a table in-memory database and saw that all the data is populated correctly. In this section, we will create a repository that returns the response for the service.

Step 1: Create an interface with the name ExchangeValueRepository and extends the JpaRepository class. We have to pass two parameters: type of the entity that it manages and the type of the Id field.

Step 2: Open CurrencyExchageController.java file and autowired the ExchageValueRepository.

Step 3: Create a query method in the ExcahngeValueRepository.java file.

In the above statement, ExchangeValue is the expected response. There are two columns that we have to find are from and to.

If we want to find data on the basis of single column, we can pass a column name. For example:

ExcahngeValueRepository.java

Step 4: In the CurrencyExchangeController.java use the following statement:

Instead of using the following statement:

CurrencyExchangeController.java

Step 5: Restart the application to pick up the changes. Open the browser and type the URI http://localhost:8000/currency-exchange/from/USD/to/INR. It returns the following response:

Creating a JPA Repository

We can also try a different conversion by changing the currency USD to EUR in the URI.

http://localhost:8000/currency-exchange/from/EUR/to/INR.

It returns the following response:

Creating a JPA Repository

In this above response, we are retrieving the values from the database.

When we pass the currency in the URI (EUR/to/INR), the query gets fired to the database. To see which query gets fired, we can see the query in the log.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA