|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.inheritance.discriminator; |
| 6 | + |
| 7 | +import jakarta.persistence.DiscriminatorColumn; |
| 8 | +import jakarta.persistence.DiscriminatorValue; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.Inheritance; |
| 12 | +import jakarta.persistence.InheritanceType; |
| 13 | +import jakarta.persistence.Table; |
| 14 | +import jakarta.persistence.Tuple; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.Jira; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +@DomainModel(annotatedClasses = { |
| 28 | + JoinedDiscSameAttributeNameTest.Ancestor.class, |
| 29 | + JoinedDiscSameAttributeNameTest.DescendantA.class, |
| 30 | + JoinedDiscSameAttributeNameTest.DescendantB.class, |
| 31 | + JoinedDiscSameAttributeNameTest.DescendantTak.class, |
| 32 | + JoinedDiscSameAttributeNameTest.DescendantD.class, |
| 33 | +}) |
| 34 | +@SessionFactory |
| 35 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-19756" ) |
| 36 | +public class JoinedDiscSameAttributeNameTest { |
| 37 | + @Test |
| 38 | + void testCoalesceSameType(SessionFactoryScope scope) { |
| 39 | + scope.inTransaction( session -> { |
| 40 | + final var cb = session.getCriteriaBuilder(); |
| 41 | + final var query = cb.createTupleQuery(); |
| 42 | + final var root = query.from( Ancestor.class ); |
| 43 | + final var dscCRoot = cb.treat( root, DescendantTak.class ); |
| 44 | + |
| 45 | + query.select( cb.tuple( |
| 46 | + root.get( JoinedDiscSameAttributeNameTest_.Ancestor_.id ).alias( "id" ), |
| 47 | + cb.coalesce( |
| 48 | + dscCRoot.get( JoinedDiscSameAttributeNameTest_.DescendantTak_.subtitle ), |
| 49 | + dscCRoot.get( JoinedDiscSameAttributeNameTest_.DescendantTak_.title ) |
| 50 | + ).alias( "description" ) |
| 51 | + ) ).orderBy( cb.asc( root.get( JoinedDiscSameAttributeNameTest_.Ancestor_.id ) ) ); |
| 52 | + |
| 53 | + final var resultList = session.createSelectionQuery( query ).getResultList(); |
| 54 | + assertResults( resultList, null, "title", null ); |
| 55 | + } ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testCoalesceDifferentTypes(SessionFactoryScope scope) { |
| 60 | + scope.inTransaction( session -> { |
| 61 | + final var cb = session.getCriteriaBuilder(); |
| 62 | + final var query = cb.createTupleQuery(); |
| 63 | + final var root = query.from( Ancestor.class ); |
| 64 | + final var dscARoot = cb.treat( root, DescendantA.class ); |
| 65 | + final var dscCRoot = cb.treat( root, DescendantTak.class ); |
| 66 | + final var dscDRoot = cb.treat( root, DescendantD.class ); |
| 67 | + |
| 68 | + query.select( cb.tuple( |
| 69 | + root.get( JoinedDiscSameAttributeNameTest_.Ancestor_.id ).alias( "id" ), |
| 70 | + cb.coalesce( |
| 71 | + dscDRoot.get( JoinedDiscSameAttributeNameTest_.DescendantD_.subtitle ), |
| 72 | + cb.coalesce( |
| 73 | + cb.coalesce( |
| 74 | + dscARoot.get( JoinedDiscSameAttributeNameTest_.DescendantA_.subtitle ), |
| 75 | + dscARoot.get( JoinedDiscSameAttributeNameTest_.DescendantA_.title ) |
| 76 | + ), |
| 77 | + cb.coalesce( |
| 78 | + dscCRoot.get( JoinedDiscSameAttributeNameTest_.DescendantTak_.subtitle ), |
| 79 | + dscCRoot.get( JoinedDiscSameAttributeNameTest_.DescendantTak_.title ) |
| 80 | + ) |
| 81 | + ) |
| 82 | + ).alias( "description" ) |
| 83 | + ) ).orderBy( cb.asc( root.get( JoinedDiscSameAttributeNameTest_.Ancestor_.id ) ) ); |
| 84 | + |
| 85 | + final var resultList = session.createSelectionQuery( query ).getResultList(); |
| 86 | + assertResults( resultList, null, "title", "subtitle" ); |
| 87 | + } ); |
| 88 | + } |
| 89 | + |
| 90 | + private static void assertResults(List<Tuple> resultList, String... expected) { |
| 91 | + assertThat( resultList ).hasSize( expected.length ); |
| 92 | + for ( int i = 0; i < expected.length; i++ ) { |
| 93 | + final var r = resultList.get( i ); |
| 94 | + assertThat( r.get( 0, Integer.class) ).isEqualTo( i + 1 ); |
| 95 | + assertThat( r.get( 1, String.class ) ).isEqualTo( expected[i] ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @BeforeAll |
| 100 | + public void setUp(SessionFactoryScope scope) { |
| 101 | + scope.inTransaction( session -> { |
| 102 | + final var descendantA = new DescendantA(); |
| 103 | + descendantA.id = 1; |
| 104 | + session.persist( descendantA ); |
| 105 | + final var descendantTak = new DescendantTak(); |
| 106 | + descendantTak.id = 2; |
| 107 | + descendantTak.title = "title"; |
| 108 | + session.persist( descendantTak ); |
| 109 | + final var descendantD = new DescendantD(); |
| 110 | + descendantD.id = 3; |
| 111 | + descendantD.subtitle = "subtitle"; |
| 112 | + session.persist( descendantD ); |
| 113 | + } ); |
| 114 | + } |
| 115 | + |
| 116 | + @AfterAll |
| 117 | + public void tearDown(SessionFactoryScope scope) { |
| 118 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 119 | + } |
| 120 | + |
| 121 | + @Entity(name = "Ancestor") |
| 122 | + @Table(name = "t_ancestor") |
| 123 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 124 | + @DiscriminatorColumn(name = "def_type_id") |
| 125 | + static abstract class Ancestor { |
| 126 | + @Id |
| 127 | + Integer id; |
| 128 | + } |
| 129 | + |
| 130 | + @Entity(name = "DescendantA") |
| 131 | + @DiscriminatorValue("A") |
| 132 | + @Table(name = "t_descendant_a") |
| 133 | + static class DescendantA extends Ancestor { |
| 134 | + String title; |
| 135 | + String subtitle; |
| 136 | + } |
| 137 | + |
| 138 | + @Entity(name = "DescendantB") |
| 139 | + @Table(name = "t_descendant_b") |
| 140 | + static abstract class DescendantB extends Ancestor { |
| 141 | + } |
| 142 | + |
| 143 | + @Entity(name = "DescendantTak") |
| 144 | + @DiscriminatorValue("C") |
| 145 | + @Table(name = "t_descendant_c") |
| 146 | + static class DescendantTak extends DescendantB { |
| 147 | + String title; |
| 148 | + String subtitle; |
| 149 | + } |
| 150 | + |
| 151 | + @Entity(name = "DescendantD") |
| 152 | + @DiscriminatorValue("D") |
| 153 | + @Table(name = "t_descendant_d") |
| 154 | + static class DescendantD extends DescendantB { |
| 155 | + String subtitle; |
| 156 | + } |
| 157 | +} |
0 commit comments