data-jpa-3-to-4: Spring Data JPA 3.5 vs 4.2 side by side - package moves, nullness, Specification, keyset, Page JSON, Jackson 3, test slices, real transcripts

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7N4ZDbUtA3ofqAqRV
This commit is contained in:
Ankur Mhatre
2026-12-03 09:31:28 +05:30
co-authored by Claude Sonnet 5.5
parent 3f061c5e5d
commit 7a2bcc1eb6
100 changed files with 2314 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
<?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>4.2.0</version>
<relativePath/>
</parent>
<groupId>com.ankurm</groupId>
<artifactId>data-jpa-3-to-4-boot4</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource><directory>../common/src/main/resources</directory></resource>
</resources>
<plugins>
<!-- The tests and the code under test that is identical on both generations live in ../common -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>common-main</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration><sources><source>../common/src/main/java</source></sources></configuration>
</execution>
<execution>
<id>common-test</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration><sources><source>../common/src/test/java</source></sources></configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-processor</artifactId>
<version>${hibernate.version}</version>
</path>
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</path>
<path>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>${jakarta-persistence.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,27 @@
package com.ankurm.jpamig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.ankurm.jpamig.domain.Book;
import com.ankurm.jpamig.repo.BookRepository;
import java.time.LocalDate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase;
import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest;
import org.springframework.boot.jpa.test.autoconfigure.TestEntityManager;
/** Boot 4 imports. Compare with boot3/src/test/java: only the import lines differ. */
@DataJpaTest
@AutoConfigureTestDatabase
class BookSliceTest {
@Autowired TestEntityManager em;
@Autowired BookRepository books;
@Test
void findsByIsbn() {
em.persistAndFlush(new Book("Dune", null, "978-0002", "9.99", LocalDate.of(1965, 8, 1)));
assertEquals("Dune", books.findByIsbn("978-0002").orElseThrow().getTitle());
}
}
@@ -0,0 +1,38 @@
package com.ankurm.jpamig;
import com.ankurm.jpamig.domain.Book;
import com.ankurm.jpamig.domain.Book_;
import com.ankurm.jpamig.repo.BookRepository;
import java.time.LocalDate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jpa.domain.PredicateSpecification;
import org.springframework.data.jpa.domain.UpdateSpecification;
import org.springframework.transaction.support.TransactionTemplate;
/** Spring Data JPA 4: one PredicateSpecification serves reads, bulk deletes and bulk updates. */
@SpringBootTest
class BulkSpecificationTest {
@Autowired BookRepository books;
@Autowired TransactionTemplate tx;
@Test
void bulkOperations() {
Seed.books(books);
var t = new Transcript("23-bulk-specification");
PredicateSpecification<Book> old = (from, cb) -> cb.lessThan(from.get(Book_.publishedOn), LocalDate.of(1900, 1, 1));
PredicateSpecification<Book> dune = (from, cb) -> cb.equal(from.get(Book_.title), "Dune");
t.attempt("books.count(predicate)", () -> books.count(old));
t.sql();
UpdateSpecification<Book> retitle = UpdateSpecification
.<Book>update((root, update, cb) -> update.set(root.get(Book_.title), "Dune (1965)"))
.where(dune);
t.attempt("books.update(UpdateSpecification)", () -> tx.execute(s -> books.update(retitle)));
t.sql();
t.attempt("books.delete(predicate)", () -> books.delete(old));
t.sql();
t.write();
}
}