Skip to content

Java: Spring Boot, Spring Data JPA, Hibernate

Hibernate updates the schema from the entity (ddl-auto=update, catalog introspection), then Spring Data saves and counts rows. The code is the same against the original database and against MachSQL; only the address changes.

JDBC_URL=jdbc:postgresql://old-postgres:5432/app
JDBC_URL=jdbc:postgresql://machsql-host:5432/app

Nothing else changes: not the code, not the driver, not its version, not the SQL.

  • Java 25 (LTS)
  • Spring Boot 4.1.2 (Hibernate ORM 7 and the PostgreSQL JDBC driver from its dependency management)
Terminal window
export JDBC_URL=jdbc:postgresql://machsql-host:5432/app DB_PASSWORD=...
mvn -q spring-boot:run

Expected output: inserted todo <id>, <n> open.

The program as it is kept and tested in the Mach Database for PostgreSQL repository; highlighted lines read the connection setting.

src/main/resources/application.properties
# The only line that changes is the host inside JDBC_URL.
spring.datasource.url=${JDBC_URL}
spring.datasource.username=${DB_USER:app}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.main.web-application-type=none
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 https://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>4.1.2</version>
<relativePath/>
</parent>
<groupId>example.machsql</groupId>
<artifactId>java-spring-boot</artifactId>
<version>1.0.0</version>
<properties>
<java.version>25</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
src/main/java/example/Application.java
package example;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/** Spring Boot with Spring Data JPA and Hibernate. */
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner demo(TodoRepository todos) {
return args -> {
Todo todo = todos.save(new Todo("write the examples"));
long open = todos.countByDoneFalse();
System.out.printf("inserted todo %d, %d open%n", todo.getId(), open);
};
}
}
src/main/java/example/Todo.java
package example;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "jpa_todo")
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private boolean done;
protected Todo() {}
public Todo(String title) {
this.title = title;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public boolean isDone() {
return done;
}
}
src/main/java/example/TodoRepository.java
package example;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<Todo, Long> {
long countByDoneFalse();
}