HHH-16812 add test for issue

This commit is contained in:
Andrea Boriero 2023-07-13 16:45:35 +02:00 committed by Andrea Boriero
parent d5819c71f3
commit d69138f1b9
2 changed files with 214 additions and 53 deletions

View File

@ -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;
}
}
}

View File

@ -1,61 +1,68 @@
package org.hibernate.orm.test.embeddable; package org.hibernate.orm.test.embeddable;
import jakarta.persistence.*;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.annotations.*; import org.hibernate.annotations.BatchSize;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.annotations.Parent;
import org.hibernate.testing.orm.junit.DomainModel; 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.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.HashSet; import jakarta.persistence.DiscriminatorColumn;
import java.util.Set; 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 * @author gtoison
*
*/ */
@SessionFactory @SessionFactory
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
EmbeddableWithParentWithInheritanceTest.Food.class, EmbeddableWithParentWithInheritanceTest.Food.class,
EmbeddableWithParentWithInheritanceTest.Cheese.class, EmbeddableWithParentWithInheritanceTest.Cheese.class,
EmbeddableWithParentWithInheritanceTest.Roquefort.class, EmbeddableWithParentWithInheritanceTest.Smell.class
EmbeddableWithParentWithInheritanceTest.Smell.class}) })
@JiraKey("HHH-16812")
public class EmbeddableWithParentWithInheritanceTest { public class EmbeddableWithParentWithInheritanceTest {
@Test
public void test(SessionFactoryScope scope) {
Cheese roquefort = new Cheese();
private static final int COUNT = 4; scope.inTransaction( s -> {
@Test public void test(SessionFactoryScope scope) {
Long savedId = scope.fromSession(s -> {
s.getSession().beginTransaction();
Roquefort cheese = new Roquefort();
Smell smell = new Smell(); Smell smell = new Smell();
cheese.setSmell(smell); roquefort.setSmell( smell );
smell.setCheese(cheese); smell.setCheese( roquefort );
smell.setName("blue cheese"); smell.setName( "blue roquefort" );
s.persist(cheese); s.persist( roquefort );
s.getSession().getTransaction().commit();
return cheese.id;
} ); } );
scope.inSession( s -> { scope.inSession( s -> {
Food food = s.getReference(Food.class, savedId); Food food = s.getReference( Food.class, roquefort.getId() );
assertFalse( Hibernate.isInitialized( food ) ); assertFalse( Hibernate.isInitialized( food ) );
assertEquals("Smells like blue cheese", food.describe()); Object unproxied = Hibernate.unproxy( food );
assertThat( unproxied ).isInstanceOf( Cheese.class );
Cheese cheese = (Cheese) unproxied;
assertThat( cheese.getSmell() ).isNotNull();
assertThat( cheese.getSmell().getCheese() ).isNotNull();
} ); } );
} }
@Entity @Entity(name = "Food")
@Cacheable
@BatchSize(size = 512) @BatchSize(size = 512)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type") @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type")
@ -63,6 +70,8 @@ public class EmbeddableWithParentWithInheritanceTest {
public static class Food { public static class Food {
Long id; Long id;
String description;
public Food() { public Food() {
} }
@ -79,30 +88,23 @@ public class EmbeddableWithParentWithInheritanceTest {
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
public String getDescription() {
return description;
} }
@Entity public void setDescription(String description) {
@DiscriminatorValue("VEGETABLE") this.description = description;
public static class Vegetable extends Food { }
} }
@Entity
@DiscriminatorValue("CHEESE")
public static class Cheese extends Food {
}
@Entity @Entity(name = "Roquefort")
@DiscriminatorValue("ROQUEFORT") @DiscriminatorValue("ROQUEFORT")
public static class Roquefort extends Cheese { public static class Cheese extends Food {
Smell smell; Smell smell;
public Roquefort() { public Cheese() {
}
@Override
public String describe() {
return "Smells like " + getSmell().getName();
} }
@Embedded @Embedded