Skip to content

Commit d628f60

Browse files
committed
[#1508] Re-enable LazyPropertyTest
1 parent 77451b1 commit d628f60

File tree

1 file changed

+111
-55
lines changed

1 file changed

+111
-55
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyPropertyTest.java

Lines changed: 111 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
*/
66
package org.hibernate.reactive;
77

8-
import io.vertx.ext.unit.TestContext;
8+
import java.util.ArrayList;
9+
import java.util.Collection;
10+
import java.util.List;
11+
912
import org.hibernate.Hibernate;
1013
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
1114
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
1215
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
13-
import org.junit.Ignore;
16+
1417
import org.junit.Test;
1518

19+
import io.vertx.ext.unit.TestContext;
1620
import jakarta.persistence.Basic;
1721
import jakarta.persistence.Entity;
1822
import jakarta.persistence.GeneratedValue;
@@ -21,16 +25,16 @@
2125
import jakarta.persistence.OneToMany;
2226
import jakarta.persistence.Table;
2327
import jakarta.persistence.metamodel.Attribute;
24-
import java.util.ArrayList;
25-
import java.util.Collection;
26-
import java.util.List;
2728

28-
import static java.util.Collections.singleton;
2929
import static jakarta.persistence.CascadeType.PERSIST;
3030
import static jakarta.persistence.FetchType.LAZY;
31+
import static java.util.Collections.singleton;
3132

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+
*/
3438
public class LazyPropertyTest extends BaseReactiveTest {
3539

3640
@Override
@@ -40,94 +44,146 @@ protected Collection<Class<?>> annotatedEntities() {
4044

4145
@Test
4246
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 );
5155

5256
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+
} )
7272
)
7373
)
7474
);
7575
}
7676

77-
@Entity(name="Author")
78-
@Table(name="authors")
77+
@Entity(name = "Author")
78+
@Table(name = "authors")
7979
static class Author {
80-
@Id @GeneratedValue
81-
Integer id;
80+
@Id
81+
@GeneratedValue
82+
private Integer id;
8283

83-
String name;
84+
private String name;
8485

8586
@OneToMany(mappedBy = "author", cascade = PERSIST)
86-
List<Book> books = new ArrayList<>();
87+
private List<Book> books = new ArrayList<>();
8788

88-
Author(String name) {
89+
public Author(String name) {
8990
this.name = name;
9091
}
9192

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+
}
93119
}
94120

95-
@Entity(name="Book")
96-
@Table(name="books")
121+
@Entity(name = "Book")
122+
@Table(name = "books")
97123
static class Book extends LazyAttributeLoadingInterceptor
98124
implements PersistentAttributeInterceptable {
99125
@Id
100126
@GeneratedValue
101-
Integer id;
127+
private Integer id;
102128

103129
@Basic(fetch = LAZY)
104-
String isbn;
130+
private String isbn;
105131

106-
public String getIsbn() {
132+
private String getIsbn() {
107133
return isbn;
108134
}
109135

110-
String title;
136+
private String title;
111137

112138
@ManyToOne(fetch = LAZY)
113-
Author author;
139+
private Author author;
114140

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 );
117143
this.title = title;
118144
this.isbn = isbn;
119145
this.author = author;
120146
}
121147

122-
Book() {
123-
super("Book", 1, singleton("isbn"), null);
148+
public Book() {
149+
super( "Book", 1, singleton( "isbn" ), null );
124150
}
125151

126152
@Override
127153
public PersistentAttributeInterceptor $$_hibernate_getInterceptor() {
128154
return this;
129155
}
156+
130157
@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+
}
132188
}
133189
}

0 commit comments

Comments
 (0)