---
# System prepended metadata

title: 2Building your First RESTful CRUD API.

---

# 2Building your First RESTful CRUD API.
## Part 2: Creating the Repository Layer
The Repository layer is a lightweight layer which has only one responsibility - perform queries on a database (in our case, H2).
Spring Data JPA focuses on using JPA to store our data in a relational database that we choose. We can pass in our entity objects to the repository layer, and JPA will perform the database operations for us.

### Why should we use JPA?
It has some cool benefits:
1. In our Repository layer, we need to provide an interface and `extend` either the `CrudRepository` interface or the `JpaRepository` interface. Out of the box, it provides a class which implements the JPA interfaces, which directly interact with an ***Object Relational Mapper (ORM)*** - ***Hibernate***, and can establish connections with the database and perform queries on it without us needing to do much configuration. 
2. If we have complex queries (within reason), we can define these in our interface, and Spring Data JPA is clever enough to create implementations of these methods declared at runtime from the repository interface itself. 😮 
Think about that for ***1 second***... it literally generates implemenations of our code ON THE FLY  and will be able to perform the queries for us... that's freakin' mad! 

### Implementing the Repository Layer

We can now use Spring Data JPA to make a ReservationEntity object interact with the database very easily.

1. Create a new package called `respository` within `com.rms.reservationservice`.
2. Create an `interface` called `ReservationRespository`. Within this interface, we can make use of some of the interfaces that JPA provides for us so that we do not have to rewrite the same boilerplate Hibernate code.

```
package com.rms.reservationservice.respository;

import com.rms.reservationservice.entity.ReservationEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReservationRepository extends CrudRepository<ReservationEntity, String> {}
```
- ***@Repository*** - A Spring annotation that indicates that this interface is to be used for storage, retrieval, and search behavior for a database.
- `@Repository` is a child of the `@Component` annotation, which indicates to Spring that this interface is a Spring Component. At application startup, Spring's Component Scanner will register all `@Component`'s and its subclassees into the Application Context, enabling us to inject this repository interface into other classes as we will see shortly. 
(If you are not familiar with dependency Injection, I will be making a guide on how this works in more detail soon)
- ***CrudRepository<T, ID>*** - Provides the functionality to perform CRUD operations on this `ReservationEntity` object in the database. We pass in the entity object name, as well as the primary key type, which in our case is a `String`. CRUD methods are provided to us by the `CrudRepository` interface and its basic implementation provided to us by the `SimpleJpaRepository` class.
    - ***C***reate a new resource
    - ***R***etrieve an existing resource (by ID)
    - ***U***pdate an existing resource
    - ***D***elete an existing resource

If you have any trouble with this, fork the project and checkout branch [part-2-creating-repository-layer](https://github.com/csapty12/restaurantManagementSystem/tree/part-2-creating-repository-layer) to see the repository layer created.

## Configuring the database
Now that we have the `ReservationEntity` and the `ReservationRepository` created, now lets configure the database. 

1. open `src/main/resouces/application.properties`
2. copy and paste the following properties:
```
# url to connect to the database and gives the database name: testdb
spring.datasource.url=jdbc:h2:mem:testdb

#Spring boot needs to verify that a valid Driver class is available. In our case, its the h2 driver.
spring.datasource.driverClassName=org.h2.Driver

spring.datasource.username=sa
spring.datasource.password=password

#Name of the target database to operate on, auto-detected by default
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

#enables the web UI so we can interact with the database directly.
spring.h2.console.enabled=true
```

By default, Spring Boot configures the application to connect to an in-memory store with the username `sa` and an empty password. But we can override the spring boot datasource properties as we like.

- ***spring.datasource*** - A factory for connections to any physical data source. we provide additional information so that we can easily connect to the database that we want.

Once you have included these properties in your `application.properties` file, start your application up using the gradle command`./gradlew bootRun`. By default, your application will start up on port `8080`. Since we are running this on our local machine, go to `http://localhost:8080/h2-console` where you will be presented with a database login page:
![](https://i.imgur.com/tscKVqC.png)
The Driver Class, DB URL and username have already been prepopulated, all you need to do is enter in the password provided in the `application.properties` file, and you will be logged in.
![](https://i.imgur.com/MlhwDg7.png)
Without you needing to create the tables yourself, by using the `@Entity` and `@Table` annotations, the `RESERVATION` table has been created automatically with all of the fields that were provided from our entity object all thanks to JPA. Pretty cool huh!

**Congratulations!!** 🎉🎉 You have just successfully connected to a database, and leverages the powers of Spring Data JPA to let it handle allllll the boilerplate stuff for you! How long did that take?! not even 10 minutes. Imagine having to do all this yourself! Thank you Spring!

If you did get stuck at any point, please checkout the branch [part-2-configuring-the-database](https://github.com/csapty12/restaurantManagementSystem/tree/part-2-configuring-the-database) and compare your code against mine.

Now that we have successfully connected to our database, the next step is to create a domain object, that we can pass around the application. Check out [Part 3: Creating the Reservation Domain / Model Object](https://hackmd.io/@csapty12/HkrVhetCP)