|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.inheritance; |
| 6 | + |
| 7 | +import jakarta.persistence.Embeddable; |
| 8 | +import jakarta.persistence.EmbeddedId; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.Inheritance; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import jakarta.persistence.Tuple; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +@DomainModel( annotatedClasses = { |
| 25 | + InheritanceToOneSubtypeJoinGroupByTest.Base.class, |
| 26 | + InheritanceToOneSubtypeJoinGroupByTest.EntityA.class, |
| 27 | + InheritanceToOneSubtypeJoinGroupByTest.EntityB.class, |
| 28 | + InheritanceToOneSubtypeJoinGroupByTest.WhitelistEntry.class, |
| 29 | +} ) |
| 30 | +@SessionFactory |
| 31 | +public class InheritanceToOneSubtypeJoinGroupByTest { |
| 32 | + @Test |
| 33 | + void testGroupByA(SessionFactoryScope scope) { |
| 34 | + scope.inSession( session -> { |
| 35 | + final EntityA result = session.createQuery( |
| 36 | + "SELECT a FROM WhitelistEntry we JOIN we.primaryKey.a a group by a", |
| 37 | + EntityA.class |
| 38 | + ).getSingleResult(); |
| 39 | + assertThat( result.getAName() ).isEqualTo( "a" ); |
| 40 | + } ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testGroupByB(SessionFactoryScope scope) { |
| 45 | + scope.inSession( session -> { |
| 46 | + final Tuple result = session.createQuery( |
| 47 | + "SELECT b.id, b.bName FROM WhitelistEntry we JOIN we.primaryKey.b b group by b", |
| 48 | + Tuple.class |
| 49 | + ).getSingleResult(); |
| 50 | + assertThat( result.get( 1, String.class ) ).isEqualTo( "b" ); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeAll |
| 55 | + public void setUp(SessionFactoryScope scope) { |
| 56 | + scope.inTransaction( session -> { |
| 57 | + final EntityA a = new EntityA(); |
| 58 | + a.setAName( "a" ); |
| 59 | + final EntityB b = new EntityB(); |
| 60 | + b.setBName( "b" ); |
| 61 | + final WhitelistEntry whitelistEntry = new WhitelistEntry(); |
| 62 | + whitelistEntry.setName( "whitelistEntry" ); |
| 63 | + final WhitelistEntryPK primaryKey = new WhitelistEntryPK(); |
| 64 | + primaryKey.setA( a ); |
| 65 | + primaryKey.setB( b ); |
| 66 | + whitelistEntry.setPrimaryKey( primaryKey ); |
| 67 | + session.persist( a ); |
| 68 | + session.persist( b ); |
| 69 | + session.persist( whitelistEntry ); |
| 70 | + } ); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterAll |
| 74 | + public void tearDown(SessionFactoryScope scope) { |
| 75 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 76 | + } |
| 77 | + |
| 78 | + @Entity(name = "Base") |
| 79 | + @Inheritance |
| 80 | + public static class Base { |
| 81 | + @Id |
| 82 | + @GeneratedValue |
| 83 | + private Long id; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @Entity(name = "EntityA") |
| 88 | + public static class EntityA extends Base { |
| 89 | + private String aName; |
| 90 | + |
| 91 | + public String getAName() { |
| 92 | + return aName; |
| 93 | + } |
| 94 | + |
| 95 | + public void setAName(String name) { |
| 96 | + this.aName = name; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Entity(name = "EntityB") |
| 101 | + public static class EntityB extends Base { |
| 102 | + private String bName; |
| 103 | + |
| 104 | + public String getBName() { |
| 105 | + return bName; |
| 106 | + } |
| 107 | + |
| 108 | + public void setBName(String name) { |
| 109 | + this.bName = name; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Embeddable |
| 114 | + public static class WhitelistEntryPK { |
| 115 | + @ManyToOne |
| 116 | + private EntityB b; |
| 117 | + |
| 118 | + @ManyToOne |
| 119 | + private EntityA a; |
| 120 | + |
| 121 | + public WhitelistEntryPK() { |
| 122 | + } |
| 123 | + |
| 124 | + public EntityB getB() { |
| 125 | + return b; |
| 126 | + } |
| 127 | + |
| 128 | + public void setB(EntityB b) { |
| 129 | + this.b = b; |
| 130 | + } |
| 131 | + |
| 132 | + public EntityA getA() { |
| 133 | + return a; |
| 134 | + } |
| 135 | + |
| 136 | + public void setA(EntityA a) { |
| 137 | + this.a = a; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Entity(name = "WhitelistEntry") |
| 142 | + public static class WhitelistEntry { |
| 143 | + @EmbeddedId |
| 144 | + private WhitelistEntryPK primaryKey; |
| 145 | + |
| 146 | + private String name; |
| 147 | + |
| 148 | + public String getName() { |
| 149 | + return name; |
| 150 | + } |
| 151 | + |
| 152 | + public void setName(String name) { |
| 153 | + this.name = name; |
| 154 | + } |
| 155 | + |
| 156 | + public WhitelistEntryPK getPrimaryKey() { |
| 157 | + return primaryKey; |
| 158 | + } |
| 159 | + |
| 160 | + public void setPrimaryKey(WhitelistEntryPK primaryKey) { |
| 161 | + this.primaryKey = primaryKey; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments