Partner – Microsoft – NPI (cat= Spring Boot)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

Partner – DBSchema – NPI (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

>> Take a look at DBSchema

1. Overview

Spring Boot makes it really easy to work with different database systems, without the hassle of manual dependency management.

More specifically, Spring Data JPA starter provides all the functionality required for seamless integration with several DataSource implementations.

In this tutorial, we’ll learn how to integrate Spring Boot with HSQLDB.

2. The Maven Dependencies

To demonstrate how easy is to integrate Spring Boot with HSQLDB, we’ll create a simple JPA repository layer that performs CRUD operations on customers entities using an in-memory HSQLDB  database.

Here’s the Spring Boot starter that we’ll use for getting our sample repository layer up and running:


Note that we’ve included the HSQLDB dependency as well. Without it, Spring Boot will try to automatically configure a DataSource bean and a JDBC connection pool for us through HikariCP.

As a consequence, if we don’t specify a valid DataSource dependency in our pom.xml file, we’ll get a build failure.

In addition, let’s make sure to check the latest version of spring-boot-starter-data-jpa on Maven Central.

3. Connecting to an HSQLDB Database

For exercising our demo repository layer, we’ll be using an in-memory database. It’s possible, however, to work with file-based databases as well. We’ll explore each of these methods in the sections below.

3.1. Running an External HSQLDB Server

Let’s take a look at how to get an external HSQLDB server running and create a file-based database. Installing HSQLDB and running the server is straightforward, overall.

Here are the steps that we should follow:

  • First, we’ll download HSQLDB and unzip it to a folder
  • Since HSQLDB doesn’t provide a default database out of the box, we’ll create one called “testdb” for example purposes
  • We’ll launch a command prompt and navigate to the HSQLDB data folder
  • Within the data folder, we’ll run the following command:
    java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file.testdb --dbname0.testdb
  • The above command will start the HSQLDB server and create our database whose source files will be stored in the data folder
  • We can make sure the database has been actually created by going to the data folder, which should contain a set of files called “testdb.lck”, “testdb.log”, “”, and “testdb.script” (the number of files varies depending on the type of database we’re creating)

Once the database has been set up, we need to create a connection to it.

To do this on Windows, let’s go to the database bin folder and run the runManagerSwing.bat file. This will open HSQLDB Database Manager’s initial screen, where we can enter the connection credentials:

  • Type: HSQL Database Engine
  • URL: jdbc:hsqldb:hsql://localhost/testdb
  • User: “SA” (System Administrator)
  • Password: leave the field empty

On Linux/Unix/Mac, we can use NetBeans, Eclipse, or IntelliJ IDEA to create the database connection through the IDE’s visual tools, using the same credentials.

In any of these tools, it’s straightforward to create a database table either by executing an SQL script in the Database Manager or within the IDE.

Once connected, we can create a customers table:

CREATE TABLE customers (
   id INT  NOT NULL,
   name VARCHAR (45),
   email VARCHAR (45),      

In just a few easy steps, we’ve created a file-based HSQLDB database containing a customers table.

3.2. The File

If we wish to connect to the previous file-based database from Spring Boot, here are the settings that we should include in the file:


Alternatively, if we use an in-memory database, we should use these:


Please note the DB_CLOSE_DELAY=-1 parameter appended to the end of the database URL. When working with an in-memory database, we need to specify this, so the JPA implementation, which is Hibernate, won’t close the database while the application is running.

4. The Customer Entity

With the database connection settings already set up,  next we need to define our Customer entity:

@Table(name = "customers")
public class Customer {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String email;

    // standard constructors / setters / getters / toString

5. The Customer Repository

In addition, we need to implement a thin persistence layer, which allows us to have basic CRUD functionality on our Customer JPA entities.

We can easily implement this layer by just extending the CrudRepository interface:

public interface CustomerRepository extends CrudRepository<Customer, Long> {}

6. Testing the Customer Repository

Finally, we should make sure that Spring Boot can actually connect to HSQLDB. We can easily accomplish this by just testing the repository layer.

Let’s start testing the repository’s findById() and findAll() methods:

public class CustomerRepositoryTest {
    private CustomerRepository customerRepository;
    public void whenFindingCustomerById_thenCorrect() { Customer("John", "[email protected]"));
    public void whenFindingAllCustomers_thenCorrect() { Customer("John", "[email protected]")); Customer("Julie", "[email protected]"));

Finally, let’s test the save() method:

public void whenSavingCustomer_thenCorrect() { Customer("Bob", "[email protected]"));
    Customer customer = customerRepository.findById(1L).orElseGet(() 
      -> new Customer("john", "[email protected]"));

7. Conclusion

In this article, we learned how to integrate Spring Boot with HSQLDB, and how to use either a file-based or in-memory database in the development of a basic JPA repository layer.

As usual, all the code samples shown in this article are available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.