|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.inheritance; |
| 8 | + |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.Jira; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.AfterAll; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Inheritance; |
| 23 | +import jakarta.persistence.InheritanceType; |
| 24 | +import jakarta.persistence.ManyToOne; |
| 25 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 26 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 27 | +import jakarta.persistence.criteria.Root; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Marco Belladelli |
| 33 | + */ |
| 34 | +@DomainModel( annotatedClasses = { |
| 35 | + TreatedSubclassSameTypeTest.MyEntity1.class, |
| 36 | + TreatedSubclassSameTypeTest.MyEntity2.class, |
| 37 | + TreatedSubclassSameTypeTest.MySubEntity1.class, |
| 38 | +} ) |
| 39 | +@SessionFactory |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17299" ) |
| 41 | +public class TreatedSubclassSameTypeTest { |
| 42 | + @BeforeAll |
| 43 | + public void setUp(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + session.persist( new MyEntity1() ); |
| 46 | + final MyEntity2 entity2 = new MyEntity2( "entity2" ); |
| 47 | + final MySubEntity1 entity1 = new MySubEntity1( entity2 ); |
| 48 | + session.persist( entity2 ); |
| 49 | + session.persist( entity1 ); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterAll |
| 54 | + public void tearDown(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( session -> { |
| 56 | + session.createMutationQuery( "delete from MyEntity1" ).executeUpdate(); |
| 57 | + session.createMutationQuery( "delete from MyEntity2" ).executeUpdate(); |
| 58 | + } ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testJoinOnTreat(SessionFactoryScope scope) { |
| 63 | + executeQuery( scope, criteria -> criteria.from( MyEntity1.class ), true ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testJoin(SessionFactoryScope scope) { |
| 68 | + executeQuery( scope, criteria -> criteria.from( MyEntity1.class ), false ); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testJoinOnTreatedSubtype(SessionFactoryScope scope) { |
| 73 | + executeQuery( scope, criteria -> criteria.from( MySubEntity1.class ), true ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testJoinOnSubtype(SessionFactoryScope scope) { |
| 78 | + executeQuery( scope, criteria -> criteria.from( MySubEntity1.class ), false ); |
| 79 | + } |
| 80 | + |
| 81 | + @SuppressWarnings( "unchecked" ) |
| 82 | + private void executeQuery( |
| 83 | + SessionFactoryScope scope, |
| 84 | + Function<CriteriaQuery<MyEntity1>, Root<? extends MyEntity1>> rootSupplier, |
| 85 | + boolean treat) { |
| 86 | + scope.inTransaction( session -> { |
| 87 | + final CriteriaBuilder cb = session.getCriteriaBuilder(); |
| 88 | + final CriteriaQuery<MyEntity1> criteria = cb.createQuery( MyEntity1.class ); |
| 89 | + final Root<? extends MyEntity1> root = rootSupplier.apply( criteria ); |
| 90 | + final Root<MySubEntity1> subRoot; |
| 91 | + if ( treat ) { |
| 92 | + subRoot = cb.treat( ( (Root<MyEntity1>) root ), MySubEntity1.class ); |
| 93 | + } |
| 94 | + else { |
| 95 | + subRoot = (Root<MySubEntity1>) root; |
| 96 | + } |
| 97 | + criteria.where( cb.equal( subRoot.join( "ref" ).get( "name" ), "entity2" ) ); |
| 98 | + final MyEntity1 result = session.createQuery( criteria ).getSingleResult(); |
| 99 | + assertThat( result ).isInstanceOf( MySubEntity1.class ); |
| 100 | + assertThat( ( (MySubEntity1) result ).getRef().getName() ).isEqualTo( "entity2" ); |
| 101 | + } ); |
| 102 | + } |
| 103 | + |
| 104 | + @Entity( name = "MyEntity1" ) |
| 105 | + @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) |
| 106 | + public static class MyEntity1 { |
| 107 | + @Id |
| 108 | + @GeneratedValue |
| 109 | + private Long id; |
| 110 | + } |
| 111 | + |
| 112 | + @Entity( name = "MySubEntity1" ) |
| 113 | + public static class MySubEntity1 extends MyEntity1 { |
| 114 | + @ManyToOne |
| 115 | + private MyEntity2 ref; |
| 116 | + |
| 117 | + public MySubEntity1() { |
| 118 | + } |
| 119 | + |
| 120 | + public MySubEntity1(MyEntity2 ref) { |
| 121 | + this.ref = ref; |
| 122 | + } |
| 123 | + |
| 124 | + public MyEntity2 getRef() { |
| 125 | + return ref; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Entity( name = "MyEntity2" ) |
| 130 | + public static class MyEntity2 { |
| 131 | + @Id |
| 132 | + @GeneratedValue |
| 133 | + private Long id; |
| 134 | + |
| 135 | + private String name; |
| 136 | + |
| 137 | + public MyEntity2() { |
| 138 | + } |
| 139 | + |
| 140 | + public MyEntity2(String name) { |
| 141 | + this.name = name; |
| 142 | + } |
| 143 | + |
| 144 | + public String getName() { |
| 145 | + return name; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments