From 62d0abdbd4b5eaa3d36282f44027838387549b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Sat, 11 Jan 2025 09:18:11 +0100 Subject: [PATCH 1/2] HHH-19195 Test case using classes described in https://in.relation.to/2024/07/12/embeddable-inheritance/ --- .../embeddable/InheritedPropertyTest.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/embeddable/InheritedPropertyTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/embeddable/InheritedPropertyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/embeddable/InheritedPropertyTest.java new file mode 100644 index 000000000000..3e0e07a1fa8d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/embeddable/InheritedPropertyTest.java @@ -0,0 +1,120 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.inheritance.embeddable; + +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +@DomainModel( + annotatedClasses = { + InheritedPropertyTest.Animal.class, + InheritedPropertyTest.Cat.class, + InheritedPropertyTest.Dog.class, + InheritedPropertyTest.Fish.class, + InheritedPropertyTest.Mammal.class, + InheritedPropertyTest.Owner.class + } +) +@SessionFactory +public class InheritedPropertyTest { + + @BeforeEach + void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final var cat = new Cat(); + cat.age = 7; + cat.name = "Jones"; + cat.mother = "Kitty"; + final var owner = new Owner(); + owner.id = 1L; + owner.pet = cat; + session.persist( owner ); + } ); + } + + @AfterEach + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from Owner" ).executeUpdate() ); + } + + @Test + void testInheritedProperty(SessionFactoryScope scope) { + assertDoesNotThrow( () -> scope.inSession( + session -> session.createQuery( + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Cat).mother = :mother", + Owner.class ) ) ); + scope.inSession( + session -> { + final var cats = session.createQuery( + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Cat).mother = :mother", + Owner.class ) + .setParameter( "mother", "Kitty" ) + .getResultList(); + assertEquals( 1, cats.size() ); + final var owner = cats.get( 0 ); + assertInstanceOf( Cat.class, owner.pet ); + assertEquals( "Jones", owner.pet.name ); + assertEquals( "Kitty", ((Cat) owner.pet).mother ); + } ); + } + + @Test + void testDeclaredPropertyCreateQuery(SessionFactoryScope scope) { + assertDoesNotThrow( () -> scope.inSession( + session -> session.createQuery( + "select o from Owner o where treat(o.pet as InheritedPropertyTest$Mammal).mother = :mother", + Owner.class ) ) ); + } + + @Entity(name = "Owner") + public static class Owner { + @Id + Long id; + + @Embedded + Animal pet; + } + + @Embeddable + @DiscriminatorColumn(name = "animal_type") + public static class Animal { + int age; + + String name; + } + + @Embeddable + public static class Fish extends Animal { + int fins; + } + + @Embeddable + public static class Mammal extends Animal { + String mother; + } + + @Embeddable + public static class Cat extends Mammal { + // [...] + } + + @Embeddable + public static class Dog extends Mammal { + // [...] + } +} From 8cc9bed9a035b9547f3370db67a6f69301a6416c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Sat, 22 Feb 2025 07:46:45 +0100 Subject: [PATCH 2/2] HHH-19195 Discriminator values should be hierarchically sorted. While collecting they are inserted in proper order, but TreeMap will not preserver ordering, but sort them alphabetically by names. To keep values order LinkedHashMap should be used instead of TreeMap. --- .../org/hibernate/boot/model/internal/EmbeddableBinder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java index f7c2ea568b83..e6502a70f233 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java @@ -59,10 +59,10 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.TreeMap; import static org.hibernate.boot.model.internal.AnnotatedDiscriminatorColumn.DEFAULT_DISCRIMINATOR_COLUMN_NAME; import static org.hibernate.boot.model.internal.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn; @@ -516,7 +516,7 @@ static Component fillEmbeddable( final BasicType discriminatorType = (BasicType) component.getDiscriminator().getType(); // Discriminator values are used to construct the embeddable domain // type hierarchy so order of processing is important - final Map discriminatorValues = new TreeMap<>(); + final Map discriminatorValues = new LinkedHashMap<>(); collectDiscriminatorValue( returnedClassOrElement, discriminatorType, discriminatorValues ); collectSubclassElements( propertyAccessor,