5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
- import io .vertx .ext .unit .TestContext ;
8
+ import java .util .ArrayList ;
9
+ import java .util .Collection ;
10
+ import java .util .List ;
11
+
9
12
import org .hibernate .Hibernate ;
10
13
import org .hibernate .bytecode .enhance .spi .interceptor .LazyAttributeLoadingInterceptor ;
11
14
import org .hibernate .engine .spi .PersistentAttributeInterceptable ;
12
15
import org .hibernate .engine .spi .PersistentAttributeInterceptor ;
13
- import org . junit . Ignore ;
16
+
14
17
import org .junit .Test ;
15
18
19
+ import io .vertx .ext .unit .TestContext ;
16
20
import jakarta .persistence .Basic ;
17
21
import jakarta .persistence .Entity ;
18
22
import jakarta .persistence .GeneratedValue ;
21
25
import jakarta .persistence .OneToMany ;
22
26
import jakarta .persistence .Table ;
23
27
import jakarta .persistence .metamodel .Attribute ;
24
- import java .util .ArrayList ;
25
- import java .util .Collection ;
26
- import java .util .List ;
27
28
28
- import static java .util .Collections .singleton ;
29
29
import static jakarta .persistence .CascadeType .PERSIST ;
30
30
import static jakarta .persistence .FetchType .LAZY ;
31
+ import static java .util .Collections .singleton ;
31
32
32
-
33
- @ Ignore // this fails with Hibernate ORM too
33
+ /**
34
+ * Lazy properties only work when the related bytecode enhancement is enabled.
35
+ * We test bytecode enhancements in a separate module, new related tests should be created there.
36
+ * I'm keeping this one because it seems to work and might highlight if something changes in the future.
37
+ */
34
38
public class LazyPropertyTest extends BaseReactiveTest {
35
39
36
40
@ Override
@@ -40,94 +44,146 @@ protected Collection<Class<?>> annotatedEntities() {
40
44
41
45
@ Test
42
46
public void testLazyProperty (TestContext context ) {
43
- Author author1 = new Author ("Iain M. Banks" );
44
- Author author2 = new Author ("Neal Stephenson" );
45
- Book book1 = new Book ("1-85723-235-6" , "Feersum Endjinn" , author1 );
46
- Book book2 = new Book ("0-380-97346-4" , "Cryptonomicon" , author2 );
47
- Book book3 = new Book ("0-553-08853-X" , "Snow Crash" , author2 );
48
- author1 .books .add (book1 );
49
- author2 .books .add (book2 );
50
- author2 .books .add (book3 );
47
+ Author author1 = new Author ( "Iain M. Banks" );
48
+ Author author2 = new Author ( "Neal Stephenson" );
49
+ Book book1 = new Book ( "1-85723-235-6" , "Feersum Endjinn" , author1 );
50
+ Book book2 = new Book ( "0-380-97346-4" , "Cryptonomicon" , author2 );
51
+ Book book3 = new Book ( "0-553-08853-X" , "Snow Crash" , author2 );
52
+ author1 .getBooks () .add ( book1 );
53
+ author2 .getBooks () .add ( book2 );
54
+ author2 .getBooks () .add ( book3 );
51
55
52
56
Attribute <? super Book , ?> Book_isbn = getSessionFactory ().getMetamodel ()
53
- .entity (Book .class ).getAttribute ("isbn" );
54
-
55
- test ( context ,
56
- getSessionFactory ().withTransaction (
57
- //persist the Authors with their Books in a transaction
58
- (session , tx ) -> session .persist (author1 , author2 )
59
- ).thenCompose (
60
- v -> getSessionFactory ().withSession (
61
- //retrieve a Book
62
- session -> session .find (Book .class , book1 .id )
63
- //print its title
64
- .thenCompose ( book -> {
65
- context .assertFalse ( Hibernate .isPropertyInitialized (book , "isbn" ) );
66
- return session .fetch ( book , Book_isbn )
67
- .thenAccept ( isbn -> {
68
- context .assertNotNull ( isbn );
69
- context .assertTrue ( Hibernate .isPropertyInitialized (book , "isbn" ) );
70
- } );
71
- } )
57
+ .entity ( Book .class ).getAttribute ( "isbn" );
58
+
59
+ test ( context , getSessionFactory ()
60
+ .withTransaction ( session -> session .persist ( author1 , author2 ) )
61
+ .thenCompose ( v -> getSessionFactory ()
62
+ .withSession ( session -> session
63
+ .find ( Book .class , book1 .id )
64
+ .thenCompose ( book -> {
65
+ context .assertFalse ( Hibernate .isPropertyInitialized ( book , "isbn" ) );
66
+ return session .fetch ( book , Book_isbn )
67
+ .thenAccept ( isbn -> {
68
+ context .assertNotNull ( isbn );
69
+ context .assertTrue ( Hibernate .isPropertyInitialized ( book , "isbn" ) );
70
+ } );
71
+ } )
72
72
)
73
73
)
74
74
);
75
75
}
76
76
77
- @ Entity (name = "Author" )
78
- @ Table (name = "authors" )
77
+ @ Entity (name = "Author" )
78
+ @ Table (name = "authors" )
79
79
static class Author {
80
- @ Id @ GeneratedValue
81
- Integer id ;
80
+ @ Id
81
+ @ GeneratedValue
82
+ private Integer id ;
82
83
83
- String name ;
84
+ private String name ;
84
85
85
86
@ OneToMany (mappedBy = "author" , cascade = PERSIST )
86
- List <Book > books = new ArrayList <>();
87
+ private List <Book > books = new ArrayList <>();
87
88
88
- Author (String name ) {
89
+ public Author (String name ) {
89
90
this .name = name ;
90
91
}
91
92
92
- Author () {}
93
+ public Author () {
94
+ }
95
+
96
+ public Integer getId () {
97
+ return id ;
98
+ }
99
+
100
+ public void setId (Integer id ) {
101
+ this .id = id ;
102
+ }
103
+
104
+ public String getName () {
105
+ return name ;
106
+ }
107
+
108
+ public void setName (String name ) {
109
+ this .name = name ;
110
+ }
111
+
112
+ public List <Book > getBooks () {
113
+ return books ;
114
+ }
115
+
116
+ public void setBooks (List <Book > books ) {
117
+ this .books = books ;
118
+ }
93
119
}
94
120
95
- @ Entity (name = "Book" )
96
- @ Table (name = "books" )
121
+ @ Entity (name = "Book" )
122
+ @ Table (name = "books" )
97
123
static class Book extends LazyAttributeLoadingInterceptor
98
124
implements PersistentAttributeInterceptable {
99
125
@ Id
100
126
@ GeneratedValue
101
- Integer id ;
127
+ private Integer id ;
102
128
103
129
@ Basic (fetch = LAZY )
104
- String isbn ;
130
+ private String isbn ;
105
131
106
- public String getIsbn () {
132
+ private String getIsbn () {
107
133
return isbn ;
108
134
}
109
135
110
- String title ;
136
+ private String title ;
111
137
112
138
@ ManyToOne (fetch = LAZY )
113
- Author author ;
139
+ private Author author ;
114
140
115
- Book (String isbn , String title , Author author ) {
116
- super ("Book" , 1 , singleton ("isbn" ), null );
141
+ public Book (String isbn , String title , Author author ) {
142
+ super ( "Book" , 1 , singleton ( "isbn" ), null );
117
143
this .title = title ;
118
144
this .isbn = isbn ;
119
145
this .author = author ;
120
146
}
121
147
122
- Book () {
123
- super ("Book" , 1 , singleton ("isbn" ), null );
148
+ public Book () {
149
+ super ( "Book" , 1 , singleton ( "isbn" ), null );
124
150
}
125
151
126
152
@ Override
127
153
public PersistentAttributeInterceptor $$_hibernate_getInterceptor () {
128
154
return this ;
129
155
}
156
+
130
157
@ Override
131
- public void $$_hibernate_setInterceptor (PersistentAttributeInterceptor interceptor ) {}
158
+ public void $$_hibernate_setInterceptor (PersistentAttributeInterceptor interceptor ) {
159
+ }
160
+
161
+ public Integer getId () {
162
+ return id ;
163
+ }
164
+
165
+ public void setId (Integer id ) {
166
+ this .id = id ;
167
+ }
168
+
169
+ public void setIsbn (String isbn ) {
170
+ this .isbn = isbn ;
171
+ }
172
+
173
+ public String getTitle () {
174
+ return title ;
175
+ }
176
+
177
+ public void setTitle (String title ) {
178
+ this .title = title ;
179
+ }
180
+
181
+ public Author getAuthor () {
182
+ return author ;
183
+ }
184
+
185
+ public void setAuthor (Author author ) {
186
+ this .author = author ;
187
+ }
132
188
}
133
189
}
0 commit comments