|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.inheritance; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Inheritance; |
| 23 | +import jakarta.persistence.InheritanceType; |
| 24 | +import jakarta.persistence.JoinTable; |
| 25 | +import jakarta.persistence.ManyToMany; |
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +@DomainModel( annotatedClasses = { |
| 31 | + MapManyToManyTreatJoinTest.JoinedBase.class, |
| 32 | + MapManyToManyTreatJoinTest.JoinedSub1.class, |
| 33 | + MapManyToManyTreatJoinTest.JoinedSub2.class |
| 34 | +} ) |
| 35 | +@SessionFactory |
| 36 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17255" ) |
| 37 | +public class MapManyToManyTreatJoinTest { |
| 38 | + @BeforeAll |
| 39 | + public void setUp(SessionFactoryScope scope) { |
| 40 | + scope.inTransaction( session -> { |
| 41 | + final JoinedSub1 o1 = new JoinedSub1( 1, 123 ); |
| 42 | + final JoinedSub2 o2 = new JoinedSub2( 2, "321" ); |
| 43 | + session.persist( o2 ); |
| 44 | + session.persist( o1 ); |
| 45 | + o1.getSubMap().put( 2, o2 ); |
| 46 | + o2.getSubMap().put( 1, o1 ); |
| 47 | + } ); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterAll |
| 51 | + public void tearDown(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( session -> session.createQuery( "from JoinedBase", JoinedBase.class ) |
| 53 | + .getResultList() |
| 54 | + .forEach( session::remove ) ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testTreatQueryStringProp(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( session -> { |
| 60 | + final String result = session.createSelectionQuery( |
| 61 | + "select c.stringProp from JoinedBase t join treat(t.subMap as JoinedSub2) c", |
| 62 | + String.class |
| 63 | + ).getSingleResult(); |
| 64 | + assertThat( result ).isEqualTo( "321" ); |
| 65 | + } ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testTreatQueryIntProp(SessionFactoryScope scope) { |
| 70 | + scope.inTransaction( session -> { |
| 71 | + final Integer result = session.createSelectionQuery( |
| 72 | + "select c.intProp from JoinedBase t join treat(t.subMap as JoinedSub1) c", |
| 73 | + Integer.class |
| 74 | + ).getSingleResult(); |
| 75 | + assertThat( result ).isEqualTo( 123 ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + @Entity( name = "JoinedBase" ) |
| 80 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 81 | + public static abstract class JoinedBase { |
| 82 | + @Id |
| 83 | + private Integer id; |
| 84 | + |
| 85 | + private String name; |
| 86 | + |
| 87 | + @ManyToMany |
| 88 | + @JoinTable( name = "sub_map" ) |
| 89 | + private Map<Integer, JoinedBase> subMap = new HashMap<>(); |
| 90 | + |
| 91 | + public JoinedBase() { |
| 92 | + } |
| 93 | + |
| 94 | + public JoinedBase(Integer id) { |
| 95 | + this.id = id; |
| 96 | + } |
| 97 | + |
| 98 | + public Map<Integer, JoinedBase> getSubMap() { |
| 99 | + return subMap; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Entity( name = "JoinedSub1" ) |
| 104 | + @Table( name = "joined_sub_1" ) |
| 105 | + public static class JoinedSub1 extends JoinedBase { |
| 106 | + private Integer intProp; |
| 107 | + |
| 108 | + public JoinedSub1() { |
| 109 | + } |
| 110 | + |
| 111 | + public JoinedSub1(Integer id, Integer intProp) { |
| 112 | + super( id ); |
| 113 | + this.intProp = intProp; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Entity( name = "JoinedSub2" ) |
| 118 | + @Table( name = "joined_sub_2" ) |
| 119 | + public static class JoinedSub2 extends JoinedBase { |
| 120 | + private String stringProp; |
| 121 | + |
| 122 | + public JoinedSub2() { |
| 123 | + } |
| 124 | + |
| 125 | + public JoinedSub2(Integer id, String stringProp) { |
| 126 | + super( id ); |
| 127 | + this.stringProp = stringProp; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments