Skip to content

Commit 8ca24d9

Browse files
committed
HHH-5798 - DiscriminatorValue on abstract entity classes
1 parent 6be7d76 commit 8ca24d9

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.inheritance;
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 org.hibernate.boot.MetadataSources;
14+
import org.hibernate.engine.spi.SessionFactoryImplementor;
15+
import org.hibernate.testing.orm.junit.Jira;
16+
import org.hibernate.testing.orm.junit.ServiceRegistry;
17+
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
/**
21+
* Tests that abstract classes within the hierarchy do not need {@linkplain DiscriminatorValue}
22+
*
23+
* @author Steve Ebersole
24+
*/
25+
@Jira( "https://hibernate.atlassian.net/browse/HHH-5798" )
26+
@ServiceRegistry
27+
public class SingleTableAbstractDiscriminatorTests {
28+
@Test
29+
void testCompliantModel(ServiceRegistryScope registryScope) {
30+
var model = new MetadataSources( registryScope.getRegistry() )
31+
.addAnnotatedClasses( Top.class, Middle.class, Bottom.class )
32+
.buildMetadata();
33+
try (var sf = (SessionFactoryImplementor) model.buildSessionFactory()) {
34+
}
35+
}
36+
37+
@Test
38+
void testNonCompliantModel(ServiceRegistryScope registryScope) {
39+
// technically, according to JPA, this one should fail -
40+
//
41+
// > The DiscriminatorValue annotation can only be specified on a concrete entity class.
42+
//
43+
// we do not validate this though
44+
var model = new MetadataSources( registryScope.getRegistry() )
45+
.addAnnotatedClasses( Root.class, Trunk.class, Branch.class )
46+
.buildMetadata();
47+
try (var sf = (SessionFactoryImplementor) model.buildSessionFactory()) {
48+
}
49+
}
50+
51+
@Entity
52+
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
53+
@DiscriminatorColumn
54+
public static abstract class Top {
55+
@Id
56+
private Integer id;
57+
private String name;
58+
}
59+
60+
@Entity
61+
public static abstract class Middle extends Top {
62+
}
63+
64+
@Entity
65+
@DiscriminatorValue( "B" )
66+
public static class Bottom extends Middle {
67+
}
68+
69+
@Entity
70+
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
71+
@DiscriminatorColumn
72+
public static abstract class Root {
73+
@Id
74+
private Integer id;
75+
private String name;
76+
}
77+
78+
@Entity
79+
@DiscriminatorValue( "illegal" )
80+
public static abstract class Trunk extends Root {
81+
}
82+
83+
@Entity
84+
@DiscriminatorValue( "b" )
85+
public static class Branch extends Trunk {
86+
@Id
87+
private Integer id;
88+
private String name;
89+
}
90+
}

0 commit comments

Comments
 (0)