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.
The only change
Section titled “The only change”JDBC_URL=jdbc:postgresql://old-postgres:5432/appJDBC_URL=jdbc:postgresql://machsql-host:5432/appNothing else changes: not the code, not the driver, not its version, not the SQL.
Versions
Section titled “Versions”- Java 25 (LTS)
- Spring Boot 4.1.2 (Hibernate ORM 7 and the PostgreSQL JDBC driver from its dependency management)
export JDBC_URL=jdbc:postgresql://machsql-host:5432/app DB_PASSWORD=...mvn -q spring-boot:runExpected output: inserted todo <id>, <n> open.
Source
Section titled “Source”The program as it is kept and tested in the Mach Database for PostgreSQL repository; highlighted lines read the connection setting.
# 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=updatespring.main.web-application-type=none<?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>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. */@SpringBootApplicationpublic 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); }; }}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; }}package example;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<Todo, Long> { long countByDoneFalse();}