44 */
55package org .hibernate .orm .test .bytecode .enhancement .access ;
66
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-
157import jakarta .persistence .Access ;
168import jakarta .persistence .AccessType ;
17- import jakarta .persistence .Basic ;
189import jakarta .persistence .DiscriminatorColumn ;
19- import jakarta .persistence .DiscriminatorValue ;
2010import jakarta .persistence .Entity ;
2111import jakarta .persistence .Id ;
22- import jakarta .persistence .Inheritance ;
23- import jakarta .persistence .Table ;
12+ import jakarta .persistence .MappedSuperclass ;
2413import 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 ;
2521
2622import static org .assertj .core .api .Assertions .assertThat ;
2723
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+ } )
3329@ SessionFactory
34- @ JiraKey ("HHH-19140" )
30+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-19140" )
31+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-19059" )
3532@ BytecodeEnhanced
3633public class HierarchyPropertyAccessTest {
37-
38-
3934 @ Test
4035 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" ) ) );
4439
4540 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 );
4945
5046 entity .setProperty ( "transient: updated" );
5147 } );
5248
5349 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" );
5753 } );
5854 }
5955
6056 @ Test
6157 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" ) ) );
6561
6662 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 );
7067
7168 entity .setProperty ( "transient: updated" );
7269 } );
7370
7471 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" );
7875 } );
7976 }
8077
81- @ AfterEach
78+ @ AfterAll
8279 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 ;
9386 }
9487
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 {
10191 @ Id
102- Long id ;
92+ private Long id ;
10393
104- @ Basic
105- String field ;
94+ private String field ;
10695
107- String persistProperty ;
96+ private String persistProperty ;
10897
10998 @ Transient
110- String property ;
99+ private String property ;
111100
112101 public ParentEntity () {
113102 }
@@ -118,7 +107,6 @@ public ParentEntity(Long id, String field, String property) {
118107 this .property = property ;
119108 }
120109
121- @ Basic
122110 @ Access (AccessType .PROPERTY )
123111 public String getPersistProperty () {
124112 this .persistProperty = this .property .substring ( 11 );
@@ -137,17 +125,24 @@ public String getProperty() {
137125 public void setProperty (String property ) {
138126 this .property = property ;
139127 }
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+ }
140137 }
141138
142- @ Entity
143- @ DiscriminatorValue ("Child" )
139+ @ Entity (name = "ChildEntity" )
144140 static class ChildEntity extends ParentEntity {
145-
146141 public ChildEntity () {
147142 }
148143
149144 public ChildEntity (Long id , String field , String property ) {
150- super (id , field , property );
145+ super ( id , field , property );
151146 }
152147 }
153148}
0 commit comments