From 106067388dd18e6ae03fbda03798c83ecab9cd13 Mon Sep 17 00:00:00 2001 From: Gaetano Piazzolla Date: Sat, 21 Oct 2023 10:36:49 +0200 Subject: [PATCH] JAVA-220644 | Upgrade spring-boot-persistence-h2 to Spring Boot 3 (#14998) --- .../spring-boot-persistence-h2/pom.xml | 13 ++++++++++--- .../com/baeldung/h2/exceptions/models/User.java | 4 ++-- .../h2db/demo/client/ClientSpringBootApp.java | 4 ++-- .../baeldung/h2db/demo/server/SpringBootApp.java | 2 +- .../h2db/lazy_load_no_trans/entity/Document.java | 6 ++++-- .../h2db/lazy_load_no_trans/entity/User.java | 8 ++++---- .../java/com/baeldung/h2db/notnull/models/Item.java | 9 +++++---- .../baeldung/h2db/springboot/models/Country.java | 8 ++++---- .../src/main/resources/data-trans.sql | 12 ++++++------ .../baeldung/h2db/notnull/ItemIntegrationTest.java | 11 ++++++----- 10 files changed, 44 insertions(+), 33 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-h2/pom.xml b/persistence-modules/spring-boot-persistence-h2/pom.xml index f643db2260..3c4bf888b3 100644 --- a/persistence-modules/spring-boot-persistence-h2/pom.xml +++ b/persistence-modules/spring-boot-persistence-h2/pom.xml @@ -11,9 +11,9 @@ com.baeldung - parent-boot-2 + parent-boot-3 0.0.1-SNAPSHOT - ../../parent-boot-2 + ../../parent-boot-3 @@ -44,12 +44,19 @@ db-util ${db-util.version} + + + + org.hibernate.orm + hibernate-core + 6.3.1.Final + com.baeldung.h2db.demo.server.SpringBootApp - 1.0.4 + 1.0.7 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java index e54e725fd0..e1ae3c7cf0 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2/exceptions/models/User.java @@ -1,7 +1,7 @@ package com.baeldung.h2.exceptions.models; -import javax.persistence.Entity; -import javax.persistence.Id; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; @Entity public class User { diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java index 7402312e1c..3416cba154 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java @@ -3,15 +3,15 @@ package com.baeldung.h2db.demo.client; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; -import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; +import jakarta.annotation.PostConstruct; + @SpringBootApplication @ComponentScan("com.baeldung.h2db.demo.client") public class ClientSpringBootApp { diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java index e75b42a934..2a1eb66b22 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java @@ -3,7 +3,7 @@ package com.baeldung.h2db.demo.server; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; -import javax.annotation.PostConstruct; +import jakarta.annotation.PostConstruct; import org.h2.tools.Server; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/Document.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/Document.java index 9d69e7eb58..e737722ad4 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/Document.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/Document.java @@ -1,13 +1,14 @@ package com.baeldung.h2db.lazy_load_no_trans.entity; +import jakarta.persistence.Column; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.Immutable; -import javax.persistence.Entity; -import javax.persistence.Id; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; @Entity @Getter @@ -22,5 +23,6 @@ public class Document { private String title; + @Column(name="user_id") private Long userId; } diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/User.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/User.java index ae9cb9e4e8..b46903e589 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/User.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/lazy_load_no_trans/entity/User.java @@ -8,10 +8,10 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Immutable; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.OneToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import java.util.ArrayList; import java.util.List; diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/notnull/models/Item.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/notnull/models/Item.java index d2c4015b86..d36c763f0f 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/notnull/models/Item.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/notnull/models/Item.java @@ -1,9 +1,10 @@ package com.baeldung.h2db.notnull.models; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.validation.constraints.NotNull; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.validation.constraints.NotNull; + import java.math.BigDecimal; @Entity diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java index d6edab9421..c8bec1e908 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java @@ -1,9 +1,9 @@ package com.baeldung.h2db.springboot.models; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Table; import java.util.Objects; @Table(name = "countries") diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql index 2b4aa92542..7031d3ac02 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql @@ -4,9 +4,9 @@ insert into "user" values (103, 'user3', 'comment3'); insert into "user" values (104, 'user4', 'comment4'); insert into "user" values (105, 'user5', 'comment5'); -insert into "document" values (1, 'doc1', 101); -insert into "document" values (2, 'doc2', 101); -insert into "document" values (3, 'doc3', 101); -insert into "document" values (4, 'doc4', 101); -insert into "document" values (5, 'doc5', 102); -insert into "document" values (6, 'doc6', 102); \ No newline at end of file +insert into "document" ("id","title","user_id") values (1, 'doc1', 101); +insert into "document" ("id","title","user_id") values (2, 'doc2', 101); +insert into "document" ("id","title","user_id") values (3, 'doc3', 101); +insert into "document" ("id","title","user_id") values (4, 'doc4', 101); +insert into "document" ("id","title","user_id") values (5, 'doc5', 102); +insert into "document" ("id","title","user_id") values (6, 'doc6', 102); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2db/notnull/ItemIntegrationTest.java b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2db/notnull/ItemIntegrationTest.java index 0e2e5e3319..1213df4780 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2db/notnull/ItemIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/h2db/notnull/ItemIntegrationTest.java @@ -2,16 +2,17 @@ package com.baeldung.h2db.notnull; import com.baeldung.h2db.notnull.daos.ItemRepository; import com.baeldung.h2db.notnull.models.Item; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import javax.validation.ConstraintViolationException; - import static org.assertj.core.api.Assertions.assertThatThrownBy; +import jakarta.validation.ConstraintViolationException; + @RunWith(SpringRunner.class) @SpringBootTest(classes = NotNullVsNullableApplication.class) public class ItemIntegrationTest { @@ -21,8 +22,8 @@ public class ItemIntegrationTest { @Test public void shouldNotAllowToPersistNullItemsPrice() { - assertThatThrownBy(() -> itemRepository.save(new Item())) - .hasRootCauseInstanceOf(ConstraintViolationException.class) - .hasStackTraceContaining("must not be null"); + assertThatThrownBy(() -> itemRepository.save(new Item())).hasRootCauseInstanceOf(ConstraintViolationException.class) + .hasStackTraceContaining("propertyPath=price") + .hasStackTraceContaining("null"); } }