From 1f16757d6f8c0340946f6fa022ed721cc01bfd74 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 6017b113712d303a90fbfd0bc9e365ca883c69f2 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 4b113207747a..e70c0efa39e8 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 @@ -11,9 +11,9 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.TreeMap; import org.hibernate.AnnotationException; import org.hibernate.annotations.DiscriminatorFormula; @@ -408,7 +408,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,