Spring Boot H2 Database Setup

berkay başöz
4 min readAug 12, 2021

--

Spring Boot H2 Database is an extremely useful tool in the arsenal of any developer working on a Spring Boot application.

H2 database is a java in-memory database that allows you, as a developer, a large amount of freedom to work with database changes during development phase.

The Spring Boot H2 database can be embedded within a Java application or could also be run in client-server mode. It is also very useful during development phase for iterating through schema changes without worrying about making changes to the actual database such as MySQL or PostgreSQL.

Creating Spring Boot Project

Easiest way to create spring project, navigating https://start.spring.io/ and adding Spring Web, Spring Data JPA, H2 Database and Spring Boot DevTools dependencies.

or you can add these below dependencies to your existing gradle depencencies section.

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
}

Creating Entity

First, we create com.h2.demo.domain.entity package and then we create a class which name is Account.

@Entityand @Table annotations come from javax.persistence package and we use it for persistency.

@Entity annotation. This annotation basically tells JPA that this class should be mapped to its own table. In other words, @Entity is a type of marker interface similar to Serializable in Java.

@Table annotation is not mandatory. Basically, if we don’t specify it, JPA will use the class name as the table name.

@Id annotation will act as a primary key for the table. In our case, we have declared this Id field as a Long.

Other @Data, @NoArgsConstructor, @AllArgsConstructor and @Builder classes come from lombok package. They simplify our code and we don’t need getter, setter and custom builder methods anymore. Only thing we have to do is adding lombok dependency to our gradle and enable annotation processing if you use Intellij.

compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'

Enabling H2 Database Console View

H2 also provides a console view that can be used to see the tables created as well as the data inserted. It’s a pretty handy feature to see your persistence logic in action. Also, you can inspect the data in the h2 console view allowing you to test your application locally.

To enable the H2 console view, open the application.properties file in your application’s resources folder. Then, add the below statements in the file:

Change H2 Database Setup

You can change database setup in application.property file.

Creating Repository Class

We use JpaRespository and it take care of everything for us. It has built in repository method such as findBy, find, save etc.

Creating Service Classes

We need to set or get accounts from api. We create query, id generation and mock data generation services.

Creating Controller Classes

Testing Application

We run the spring boot application. Once it starts up successfully, visit the and we visit http://localhost:8080/h2-console .

You might need to change jdbc url to dbc:h2:mem:accounttestdb which we setup in application.property file earlier. Then, once you connect to the database, you should be able to query the Account.

There is no account in the table. H2 is in memory database and tables are created when application start.

Now, we can add some mock account data. We already create mock data controller and we can invoke it with localhost:8080/mockaccount/generatedummyaccounts endpoint. After invoke, we query accounts and we see there are two account in our H2 table.

Also, we can see inserted accounts by invoking localhost:8080/account/list endpoint.

Conclusion

With this we have basically completed setting up Spring Boot H2 Database for a sample application.

The code sample for this post is available on Github for reference.

--

--