|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.proxy.concrete; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.GeneratedValue; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.Table; |
| 11 | +import org.hibernate.annotations.ConcreteProxy; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests the compatibility of Hibernate {@code @ConcreteProxy} with Java {@code sealed} |
| 21 | + * inheritance hierarchies. Proxy generation may fail during {@code SessionFactory} |
| 22 | + * bootstrap if the abstract sealed root cannot be subclassed at runtime. These tests |
| 23 | + * document the limitation and assert the expected failure behavior. |
| 24 | + * |
| 25 | + * @author Vincent Bouthinon |
| 26 | + */ |
| 27 | +@Jpa(annotatedClasses = {ConcreteProxyWithSealedClassesTest.Actor.class, ConcreteProxyWithSealedClassesTest.Postman.class}) |
| 28 | +@JiraKey("HHH-19899") |
| 29 | +class ConcreteProxyWithSealedClassesTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void testConcreteProxyWithSealedClassesTest(EntityManagerFactoryScope scope) { |
| 33 | + var id = scope.fromTransaction( entityManager -> { |
| 34 | + Actor actor = new Postman(); |
| 35 | + entityManager.persist( actor ); |
| 36 | + return actor.id; |
| 37 | + } ); |
| 38 | + scope.inTransaction( entityManager -> { |
| 39 | + Actor actor = entityManager.getReference( Actor.class, id ); |
| 40 | + assertThat( actor ).isInstanceOf( Postman.class ); |
| 41 | + } ); |
| 42 | + } |
| 43 | + |
| 44 | + @Entity(name = "actor") |
| 45 | + @Table(name = "actor") |
| 46 | + @ConcreteProxy |
| 47 | + public static abstract sealed class Actor { |
| 48 | + @Id |
| 49 | + @GeneratedValue |
| 50 | + private Long id; |
| 51 | + } |
| 52 | + |
| 53 | + @Entity(name = "Postman") |
| 54 | + public static non-sealed class Postman extends Actor { |
| 55 | + } |
| 56 | +} |
0 commit comments