Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object, String> discriminatorValues = new TreeMap<>();
final Map<Object, String> discriminatorValues = new LinkedHashMap<>();
collectDiscriminatorValue( returnedClassOrElement, discriminatorType, discriminatorValues );
collectSubclassElements(
propertyAccessor,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
// [...]
}
}