4
4
*/
5
5
package org .hibernate .orm .test .bytecode .enhancement .access ;
6
6
7
- import org .hibernate .testing .bytecode .enhancement .extension .BytecodeEnhanced ;
8
- import org .hibernate .testing .orm .junit .DomainModel ;
9
- import org .hibernate .testing .orm .junit .JiraKey ;
10
- import org .hibernate .testing .orm .junit .SessionFactory ;
11
- import org .hibernate .testing .orm .junit .SessionFactoryScope ;
12
- import org .junit .jupiter .api .AfterEach ;
13
- import org .junit .jupiter .api .Test ;
14
-
15
7
import jakarta .persistence .Access ;
16
8
import jakarta .persistence .AccessType ;
17
- import jakarta .persistence .Basic ;
18
9
import jakarta .persistence .DiscriminatorColumn ;
19
- import jakarta .persistence .DiscriminatorValue ;
20
10
import jakarta .persistence .Entity ;
21
11
import jakarta .persistence .Id ;
22
- import jakarta .persistence .Inheritance ;
23
- import jakarta .persistence .Table ;
12
+ import jakarta .persistence .MappedSuperclass ;
24
13
import jakarta .persistence .Transient ;
14
+ import org .hibernate .testing .bytecode .enhancement .extension .BytecodeEnhanced ;
15
+ import org .hibernate .testing .orm .junit .DomainModel ;
16
+ import org .hibernate .testing .orm .junit .Jira ;
17
+ import org .hibernate .testing .orm .junit .SessionFactory ;
18
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
19
+ import org .junit .jupiter .api .AfterAll ;
20
+ import org .junit .jupiter .api .Test ;
25
21
26
22
import static org .assertj .core .api .Assertions .assertThat ;
27
23
28
- @ DomainModel (
29
- annotatedClasses = {
30
- HierarchyPropertyAccessTest .ChildEntity .class ,
31
- }
32
- )
24
+ @ DomainModel (annotatedClasses = {
25
+ HierarchyPropertyAccessTest . AbstractSuperclass . class ,
26
+ HierarchyPropertyAccessTest .ParentEntity .class ,
27
+ HierarchyPropertyAccessTest . ChildEntity . class ,
28
+ } )
33
29
@ SessionFactory
34
- @ JiraKey ("HHH-19140" )
30
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-19140" )
31
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-19059" )
35
32
@ BytecodeEnhanced
36
33
public class HierarchyPropertyAccessTest {
37
-
38
-
39
34
@ Test
40
35
public void testParent (SessionFactoryScope scope ) {
41
- scope .inTransaction ( session -> {
42
- session . persist ( new ParentEntity ( 1L , "field" , "transient: property" ) );
43
- } );
36
+ assertThat ( scope .getSessionFactory (). getMappingMetamodel (). findEntityDescriptor ( ParentEntity . class )
37
+ . getBytecodeEnhancementMetadata (). isEnhancedForLazyLoading () ). isTrue ( );
38
+ scope . inTransaction ( session -> session . persist ( new ParentEntity ( 1L , "field" , "transient: property" ) ) );
44
39
45
40
scope .inTransaction ( session -> {
46
- ParentEntity entity = session .get ( ParentEntity .class , 1L );
47
- assertThat ( entity .persistProperty ).isEqualTo ( "property" );
48
- assertThat ( entity .property ).isEqualTo ( "transient: property" );
41
+ final ParentEntity entity = session .find ( ParentEntity .class , 1L );
42
+ assertThat ( entity .getPersistProperty () ).isEqualTo ( "property" );
43
+ assertThat ( entity .getProperty () ).isEqualTo ( "transient: property" );
44
+ assertThat ( entity .getSuperProperty () ).isEqualTo ( 8 );
49
45
50
46
entity .setProperty ( "transient: updated" );
51
47
} );
52
48
53
49
scope .inTransaction ( session -> {
54
- ParentEntity entity = session .get ( ParentEntity .class , 1L );
55
- assertThat ( entity .persistProperty ).isEqualTo ( "updated" );
56
- assertThat ( entity .property ).isEqualTo ( "transient: updated" );
50
+ final ParentEntity entity = session .find ( ParentEntity .class , 1L );
51
+ assertThat ( entity .getPersistProperty () ).isEqualTo ( "updated" );
52
+ assertThat ( entity .getProperty () ).isEqualTo ( "transient: updated" );
57
53
} );
58
54
}
59
55
60
56
@ Test
61
57
public void testChild (SessionFactoryScope scope ) {
62
- scope .inTransaction ( session -> {
63
- session . persist ( new ChildEntity ( 2L , "field" , "transient: property" ) );
64
- } );
58
+ assertThat ( scope .getSessionFactory (). getMappingMetamodel (). findEntityDescriptor ( ChildEntity . class )
59
+ . getBytecodeEnhancementMetadata (). isEnhancedForLazyLoading () ). isTrue ( );
60
+ scope . inTransaction ( session -> session . persist ( new ChildEntity ( 2L , "field" , "transient: property" ) ) );
65
61
66
62
scope .inTransaction ( session -> {
67
- ChildEntity entity = session .get ( ChildEntity .class , 2L );
68
- assertThat ( entity .persistProperty ).isEqualTo ( "property" );
69
- assertThat ( entity .property ).isEqualTo ( "transient: property" );
63
+ ChildEntity entity = session .find ( ChildEntity .class , 2L );
64
+ assertThat ( entity .getPersistProperty () ).isEqualTo ( "property" );
65
+ assertThat ( entity .getProperty () ).isEqualTo ( "transient: property" );
66
+ assertThat ( entity .getSuperProperty () ).isEqualTo ( 8 );
70
67
71
68
entity .setProperty ( "transient: updated" );
72
69
} );
73
70
74
71
scope .inTransaction ( session -> {
75
- ChildEntity entity = session .get ( ChildEntity .class , 2L );
76
- assertThat ( entity .persistProperty ).isEqualTo ( "updated" );
77
- assertThat ( entity .property ).isEqualTo ( "transient: updated" );
72
+ ChildEntity entity = session .find ( ChildEntity .class , 2L );
73
+ assertThat ( entity .getPersistProperty () ).isEqualTo ( "updated" );
74
+ assertThat ( entity .getProperty () ).isEqualTo ( "transient: updated" );
78
75
} );
79
76
}
80
77
81
- @ AfterEach
78
+ @ AfterAll
82
79
public void cleanup (SessionFactoryScope scope ) {
83
- scope .inTransaction ( session -> {
84
- ParentEntity parentEntity = session .get ( ParentEntity .class , 1L );
85
- if (parentEntity != null ) {
86
- session .remove ( parentEntity );
87
- }
88
- ChildEntity childEntity = session .get ( ChildEntity .class , 2L );
89
- if (childEntity != null ) {
90
- session .remove ( childEntity );
91
- }
92
- } );
80
+ scope .getSessionFactory ().getSchemaManager ().truncateMappedObjects ();
81
+ }
82
+
83
+ @ MappedSuperclass
84
+ static abstract class AbstractSuperclass {
85
+ protected Integer superProperty ;
93
86
}
94
87
95
- @ Entity
96
- @ Table (name = "PARENT_ENTITY" )
97
- @ Inheritance
98
- @ DiscriminatorColumn (name = "type" )
99
- @ DiscriminatorValue ("Parent" )
100
- static class ParentEntity {
88
+ @ Entity (name = "ParentEntity" )
89
+ @ DiscriminatorColumn (name = "entity_type" )
90
+ static class ParentEntity extends AbstractSuperclass {
101
91
@ Id
102
- Long id ;
92
+ private Long id ;
103
93
104
- @ Basic
105
- String field ;
94
+ private String field ;
106
95
107
- String persistProperty ;
96
+ private String persistProperty ;
108
97
109
98
@ Transient
110
- String property ;
99
+ private String property ;
111
100
112
101
public ParentEntity () {
113
102
}
@@ -118,7 +107,6 @@ public ParentEntity(Long id, String field, String property) {
118
107
this .property = property ;
119
108
}
120
109
121
- @ Basic
122
110
@ Access (AccessType .PROPERTY )
123
111
public String getPersistProperty () {
124
112
this .persistProperty = this .property .substring ( 11 );
@@ -137,17 +125,24 @@ public String getProperty() {
137
125
public void setProperty (String property ) {
138
126
this .property = property ;
139
127
}
128
+
129
+ @ Access (AccessType .PROPERTY )
130
+ public Integer getSuperProperty () {
131
+ return getPersistProperty ().length ();
132
+ }
133
+
134
+ public void setSuperProperty (Integer superProperty ) {
135
+ this .superProperty = superProperty ;
136
+ }
140
137
}
141
138
142
- @ Entity
143
- @ DiscriminatorValue ("Child" )
139
+ @ Entity (name = "ChildEntity" )
144
140
static class ChildEntity extends ParentEntity {
145
-
146
141
public ChildEntity () {
147
142
}
148
143
149
144
public ChildEntity (Long id , String field , String property ) {
150
- super (id , field , property );
145
+ super ( id , field , property );
151
146
}
152
147
}
153
148
}
0 commit comments