Hibernate 7 and H2 2.4.240 compatible issues

5 days ago 9
ARTICLE AD BOX

When upgrading H2 to the latest 2.4.240, the H2 specific tests failed with the following errors:

insert into posts (content,created_at,slug,status,title,version,id) values (?,?,?,?,?,?,?) [23514-240] Error: Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.404 s <<< FAILURE! -- in com.example.demo.h2.PostRepositoryTest Error: com.example.demo.h2.PostRepositoryTest.testInsertAndQuery -- Time elapsed: 0.062 s <<< ERROR! org.springframework.orm.jpa.JpaSystemException: could not execute statement [Check constraint invalid: "CONSTRAINT_4: "; SQL statement: insert into posts (content,created_at,slug,status,title,version,id) values (?,?,?,?,?,?,?) [23514-240]] [insert into posts (content,created_at,slug,status,title,version,id) values (?,?,?,?,?,?,?)] at

Check the Github action build stack: https://github.com/hantsy/spring7-sandbox/actions/runs/19777068829/job/56671283155#step:4:375

The post is simple JPA entity as follows.

@Entity() @Table(name = "posts") public class Post implements Serializable { @Id @GeneratedValue(strategy = GenerationType.UUID) UUID id; @Column(name = "title") private String title; @Column(name = "content") private String content; @Embedded private Slug slug; // use new @EnumeratedValue private Status status = Status.DRAFT; @Column(name = "created_at") private LocalDateTime createdAt; @Version private Long version = 1L; public Post() { } }

And the tests is the following.

@SpringJUnitConfig(classes = {PostRepositoryTest.TestConfig.class}) public class PostRepositoryTest { private final static Logger log = LoggerFactory.getLogger(PostRepositoryTest.class); @Autowired PostRepository posts; @BeforeEach public void setup() { var deleted = this.posts.deleteAll(); log.debug("deleted posts: {}", deleted); } @Test public void testSaveAll() { var data = List.of( Post.of("test", "content", Status.PENDING_MODERATION), Post.of("test1", "content1", Status.DRAFT) ); data.forEach(this.posts::save); var results = posts.findAll(); assertThat(results.size()).isEqualTo(2); var resultsByKeyword = posts.findByKeyword("", Status.PENDING_MODERATION, 0, 10); assertThat(resultsByKeyword.size()).isEqualTo(1); } @Test public void testInsertAndQuery() { var data = Post.of("test1", "content1", Status.DRAFT); var saved = this.posts.save(data); this.posts.findById(saved.getId()).ifPresent( p -> assertThat(p.getStatus()).isEqualTo(Status.DRAFT) ); } @Configuration @ComponentScan(basePackageClasses = PostRepository.class) @Import({JpaConfig.class}) static class TestConfig { @Bean @Primary public DataSource embeddedDataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .build(); } } }

The test worked well with the old H2 2.3.x, and I also created a test to run similar test with Postgre test container, worked.

Read Entire Article