Skip to content

Commit 662f86a

Browse files
committed
HHH-12445 Test "null"/"not-null" discriminator values
1 parent 1a2510d commit 662f86a

File tree

2 files changed

+226
-0
lines changed

2 files changed

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

Comments
 (0)