44 */
55package org .hibernate .orm .test .inheritance ;
66
7- import java .util .List ;
8-
9- import org .hibernate .testing .orm .junit .DomainModel ;
10- import org .hibernate .testing .orm .junit .Jira ;
11- import org .hibernate .testing .orm .junit .SessionFactory ;
12- import org .hibernate .testing .orm .junit .SessionFactoryScope ;
13- import org .junit .jupiter .api .AfterAll ;
14- import org .junit .jupiter .api .Test ;
15-
167import jakarta .persistence .Entity ;
178import jakarta .persistence .FetchType ;
18- import jakarta .persistence .GeneratedValue ;
199import jakarta .persistence .Id ;
2010import jakarta .persistence .Inheritance ;
2111import jakarta .persistence .InheritanceType ;
2212import jakarta .persistence .ManyToMany ;
2313import jakarta .persistence .ManyToOne ;
14+ import org .hibernate .testing .orm .junit .DomainModel ;
15+ import org .hibernate .testing .orm .junit .Jira ;
16+ import org .hibernate .testing .orm .junit .SessionFactory ;
17+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18+ import org .junit .jupiter .api .AfterAll ;
19+ import org .junit .jupiter .api .BeforeEach ;
20+ import org .junit .jupiter .api .Test ;
21+
22+ import java .util .List ;
2423
2524import static org .assertj .core .api .Assertions .assertThat ;
2625
26+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
2727@ DomainModel ( annotatedClasses = {
2828 // The user entities come first, so they get the lowest discriminator values
2929 JoinedInheritanceCollectionSameHierarchyTest .UserEntity .class ,
3939public class JoinedInheritanceCollectionSameHierarchyTest {
4040 @ Test
4141 public void testGetDiscriminatorCollection (SessionFactoryScope scope ) {
42- final Long id = scope .fromTransaction ( session -> {
43- final UserEntity user = new UserEntity ( "test_user" );
44- session .persist ( user );
45- final GoodCompany company = new GoodCompany ();
46- company .employee = user ;
47- session .persist ( company );
48- final CompanyRegistry companyRegistry = new CompanyRegistry ( List .of ( company ) );
49- session .persist ( companyRegistry );
50- return companyRegistry .id ;
51- } );
5242 scope .inSession ( session -> {
53- final CompanyRegistry companyRegistry = session .get ( CompanyRegistry .class , id );
43+ final CompanyRegistry companyRegistry = session .find ( CompanyRegistry .class , 30 );
5444 assertThat ( companyRegistry .getCompanies () ).hasSize ( 1 )
5545 .extracting ( AbstractCompany ::getEmployee )
5646 .extracting ( UserEntity ::getName )
5747 .containsOnly ( "test_user" );
5848 } );
5949 }
6050
61- @ AfterAll
62- public void tearDown (SessionFactoryScope scope ) {
63- scope .inTransaction ( session -> {
64- session .createMutationQuery ( "delete from CompanyRegistry" ).executeUpdate ();
65- session .createMutationQuery ( "delete from AbstractCompany" ).executeUpdate ();
66- session .createMutationQuery ( "delete from SuperEntity" ).executeUpdate ();
51+ @ BeforeEach
52+ void createTestData (SessionFactoryScope sessions ) {
53+ sessions .inTransaction ( session -> {
54+ final UserEntity user = new UserEntity ( 1 , "test_user" );
55+ session .persist ( user );
56+ final GoodCompany company = new GoodCompany ( 20 );
57+ company .employee = user ;
58+ session .persist ( company );
59+ final CompanyRegistry companyRegistry = new CompanyRegistry ( 30 , List .of ( company ) );
60+ session .persist ( companyRegistry );
6761 } );
6862 }
6963
64+ @ AfterAll
65+ public void dropTestData (SessionFactoryScope sessions ) {
66+ sessions .dropData ();
67+ }
68+
7069 @ Entity ( name = "SuperEntity" )
7170 @ Inheritance ( strategy = InheritanceType .JOINED )
7271 static abstract class SuperEntity {
7372 @ Id
74- @ GeneratedValue
75- Long id ;
73+ Integer id ;
74+
75+ public SuperEntity () {
76+ }
77+
78+ public SuperEntity (Integer id ) {
79+ this .id = id ;
80+ }
7681 }
7782
7883 @ Entity ( name = "CompanyRegistry" )
@@ -83,7 +88,8 @@ static class CompanyRegistry extends SuperEntity {
8388 public CompanyRegistry () {
8489 }
8590
86- public CompanyRegistry (List <AbstractCompany > companies ) {
91+ public CompanyRegistry (Integer id , List <AbstractCompany > companies ) {
92+ super ( id );
8793 this .companies = companies ;
8894 }
8995
@@ -97,23 +103,50 @@ abstract static class AbstractCompany extends SuperEntity {
97103 @ ManyToOne ( fetch = FetchType .LAZY )
98104 UserEntity employee ;
99105
106+ public AbstractCompany () {
107+ }
108+
109+ public AbstractCompany (Integer id ) {
110+ super ( id );
111+ }
112+
100113 public UserEntity getEmployee () {
101114 return employee ;
102115 }
103116 }
104117
105118 @ Entity ( name = "GoodCompany" )
106119 static class GoodCompany extends AbstractCompany {
120+ public GoodCompany () {
121+ }
122+
123+ public GoodCompany (Integer id ) {
124+ super ( id );
125+ }
107126 }
108127
109128 @ Entity ( name = "BadCompany" )
110129 static class BadCompany extends AbstractCompany {
111130 // (unused) sibling subtype for 'GoodCompany'
131+
132+ public BadCompany () {
133+ }
134+
135+ public BadCompany (Integer id ) {
136+ super ( id );
137+ }
112138 }
113139
114140 @ Entity ( name = "AbstractUser" )
115141 static class BaseUser extends SuperEntity {
116142 // necessary intermediate entity so 'BaseUser' is the first child type and gets the lowest discriminator value
143+
144+ public BaseUser () {
145+ }
146+
147+ public BaseUser (Integer id ) {
148+ super ( id );
149+ }
117150 }
118151
119152 @ Entity ( name = "UserEntity" )
@@ -123,7 +156,8 @@ static class UserEntity extends BaseUser {
123156 public UserEntity () {
124157 }
125158
126- public UserEntity (String name ) {
159+ public UserEntity (Integer id , String name ) {
160+ super ( id );
127161 this .name = name ;
128162 }
129163
0 commit comments