|
| 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.Collection; |
| 10 | +import java.util.HashSet; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.Inheritance; |
| 24 | +import jakarta.persistence.InheritanceType; |
| 25 | +import jakarta.persistence.ManyToOne; |
| 26 | +import jakarta.persistence.OneToMany; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Marco Belladelli |
| 32 | + */ |
| 33 | +@DomainModel( annotatedClasses = { |
| 34 | + TreatedPluralJoinIsNullTest.MainEntity.class, |
| 35 | + TreatedPluralJoinIsNullTest.ParentEntity.class, |
| 36 | + TreatedPluralJoinIsNullTest.ChildEntity.class, |
| 37 | +} ) |
| 38 | +@SessionFactory |
| 39 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17178" ) |
| 40 | +public class TreatedPluralJoinIsNullTest { |
| 41 | + @BeforeAll |
| 42 | + public void setUp(SessionFactoryScope scope) { |
| 43 | + scope.inTransaction( session -> { |
| 44 | + session.persist( new MainEntity() ); |
| 45 | + final MainEntity main = new MainEntity(); |
| 46 | + final ChildEntity child = new ChildEntity( main, "test_child" ); |
| 47 | + session.persist( main ); |
| 48 | + session.persist( child ); |
| 49 | + } ); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterAll |
| 53 | + public void tearDown(SessionFactoryScope scope) { |
| 54 | + scope.inTransaction( session -> { |
| 55 | + session.createMutationQuery( "delete from ParentEntity" ).executeUpdate(); |
| 56 | + session.createMutationQuery( "delete from MainEntity" ).executeUpdate(); |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testIsNull(SessionFactoryScope scope) { |
| 62 | + scope.inTransaction( session -> { |
| 63 | + final MainEntity result = session.createQuery( |
| 64 | + "select m from MainEntity m left join treat(m.parents as ChildEntity) c where c is null", |
| 65 | + MainEntity.class |
| 66 | + ).getSingleResult(); |
| 67 | + assertThat( result.getParents() ).isEmpty(); |
| 68 | + } ); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testIsNotNull(SessionFactoryScope scope) { |
| 73 | + scope.inTransaction( session -> { |
| 74 | + final MainEntity result = session.createQuery( |
| 75 | + "select m from MainEntity m left join treat(m.parents as ChildEntity) c where c is not null", |
| 76 | + MainEntity.class |
| 77 | + ).getSingleResult(); |
| 78 | + assertThat( result.getParents() ).hasSize( 1 ); |
| 79 | + final ParentEntity parent = result.getParents().iterator().next(); |
| 80 | + assertThat( parent ).isInstanceOf( ChildEntity.class ); |
| 81 | + assertThat( ( (ChildEntity) parent ).getData() ).isEqualTo( "test_child" ); |
| 82 | + } ); |
| 83 | + } |
| 84 | + |
| 85 | + @Entity( name = "MainEntity" ) |
| 86 | + public static class MainEntity { |
| 87 | + @Id |
| 88 | + @GeneratedValue |
| 89 | + private Long id; |
| 90 | + |
| 91 | + @OneToMany( mappedBy = "main" ) |
| 92 | + private Collection<ParentEntity> parents = new HashSet<>(); |
| 93 | + |
| 94 | + public Long getId() { |
| 95 | + return id; |
| 96 | + } |
| 97 | + |
| 98 | + public Collection<ParentEntity> getParents() { |
| 99 | + return parents; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Entity( name = "ParentEntity" ) |
| 104 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 105 | + public abstract static class ParentEntity { |
| 106 | + @Id |
| 107 | + @GeneratedValue |
| 108 | + private Long id; |
| 109 | + |
| 110 | + @ManyToOne |
| 111 | + private MainEntity main; |
| 112 | + |
| 113 | + public ParentEntity() { |
| 114 | + } |
| 115 | + |
| 116 | + public ParentEntity(MainEntity main) { |
| 117 | + this.main = main; |
| 118 | + } |
| 119 | + |
| 120 | + public Long getId() { |
| 121 | + return id; |
| 122 | + } |
| 123 | + |
| 124 | + public MainEntity getMain() { |
| 125 | + return main; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Entity( name = "ChildEntity" ) |
| 130 | + public static class ChildEntity extends ParentEntity { |
| 131 | + private String data; |
| 132 | + |
| 133 | + public ChildEntity() { |
| 134 | + } |
| 135 | + |
| 136 | + public ChildEntity(MainEntity main, String data) { |
| 137 | + super( main ); |
| 138 | + this.data = data; |
| 139 | + } |
| 140 | + |
| 141 | + public String getData() { |
| 142 | + return data; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments