0

I am trying to build an application with a embedded HSQL Database.

I am using Spring Boot 2.7.2, Hibernate 6.1.0-Final, Hibernate-Core Version 5.6.10-Final and HSQL 2.7.0 with Java 18.

For this, I've done alot of research for the error, even here on stack overflow, but somehow, none of them works.

First off, here's my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>de.bloise</groupId>
<artifactId>skt</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>18</maven.compiler.source>
    <maven.compiler.target>18</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hsql.version>2.7.0</hsql.version>
    <hibernate.version>6.1.0.Final</hibernate.version>
    <hibernate.core.version>5.6.10.Final</hibernate.core.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsql.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.core.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.6.10.Final</version>
    </dependency>

    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <version>2.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${project.parent.version}</version>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

Then, I'm using currently following applications.properties:

# ===============================
# = SERVER
# ===============================
server.port= 9050

# HIBERNATE
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
spring.jackson.serialization.fail-on-empty-beans=false

# Naming strategy
#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
#spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

# Database
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:hsql://localhost/testdb
spring.datasource.username=sa
spring.datasource.password=

So, for what I've tried, was to remove the datasource.url (as read somewhere here on stackoverflow), but the error popped up.

Then I found a way to use "hsql-config.xml" (with some configuration) and use it at my spring-boot entry point with @ImportResource(value="classpath:/hsql-config.xml") But it did not help.

Any ideas on how to make this work? I've looked up several tutorials accross the internet, it seems that non of those brought a solution.

Tony B
  • 344
  • 5
  • 19
  • As I understand you try with embedded in-memory database, right? if so, then you need to change to the following property: spring.datasource.url=jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1 – Moemen Aug 08 '22 at 21:42
  • yes, at first i want to do this. on the other hand, a file-based operation would be long-term, since I plan a persistance stack on that. – Tony B Aug 09 '22 at 11:08
  • Well, then there is two options. the first one is to start HSQLDB as a standalone not embedded server. the second one is to make the datasource bean depends on the loading properties bean such as this first answer: https://stackoverflow.com/questions/70006118/how-to-correctly-integrate-hsqldb-with-spring-boot using @DependsOn annotation. – Moemen Aug 09 '22 at 11:55
  • Okay, seems working. The embedded database isn't persisting? – Tony B Aug 09 '22 at 21:23
  • According to this https://stackoverflow.com/questions/6734158/has-hsqldb-some-mechanism-to-save-in-memory-data-to-file you can save the embedded database to a file .. I'm going to post the solution as an answer for benefit. – Moemen Aug 10 '22 at 10:56
  • 1
    Don't mix hibernate versions and Hibernate 6.1 isn't supported yet (only as of Spring Boot 3). Remove the hibernate dependencies and the version from the HSQLDB dependency (Spring Boot manages that for you). Finally your JDBC url indicates the use of an externally running HSQLDB, so you aren't using an embedded DB. You need to point it to a file or use an in-memory URL. – M. Deinum Aug 10 '22 at 11:04

2 Answers2

0

There are two options. the first one is to start HSQLDB as a standalone not embedded server. the second one is to make the datasource bean depends on the loading properties bean such as this first answer: Has HSQLDB some mechanism to save in-memory data to file? using @DependsOn annotation.

Moemen
  • 364
  • 3
  • 11
0

Your database URL accesses a separate HSQLDB server instance, which should be started before your application, and is similar to a PostgreSQL or MySQL server.

spring.datasource.url=jdbc:hsqldb:hsql://localhost/testdb

If you don't want to rely on a separate server, you can use a file: database URL, which opens the database directly from your application. For example:

spring.datasource.url=jdbc:hsqldb:file:C:/dbfiles/testdb
fredt
  • 24,044
  • 3
  • 40
  • 61