From d69138f1b935f84d1e1b4cebfbed2e4040093fac Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 13 Jul 2023 16:45:35 +0200 Subject: [PATCH] HHH-16812 add test for issue --- ...eddableWithParentWithInheritance2Test.java | 159 ++++++++++++++++++ ...beddableWithParentWithInheritanceTest.java | 108 ++++++------ 2 files changed, 214 insertions(+), 53 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritance2Test.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritance2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritance2Test.java new file mode 100644 index 0000000000..d45dbe7ac9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritance2Test.java @@ -0,0 +1,159 @@ +package org.hibernate.orm.test.embeddable; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.BatchSize; +import org.hibernate.annotations.Parent; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.DiscriminatorType; +import jakarta.persistence.DiscriminatorValue; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * @author gtoison + */ +@SessionFactory +@DomainModel(annotatedClasses = { + EmbeddableWithParentWithInheritance2Test.Food.class, + EmbeddableWithParentWithInheritance2Test.Cheese.class, + EmbeddableWithParentWithInheritance2Test.Smell.class +}) +@JiraKey("HHH-16812") +public class EmbeddableWithParentWithInheritance2Test { + + @Test + public void test(SessionFactoryScope scope) { + Cheese roquefort = new Cheese(); + + scope.inTransaction( s -> { + Smell smell = new Smell(); + + roquefort.setSmell( smell ); + SmellOf smellOf = new SmellOf(); + smellOf.setCheese( roquefort ); + smellOf.setIntensity( 2 ); + smell.setSmellOf( smellOf ); + smell.setName( "blue roquefort" ); + + s.persist( roquefort ); + } ); + + scope.inSession( s -> { + Food food = s.getReference( Food.class, roquefort.getId() ); + assertFalse( Hibernate.isInitialized( food ) ); + Object unproxied = Hibernate.unproxy( food ); + assertThat( unproxied ).isInstanceOf( Cheese.class ); + Cheese cheese = (Cheese) unproxied; + assertThat(cheese.getSmell()).isNotNull(); + assertThat(cheese.getSmell().getSmellOf()).isNotNull(); + assertThat(cheese.getSmell().getSmellOf().getCheese()).isNotNull(); + } ); + } + + @Entity(name = "Food") + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type") + @DiscriminatorValue(value = "FOOD") + public static class Food { + Long id; + + public Food() { + } + + public String describe() { + return "Good food"; + } + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } + + + @Entity(name = "Roquefort") + @DiscriminatorValue("ROQUEFORT") + public static class Cheese extends Food { + Smell smell; + + public Cheese() { + } + + @Embedded + public Smell getSmell() { + return smell; + } + + public void setSmell(Smell smell) { + this.smell = smell; + } + } + + @Embeddable + public static class Smell { + SmellOf smellOf; + + String name; + + public SmellOf getSmellOf() { + return smellOf; + } + + public void setSmellOf(SmellOf smellOf) { + this.smellOf = smellOf; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Embeddable + public static class SmellOf { + Cheese cheese; + + Integer intensity; + + @Parent + public Cheese getCheese() { + return cheese; + } + + public void setCheese(Cheese cheese) { + this.cheese = cheese; + } + + public Integer getIntensity() { + return intensity; + } + + public void setIntensity(Integer intensity) { + this.intensity = intensity; + } + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritanceTest.java index 262984f018..099845fdd7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddableWithParentWithInheritanceTest.java @@ -1,61 +1,68 @@ package org.hibernate.orm.test.embeddable; -import jakarta.persistence.*; import org.hibernate.Hibernate; -import org.hibernate.annotations.*; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.annotations.BatchSize; +import org.hibernate.annotations.Parent; + import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; -import java.util.HashSet; -import java.util.Set; +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.DiscriminatorType; +import jakarta.persistence.DiscriminatorValue; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author gtoison - * */ @SessionFactory @DomainModel(annotatedClasses = { EmbeddableWithParentWithInheritanceTest.Food.class, EmbeddableWithParentWithInheritanceTest.Cheese.class, - EmbeddableWithParentWithInheritanceTest.Roquefort.class, - EmbeddableWithParentWithInheritanceTest.Smell.class}) + EmbeddableWithParentWithInheritanceTest.Smell.class +}) +@JiraKey("HHH-16812") public class EmbeddableWithParentWithInheritanceTest { + @Test + public void test(SessionFactoryScope scope) { + Cheese roquefort = new Cheese(); - private static final int COUNT = 4; - - @Test public void test(SessionFactoryScope scope) { - Long savedId = scope.fromSession(s -> { - s.getSession().beginTransaction(); - - Roquefort cheese = new Roquefort(); + scope.inTransaction( s -> { Smell smell = new Smell(); - cheese.setSmell(smell); - smell.setCheese(cheese); - smell.setName("blue cheese"); + roquefort.setSmell( smell ); + smell.setCheese( roquefort ); + smell.setName( "blue roquefort" ); - s.persist(cheese); - - s.getSession().getTransaction().commit(); + s.persist( roquefort ); + } ); - return cheese.id; - }); - - scope.inSession(s -> { - Food food = s.getReference(Food.class, savedId); - assertFalse(Hibernate.isInitialized(food)); - assertEquals("Smells like blue cheese", food.describe()); - }); + scope.inSession( s -> { + Food food = s.getReference( Food.class, roquefort.getId() ); + assertFalse( Hibernate.isInitialized( food ) ); + Object unproxied = Hibernate.unproxy( food ); + assertThat( unproxied ).isInstanceOf( Cheese.class ); + Cheese cheese = (Cheese) unproxied; + assertThat( cheese.getSmell() ).isNotNull(); + assertThat( cheese.getSmell().getCheese() ).isNotNull(); + } ); } - @Entity - @Cacheable + @Entity(name = "Food") @BatchSize(size = 512) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type") @@ -63,6 +70,8 @@ public class EmbeddableWithParentWithInheritanceTest { public static class Food { Long id; + String description; + public Food() { } @@ -79,30 +88,23 @@ public class EmbeddableWithParentWithInheritanceTest { public void setId(Long id) { this.id = id; } - } - @Entity - @DiscriminatorValue("VEGETABLE") - public static class Vegetable extends Food { - - } - - @Entity - @DiscriminatorValue("CHEESE") - public static class Cheese extends Food { - } - - @Entity - @DiscriminatorValue("ROQUEFORT") - public static class Roquefort extends Cheese { - Smell smell; - - public Roquefort() { + public String getDescription() { + return description; } - @Override - public String describe() { - return "Smells like " + getSmell().getName(); + public void setDescription(String description) { + this.description = description; + } + } + + + @Entity(name = "Roquefort") + @DiscriminatorValue("ROQUEFORT") + public static class Cheese extends Food { + Smell smell; + + public Cheese() { } @Embedded