|
| 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.test.inheritance.discriminator.joinedsubclass; |
| 8 | + |
| 9 | +import java.sql.Statement; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import javax.persistence.DiscriminatorColumn; |
| 14 | +import javax.persistence.DiscriminatorValue; |
| 15 | +import javax.persistence.Entity; |
| 16 | +import javax.persistence.Id; |
| 17 | +import javax.persistence.Inheritance; |
| 18 | +import javax.persistence.InheritanceType; |
| 19 | + |
| 20 | +import org.hibernate.testing.TestForIssue; |
| 21 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +@TestForIssue(jiraKey = "HHH-12445") |
| 27 | +public class JoinedNullNotNullDiscriminatorTest extends BaseCoreFunctionalTestCase { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Class<?>[] getAnnotatedClasses() { |
| 31 | + return new Class<?>[] { |
| 32 | + RootEntity.class, |
| 33 | + Val1Entity.class, |
| 34 | + Val2Entity.class, |
| 35 | + NotNullEntity.class |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void test() { |
| 41 | + inTransaction( session -> { |
| 42 | + Val1Entity val1 = new Val1Entity(); |
| 43 | + val1.setId( 1L ); |
| 44 | + |
| 45 | + Val2Entity val2 = new Val2Entity(); |
| 46 | + val2.setId( 2L ); |
| 47 | + |
| 48 | + RootEntity root = new RootEntity(); |
| 49 | + root.setId( 3L ); |
| 50 | + |
| 51 | + session.persist( val1 ); |
| 52 | + session.persist( val2 ); |
| 53 | + session.persist( root ); |
| 54 | + |
| 55 | + session.doWork( connection -> { |
| 56 | + try (Statement statement = connection.createStatement()) { |
| 57 | + statement.executeUpdate( |
| 58 | + "insert into root_ent (DTYPE, id) " + |
| 59 | + "values ('other', 4)" |
| 60 | + ); |
| 61 | + } |
| 62 | + } ); |
| 63 | + } ); |
| 64 | + |
| 65 | + inTransaction( session -> { |
| 66 | + Map<Long, RootEntity> entities = session.createQuery( |
| 67 | + "select e from root_ent e", RootEntity.class ) |
| 68 | + .getResultList() |
| 69 | + .stream() |
| 70 | + .collect( Collectors.toMap( RootEntity::getId, Function.identity() ) ); |
| 71 | + |
| 72 | + assertThat( entities ).extractingByKey( 1L ).isInstanceOf( Val1Entity.class ); |
| 73 | + assertThat( entities ).extractingByKey( 2L ).isInstanceOf( Val2Entity.class ); |
| 74 | + assertThat( entities ).extractingByKey( 3L ).isInstanceOf( RootEntity.class ); |
| 75 | + assertThat( entities ).extractingByKey( 4L ).isInstanceOf( NotNullEntity.class ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + @Entity(name = "root_ent") |
| 80 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 81 | + @DiscriminatorColumn() |
| 82 | + @DiscriminatorValue("null") |
| 83 | + public static class RootEntity { |
| 84 | + |
| 85 | + @Id |
| 86 | + private Long id; |
| 87 | + |
| 88 | + public Long getId() { |
| 89 | + return id; |
| 90 | + } |
| 91 | + |
| 92 | + public void setId(Long id) { |
| 93 | + this.id = id; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Entity(name = "val1_ent") |
| 98 | + @DiscriminatorValue("val1") |
| 99 | + public static class Val1Entity extends RootEntity { |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Entity(name = "val2_ent") |
| 104 | + @DiscriminatorValue("val2") |
| 105 | + public static class Val2Entity extends RootEntity { |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Entity(name = "notnull_ent") |
| 110 | + @DiscriminatorValue("not null") |
| 111 | + public static class NotNullEntity extends RootEntity { |
| 112 | + |
| 113 | + } |
| 114 | +} |
0 commit comments