|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.inheritance.embeddable; |
| 6 | + |
| 7 | +import org.hibernate.MappingException; |
| 8 | +import org.hibernate.boot.Metadata; |
| 9 | +import org.hibernate.boot.MetadataSources; |
| 10 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.Jira; |
| 13 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Embeddable; |
| 17 | +import jakarta.persistence.Embedded; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.Id; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.fail; |
| 23 | + |
| 24 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19648") |
| 25 | +public class EmbeddableInheritanceRecursiveTest { |
| 26 | + @Test |
| 27 | + public void testSimpleRecursiveEmbedded() { |
| 28 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 29 | + final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() ) |
| 30 | + .addAnnotatedClass( Root1.class ) |
| 31 | + .addAnnotatedClass( Entity1.class ); |
| 32 | + try { |
| 33 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 34 | + fail( "Expected MappingException due to recursive embeddable mapping" ); |
| 35 | + } |
| 36 | + catch (Exception e) { |
| 37 | + assertThat( e ).isInstanceOf( MappingException.class ); |
| 38 | + assertThat( e.getMessage() ) |
| 39 | + .contains( "Recursive embeddable mapping detected" ) |
| 40 | + .contains( Root1.class.getName() ); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testChildWithRootProp() { |
| 46 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 47 | + final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() ) |
| 48 | + .addAnnotatedClass( Root2.class ) |
| 49 | + .addAnnotatedClass( Child2.class ) |
| 50 | + .addAnnotatedClass( Entity2.class ); |
| 51 | + try { |
| 52 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 53 | + fail( "Expected MappingException due to recursive embeddable mapping" ); |
| 54 | + } |
| 55 | + catch (Exception e) { |
| 56 | + assertThat( e ).isInstanceOf( MappingException.class ); |
| 57 | + assertThat( e.getMessage() ) |
| 58 | + .contains( "Recursive embeddable mapping detected" ) |
| 59 | + .contains( Root2.class.getName() ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testRootWithChildProp() { |
| 65 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 66 | + final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() ) |
| 67 | + .addAnnotatedClass( Root3.class ) |
| 68 | + .addAnnotatedClass( Child3.class ) |
| 69 | + .addAnnotatedClass( Entity3.class ); |
| 70 | + try { |
| 71 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 72 | + fail( "Expected MappingException due to recursive embeddable mapping" ); |
| 73 | + } |
| 74 | + catch (Exception e) { |
| 75 | + assertThat( e ).isInstanceOf( MappingException.class ); |
| 76 | + assertThat( e.getMessage() ) |
| 77 | + .contains( "Recursive embeddable mapping detected" ) |
| 78 | + .contains( Root3.class.getName() ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testMidEmbedded() { |
| 84 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 85 | + final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() ) |
| 86 | + .addAnnotatedClass( Root4.class ) |
| 87 | + .addAnnotatedClass( Mid4.class ) |
| 88 | + .addAnnotatedClass( Child4.class ) |
| 89 | + .addAnnotatedClass( Entity4.class ); |
| 90 | + try { |
| 91 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 92 | + fail( "Expected MappingException due to recursive embeddable mapping" ); |
| 93 | + } |
| 94 | + catch (Exception e) { |
| 95 | + assertThat( e ).isInstanceOf( MappingException.class ); |
| 96 | + assertThat( e.getMessage() ) |
| 97 | + .contains( "Recursive embeddable mapping detected" ) |
| 98 | + .contains( Root4.class.getName() ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testUnrelatedRecursive() { |
| 104 | + final StandardServiceRegistryBuilder registryBuilder = ServiceRegistryUtil.serviceRegistryBuilder(); |
| 105 | + final MetadataSources metadataSources = new MetadataSources( registryBuilder.build() ) |
| 106 | + .addAnnotatedClass( EmbA.class ) |
| 107 | + .addAnnotatedClass( EmbB.class ) |
| 108 | + .addAnnotatedClass( Entity5.class ); |
| 109 | + try { |
| 110 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 111 | + fail( "Expected MappingException due to recursive embeddable mapping" ); |
| 112 | + } |
| 113 | + catch (Exception e) { |
| 114 | + assertThat( e ).isInstanceOf( MappingException.class ); |
| 115 | + assertThat( e.getMessage() ) |
| 116 | + .contains( "Recursive embeddable mapping detected" ) |
| 117 | + .contains( EmbA.class.getName() ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Embeddable |
| 122 | + static class Root1 { |
| 123 | + String root1Prop; |
| 124 | + |
| 125 | + @Embedded |
| 126 | + Root1 nested1; |
| 127 | + } |
| 128 | + |
| 129 | + @Entity(name = "Entity1") |
| 130 | + static class Entity1 { |
| 131 | + @Id |
| 132 | + private Long id; |
| 133 | + |
| 134 | + @Embedded |
| 135 | + private Root1 root1; |
| 136 | + } |
| 137 | + |
| 138 | + @Embeddable |
| 139 | + static class Root2 { |
| 140 | + String root2Prop; |
| 141 | + } |
| 142 | + |
| 143 | + @Embeddable |
| 144 | + static class Child2 extends Root2 { |
| 145 | + String child2Prop; |
| 146 | + |
| 147 | + @Embedded |
| 148 | + Root2 nested2; |
| 149 | + } |
| 150 | + |
| 151 | + @Entity(name = "Entity2") |
| 152 | + static class Entity2 { |
| 153 | + @Id |
| 154 | + private Long id; |
| 155 | + |
| 156 | + @Embedded |
| 157 | + private Root2 root2; |
| 158 | + } |
| 159 | + |
| 160 | + @Embeddable |
| 161 | + static class Root3 { |
| 162 | + String root3Prop; |
| 163 | + |
| 164 | + @Embedded |
| 165 | + Child3 nested3; |
| 166 | + } |
| 167 | + |
| 168 | + @Embeddable |
| 169 | + static class Child3 extends Root3 { |
| 170 | + String child3Prop; |
| 171 | + } |
| 172 | + |
| 173 | + @Entity(name = "Entity3") |
| 174 | + static class Entity3 { |
| 175 | + @Id |
| 176 | + private Long id; |
| 177 | + |
| 178 | + @Embedded |
| 179 | + private Root3 root3; |
| 180 | + } |
| 181 | + |
| 182 | + @Embeddable |
| 183 | + static class Root4 { |
| 184 | + String root4Prop; |
| 185 | + } |
| 186 | + |
| 187 | + @Embeddable |
| 188 | + static class Mid4 extends Root4 { |
| 189 | + } |
| 190 | + |
| 191 | + @Embeddable |
| 192 | + static class Child4 extends Mid4 { |
| 193 | + String child4Prop; |
| 194 | + |
| 195 | + @Embedded |
| 196 | + Mid4 nested4; |
| 197 | + } |
| 198 | + |
| 199 | + @Entity(name = "Entity4") |
| 200 | + static class Entity4 { |
| 201 | + @Id |
| 202 | + private Long id; |
| 203 | + |
| 204 | + @Embedded |
| 205 | + private Root4 root4; |
| 206 | + } |
| 207 | + |
| 208 | + @Embeddable |
| 209 | + static class EmbA { |
| 210 | + String emb1; |
| 211 | + |
| 212 | + @Embedded |
| 213 | + EmbB embB; |
| 214 | + } |
| 215 | + |
| 216 | + @Embeddable |
| 217 | + static class EmbB { |
| 218 | + String embB; |
| 219 | + |
| 220 | + @Embedded |
| 221 | + EmbA embA; |
| 222 | + } |
| 223 | + |
| 224 | + @Entity(name = "Entity5") |
| 225 | + static class Entity5 { |
| 226 | + @Id |
| 227 | + private Long id; |
| 228 | + |
| 229 | + @Embedded |
| 230 | + private EmbA embA; |
| 231 | + } |
| 232 | +} |
0 commit comments