diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/MapManyToManyTreatJoinTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/MapManyToManyTreatJoinTest.java new file mode 100644 index 0000000000..e2266e10e4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/MapManyToManyTreatJoinTest.java @@ -0,0 +1,130 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.inheritance; + +import java.util.HashMap; +import java.util.Map; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.Table; + +import static org.assertj.core.api.Assertions.assertThat; + +@DomainModel( annotatedClasses = { + MapManyToManyTreatJoinTest.JoinedBase.class, + MapManyToManyTreatJoinTest.JoinedSub1.class, + MapManyToManyTreatJoinTest.JoinedSub2.class +} ) +@SessionFactory +@Jira( "https://hibernate.atlassian.net/browse/HHH-17255" ) +public class MapManyToManyTreatJoinTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final JoinedSub1 o1 = new JoinedSub1( 1, 123 ); + final JoinedSub2 o2 = new JoinedSub2( 2, "321" ); + session.persist( o2 ); + session.persist( o1 ); + o1.getSubMap().put( 2, o2 ); + o2.getSubMap().put( 1, o1 ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "from JoinedBase", JoinedBase.class ) + .getResultList() + .forEach( session::remove ) ); + } + + @Test + public void testTreatQueryStringProp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final String result = session.createSelectionQuery( + "select c.stringProp from JoinedBase t join treat(t.subMap as JoinedSub2) c", + String.class + ).getSingleResult(); + assertThat( result ).isEqualTo( "321" ); + } ); + } + + @Test + public void testTreatQueryIntProp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final Integer result = session.createSelectionQuery( + "select c.intProp from JoinedBase t join treat(t.subMap as JoinedSub1) c", + Integer.class + ).getSingleResult(); + assertThat( result ).isEqualTo( 123 ); + } ); + } + + @Entity( name = "JoinedBase" ) + @Inheritance( strategy = InheritanceType.JOINED ) + public static abstract class JoinedBase { + @Id + private Integer id; + + private String name; + + @ManyToMany + @JoinTable( name = "sub_map" ) + private Map subMap = new HashMap<>(); + + public JoinedBase() { + } + + public JoinedBase(Integer id) { + this.id = id; + } + + public Map getSubMap() { + return subMap; + } + } + + @Entity( name = "JoinedSub1" ) + @Table( name = "joined_sub_1" ) + public static class JoinedSub1 extends JoinedBase { + private Integer intProp; + + public JoinedSub1() { + } + + public JoinedSub1(Integer id, Integer intProp) { + super( id ); + this.intProp = intProp; + } + } + + @Entity( name = "JoinedSub2" ) + @Table( name = "joined_sub_2" ) + public static class JoinedSub2 extends JoinedBase { + private String stringProp; + + public JoinedSub2() { + } + + public JoinedSub2(Integer id, String stringProp) { + super( id ); + this.stringProp = stringProp; + } + } +}