From 6e6a304bde1dcb20a1e64d516335c31ef10895c6 Mon Sep 17 00:00:00 2001 From: mertcorumlu Date: Sat, 13 Jul 2024 23:08:49 +0200 Subject: [PATCH] HHH-18353 Reproducer --- .../test/java/org/hibernate/bugs/Author.java | 20 ++++++++ .../test/java/org/hibernate/bugs/Book.java | 35 ++++++++++++++ .../org/hibernate/bugs/JPAUnitTestCase.java | 47 ++++++++++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Author.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Book.java diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Author.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Author.java new file mode 100644 index 00000000..c91ce381 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Author.java @@ -0,0 +1,20 @@ +package org.hibernate.bugs; + + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + public Long getId() { + return id; + } + +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Book.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Book.java new file mode 100644 index 00000000..7545bc25 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Book.java @@ -0,0 +1,35 @@ +package org.hibernate.bugs; + +import jakarta.persistence.*; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + @ManyToOne + private Author mainAuthor; + + @ManyToOne + private Author contributedAuthor; + + public Author getMainAuthor() { + return mainAuthor; + } + + public Author getContributedAuthor() { + return contributedAuthor; + } + + public Book setMainAuthor(Author value) { + mainAuthor = value; + return this; + } + + public Book setContributedAuthor(Author value) { + contributedAuthor = value; + return this; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index 6662194d..c6b6b2d9 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -4,10 +4,18 @@ import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; +import jakarta.persistence.TypedQuery; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + /** * This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API. */ @@ -28,10 +36,45 @@ public void destroy() { // Entities are auto-discovered, so just add them anywhere on class-path // Add your tests, using standard JUnit. @Test - public void hhh123Test() throws Exception { + public void hhh18353Test() throws Exception { EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + /* + You can see first assertion will go through but second will fail. + Only different in queries is that one casts array elements explicitly, + while other tries to infer element types and assure all elements has are + of same type. + */ + + Author author1 = entityManager.merge(new Author()); + Author author2 = entityManager.merge(new Author()); + Book book = entityManager.merge(new Book().setMainAuthor(author1).setContributedAuthor(author2)); + + Set expectedResults = Set.of(author1.getId(), author2.getId()); + + String castedQueryStr = "SELECT ARRAY(CAST(b.mainAuthor.id as Long), CAST(b.contributedAuthor.id as Long)) FROM Book b WHERE b = :book"; + TypedQuery castedQuery = entityManager.createQuery(castedQueryStr, Long[].class); + castedQuery.setParameter("book", book); + Long[] castedResults = castedQuery.getSingleResult(); + + Assert.assertEquals( + "Expect found ids to be ones that we inserted into database", + expectedResults, + Set.of(castedResults) + ); + + String nonCastedQueryStr = "SELECT ARRAY(b.mainAuthor.id, b.contributedAuthor.id) FROM Book b WHERE b = :book"; + TypedQuery nonCastedQuery = entityManager.createQuery(nonCastedQueryStr, Long[].class); + nonCastedQuery.setParameter("book", book); + Long[] nonCastedResults = nonCastedQuery.getSingleResult(); + + Assert.assertEquals( + "Expect found ids to be ones that we inserted into database", + expectedResults, + Set.of(nonCastedResults) + ); + entityManager.getTransaction().commit(); entityManager.close(); }