|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.inheritance.embeddable; |
| 6 | + |
| 7 | +import jakarta.persistence.DiscriminatorColumn; |
| 8 | +import jakarta.persistence.Embeddable; |
| 9 | +import jakarta.persistence.Embedded; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 22 | + |
| 23 | +@DomainModel( |
| 24 | + annotatedClasses = { |
| 25 | + InheritedPropertyTest.Animal.class, |
| 26 | + InheritedPropertyTest.Cat.class, |
| 27 | + InheritedPropertyTest.Dog.class, |
| 28 | + InheritedPropertyTest.Fish.class, |
| 29 | + InheritedPropertyTest.Mammal.class, |
| 30 | + InheritedPropertyTest.Owner.class |
| 31 | + } |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +public class InheritedPropertyTest { |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( session -> { |
| 39 | + final var cat = new Cat(); |
| 40 | + cat.age = 7; |
| 41 | + cat.name = "Jones"; |
| 42 | + cat.mother = "Kitty"; |
| 43 | + final var owner = new Owner(); |
| 44 | + owner.id = 1L; |
| 45 | + owner.pet = cat; |
| 46 | + session.persist( owner ); |
| 47 | + } ); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void tearDown(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( session -> session.createMutationQuery( "delete from Owner" ).executeUpdate() ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testInheritedProperty(SessionFactoryScope scope) { |
| 57 | + assertDoesNotThrow( () -> scope.inSession( |
| 58 | + session -> session.createQuery( |
| 59 | + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Cat).mother = :mother", |
| 60 | + Owner.class ) ) ); |
| 61 | + scope.inSession( |
| 62 | + session -> { |
| 63 | + final var cats = session.createQuery( |
| 64 | + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Cat).mother = :mother", |
| 65 | + Owner.class ) |
| 66 | + .setParameter( "mother", "Kitty" ) |
| 67 | + .getResultList(); |
| 68 | + assertEquals( 1, cats.size() ); |
| 69 | + final var owner = cats.get( 0 ); |
| 70 | + assertInstanceOf( Cat.class, owner.pet ); |
| 71 | + assertEquals( "Jones", owner.pet.name ); |
| 72 | + assertEquals( "Kitty", ((Cat) owner.pet).mother ); |
| 73 | + } ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testDeclaredPropertyCreateQuery(SessionFactoryScope scope) { |
| 78 | + assertDoesNotThrow( () -> scope.inSession( |
| 79 | + session -> session.createQuery( |
| 80 | + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Mammal).mother = :mother", |
| 81 | + Owner.class ) ) ); |
| 82 | + } |
| 83 | + |
| 84 | + @Entity(name = "Owner") |
| 85 | + public static class Owner { |
| 86 | + @Id |
| 87 | + Long id; |
| 88 | + |
| 89 | + @Embedded |
| 90 | + Animal pet; |
| 91 | + } |
| 92 | + |
| 93 | + @Embeddable |
| 94 | + @DiscriminatorColumn(name = "animal_type") |
| 95 | + public static class Animal { |
| 96 | + int age; |
| 97 | + |
| 98 | + String name; |
| 99 | + } |
| 100 | + |
| 101 | + @Embeddable |
| 102 | + public static class Fish extends Animal { |
| 103 | + int fins; |
| 104 | + } |
| 105 | + |
| 106 | + @Embeddable |
| 107 | + public static class Mammal extends Animal { |
| 108 | + String mother; |
| 109 | + } |
| 110 | + |
| 111 | + @Embeddable |
| 112 | + public static class Cat extends Mammal { |
| 113 | + // [...] |
| 114 | + } |
| 115 | + |
| 116 | + @Embeddable |
| 117 | + public static class Dog extends Mammal { |
| 118 | + // [...] |
| 119 | + } |
| 120 | +} |
0 commit comments