Signup/Sign In

Spring Boot Application with in-memory Database

In this tutorial, we will learn the usage of in-memory database (H2) with spring boot. In many cases, instead of using Relational database, h2 is used to test the development functionality as it requires no configuration by the programmer.

Developing the Application with H2 Database:

  • H2 dependency was selected during the project setup which provides the JARs required to enable the in-memory database.

  • We have created POJOs in the last tutorial as Teacher.java and Student.java under com.tutorial.studytonight.pojo package.

  • We need to map the fields of the pojo with the columns of the table created in the in-memory database. So modify the Teacher.java file with the below code.

    @Entity
    @Table(name="TEACHER")
    public class Teacher {
        @Id
        @Column(name="ID")
        private int id;
        @Column(name="NAME")
        private String name;
        @Column(name="SUBJECT")
        private String subject;
        @Column(name="INSTITUTE")
        private String institute;
        @Column(name="CITY")
        private String city;
    
        //Setters and Getters
    
    }

  • @Entity: This annotation is used to mark the class as an entity that will be able to involve in the database transactions.

  • @Table: This annotation is used to map the class with the Table created in the database. It has an attribute called name (can be seen in the above code) used to name the table in the database.

  • @Id: This annotation is used to uniquely identify the record in the database. It acts as a primary key. In the above code field id is marked with this annotation so, that each teacher record will be unique.

  • @Column: This annotation is used to map each field in the entity to the columns of the database.

In this way, the class and fields are mapped to Tables and columns respectively.

Updating our REST project Code:

We have created the end point, service layer, repository layer to save the Teacher details in java.util.HashMap previously.

The code remains the same in controller and service layer but we need to change the repository details to store the values in Database instead of a HashMap. Observe the code in different layers.

@RestController
public class ResourceController {

    @Autowired
    ResourceService resourceService;
    
    @PostMapping("/saveTeacher")
    public void saveTeacher(@RequestBody Teacher teacher){
        resourceService.saveTeacher(teacher);
    }
}

@Service
public class ResourceService {

    @Autowired
    ResourceRepository repository;
    
    public void saveTeacher(Teacher teacher){
        repository.saveTeacher(teacher);
    }
}

@Repository
public class ResourceRepository {

    @PersistenceContext
    EntityManager entityManager;
    
    
    @Transactional
    public void saveTeacher(Teacher teacher){
        entityManager.persist(teacher);
    }
}

In the repository layer, we are using the EntityManager which helps in managing the entity and the method is annotated with javax.persistence.Transactional and helps in managing the database transaction while persisting the Teacher object in the in-memory database.

Testing with POSTMAN:

1. Run the application and send the POST request to save the entity in the database.

2. The above figure displays the POST request sent by the client to save the details in the database. The response code received was 200 OK which can be seen in the bottom-right corner.

3. In order to enable h2-console, we need the below property in application.properties.

spring.h2.console.enabled=true

4. We have made one more request to save the entity in database and for the h2-console browse to the URL localhost:8080/h2-console which will redirect to a JSP page similar to the below screenshot.

5. By default, the above fields will be filled with Username, URL and all we need to do is to click on connect without entering any fields. It will redirect to another screen similar to below figure.

6. Notice that the above figure has a table created by name Teacher as the class was mapped to the table named Teacher (refer to Teacher.java).

7. The above table contains the Teacher entities which were saved by the client.

Conclusion: In this tutorial, we have discussed about the spring boot application which is using the in-memory H2 database to store the data into the database unlike a HashMap. One important thing to note is that in-memory database is a temporary database where the records will be deleted every time we start the server. In the production environments if a RDBMS is selected, we need to provide details such as datasource URL, username and password.



About the author:
I'm a writer at studytonight.com.