Skip to content

Commit

Permalink
HHH-19195 Test case using classes described in https://in.relation.to…
Browse files Browse the repository at this point in the history
  • Loading branch information
cigaly authored and mbladel committed Mar 5, 2025
1 parent fbe192a commit 6b04816
Showing 1 changed file with 120 additions and 0 deletions.
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 {
// [...]
}
}

0 comments on commit 6b04816

Please sign in to comment.