From b0ac15af425ec311c03d2beee9d91a7308c2712f Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 19 Jul 2023 12:19:38 +0200 Subject: [PATCH] HHH-15917 Add test for issue --- .../orm/test/query/UnrelatedEntitiesTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/UnrelatedEntitiesTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/UnrelatedEntitiesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/UnrelatedEntitiesTest.java new file mode 100644 index 0000000000..5ac6d76cb9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/UnrelatedEntitiesTest.java @@ -0,0 +1,129 @@ +package org.hibernate.orm.test.query; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DomainModel( + annotatedClasses = { + UnrelatedEntitiesTest.EntityA.class, + UnrelatedEntitiesTest.EntityB.class + } +) +@SessionFactory +@JiraKey("HHH-15917") +public class UnrelatedEntitiesTest { + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Set names = new HashSet<>(); + String name = "Fab"; + names.add( name ); + EntityA a = new EntityA( "a", names ); + EntityB b = new EntityB( name ); + + session.persist( a ); + session.persist( b ); + } + ); + } + + @Test + public void testNoExceptionThrown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + List resultList = session.createQuery( + "SELECT 'foobar' FROM EntityA a JOIN EntityB b ON b.name = element(a.names) ", + String.class + ).getResultList(); + assertThat( resultList.size() ).isEqualTo( 1 ); + } ); + } + + @Test + public void testNoExceptionThrown2(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + List resultList = session.createQuery( + "SELECT 'foobar' FROM EntityA a JOIN a.names aName JOIN EntityB b ON b.name = aName", + String.class + ).getResultList(); + assertThat( resultList.size() ).isEqualTo( 1 ); + } + ); + } + + @Entity(name = "EntityA") + public static class EntityA { + + @Id + @GeneratedValue + private Long id; + + private String name; + + @ElementCollection + private Set names; + + public EntityA() { + } + + public EntityA(String name, Set names) { + this.name = name; + this.names = names; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public Set getNames() { + return names; + } + } + + @Entity(name = "EntityB") + public static class EntityB { + + @Id + @GeneratedValue + private Long id; + + private String name; + + public EntityB() { + } + + public EntityB(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + } +}