Skip to content

Commit 5cb015d

Browse files
committed
[#1891] Add test case for getReference with BE
Test `session.getReference` with Bytecode Enhancements enabled
1 parent 0cba44d commit 5cb015d

File tree

3 files changed

+381
-0
lines changed

3 files changed

+381
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.it.reference;
7+
8+
import java.util.Objects;
9+
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.ManyToOne;
15+
import jakarta.persistence.Table;
16+
17+
@Entity(name = "Writer")
18+
@Table(name = "TAuthor")
19+
public class Author {
20+
21+
@Id
22+
@GeneratedValue
23+
private Integer id;
24+
private String name;
25+
26+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
27+
private Book book;
28+
29+
public Author() {
30+
}
31+
32+
public Author(String name, Book book) {
33+
this.name = name;
34+
this.book = book;
35+
}
36+
37+
public Integer getId() {
38+
return id;
39+
}
40+
41+
public void setId(Integer id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public Book getBook() {
54+
return book;
55+
}
56+
57+
public void setBook(Book book) {
58+
this.book = book;
59+
}
60+
61+
@Override
62+
public boolean equals(Object o) {
63+
if ( this == o ) {
64+
return true;
65+
}
66+
if ( o == null || getClass() != o.getClass() ) {
67+
return false;
68+
}
69+
Author author = (Author) o;
70+
return Objects.equals( name, author.name );
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash( name );
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.it.reference;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
import jakarta.persistence.Entity;
13+
import jakarta.persistence.FetchType;
14+
import jakarta.persistence.GeneratedValue;
15+
import jakarta.persistence.Id;
16+
import jakarta.persistence.OneToMany;
17+
import jakarta.persistence.Table;
18+
import jakarta.persistence.Version;
19+
20+
@Entity(name = "Tome")
21+
@Table(name = "TBook")
22+
public class Book {
23+
24+
@Id
25+
@GeneratedValue
26+
private Integer id;
27+
@Version
28+
private Integer version = 1;
29+
private String title;
30+
31+
@OneToMany(fetch = FetchType.LAZY, mappedBy = "book")
32+
private List<Author> authors = new ArrayList<>();
33+
34+
public Book() {
35+
}
36+
37+
public Book(String title) {
38+
this.title = title;
39+
}
40+
41+
public Integer getId() {
42+
return id;
43+
}
44+
45+
public void setId(Integer id) {
46+
this.id = id;
47+
}
48+
49+
public String getTitle() {
50+
return title;
51+
}
52+
53+
public void setTitle(String title) {
54+
this.title = title;
55+
}
56+
57+
public List<Author> getAuthors() {
58+
return authors;
59+
}
60+
61+
public void setAuthors(List<Author> authors) {
62+
this.authors = authors;
63+
}
64+
65+
public Integer getVersion() {
66+
return version;
67+
}
68+
69+
public void setVersion(Integer version) {
70+
this.version = version;
71+
}
72+
73+
@Override
74+
public boolean equals(Object o) {
75+
if ( this == o ) {
76+
return true;
77+
}
78+
if ( o == null || getClass() != o.getClass() ) {
79+
return false;
80+
}
81+
Book book = (Book) o;
82+
return Objects.equals( title, book.title );
83+
}
84+
85+
@Override
86+
public int hashCode() {
87+
return Objects.hash( title );
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.it;
7+
8+
import java.util.Collection;
9+
import java.util.List;
10+
import java.util.concurrent.CompletionStage;
11+
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.LockMode;
14+
import org.hibernate.reactive.it.reference.Author;
15+
import org.hibernate.reactive.it.reference.Book;
16+
import org.hibernate.reactive.stage.Stage;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
import io.vertx.junit5.Timeout;
22+
import io.vertx.junit5.VertxTestContext;
23+
24+
import static java.util.concurrent.TimeUnit.MINUTES;
25+
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
26+
import static org.hibernate.reactive.util.impl.CompletionStages.loop;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
@Timeout(value = 10, timeUnit = MINUTES)
33+
34+
public class ReferenceBETest extends BaseReactiveIT {
35+
36+
@Override
37+
protected Collection<Class<?>> annotatedEntities() {
38+
return List.of( Author.class, Book.class );
39+
}
40+
41+
@Override
42+
public CompletionStage<Void> deleteEntities(Class<?>... entities) {
43+
return getSessionFactory()
44+
.withTransaction( s -> loop( entities, entityClass -> s
45+
.createQuery( "from " + entityName( entityClass ), entityClass )
46+
.getResultList()
47+
.thenCompose( list -> loop( list, s::remove ) ) ) );
48+
}
49+
50+
private String entityName(Class<?> entityClass) {
51+
if ( Author.class.equals( entityClass ) ) {
52+
return "Writer";
53+
}
54+
return "Tome";
55+
}
56+
57+
@Test
58+
public void testDetachedEntityReference(VertxTestContext context) {
59+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
60+
61+
test( context, getSessionFactory()
62+
.withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) )
63+
.thenCompose( v -> getSessionFactory().withSession( s -> {
64+
Book book = s.getReference( Book.class, goodOmens.getId() );
65+
assertFalse( Hibernate.isInitialized( book ) );
66+
return s.persist( new Author( "Neil Gaiman", book ) )
67+
.thenCompose( vv -> s.flush() );
68+
} ) )
69+
.thenCompose( v -> getSessionFactory().withSession( s -> {
70+
Book book = s.getReference( goodOmens );
71+
assertFalse( Hibernate.isInitialized( book ) );
72+
return s.persist( new Author( "Terry Pratchett", book ) )
73+
.thenCompose( vv -> s.flush() );
74+
} ) )
75+
.thenCompose( v -> getSessionFactory().withSession( s -> {
76+
Book book = s.getReference( goodOmens );
77+
assertFalse( Hibernate.isInitialized( book ) );
78+
return Stage.fetch( book ).thenCompose( vv -> Stage.fetch( book.getAuthors() ) );
79+
} ) )
80+
.thenAccept( optionalAssociation -> {
81+
assertTrue( Hibernate.isInitialized( optionalAssociation ) );
82+
assertNotNull( optionalAssociation );
83+
assertEquals( 2, optionalAssociation.size() );
84+
} )
85+
);
86+
}
87+
88+
@Test
89+
public void testDetachedProxyReference(VertxTestContext context) {
90+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
91+
92+
test( context, getSessionFactory()
93+
.withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) )
94+
.thenCompose( v -> getSessionFactory().withSession( s -> {
95+
Book reference = s.getReference( goodOmens );
96+
assertFalse( Hibernate.isInitialized( reference ) );
97+
return completedFuture( reference );
98+
} ) )
99+
.thenCompose( reference -> getSessionFactory().withSession( s -> {
100+
Book book = s.getReference( Book.class, reference.getId() );
101+
assertFalse( Hibernate.isInitialized( book ) );
102+
assertFalse( Hibernate.isInitialized( reference ) );
103+
return s.persist( new Author( "Neil Gaiman", book ) )
104+
.thenCompose( v -> s.flush() )
105+
.thenApply( v -> reference );
106+
} ) )
107+
.thenCompose( reference -> getSessionFactory().withSession( s -> {
108+
Book book = s.getReference( reference );
109+
assertFalse( Hibernate.isInitialized( book ) );
110+
assertFalse( Hibernate.isInitialized( reference ) );
111+
return s.persist( new Author( "Terry Pratchett", book ) )
112+
.thenCompose( v -> s.flush() )
113+
.thenApply( v -> reference );
114+
} ) )
115+
.thenCompose( reference -> getSessionFactory().withSession( s -> {
116+
Book book = s.getReference( reference );
117+
assertFalse( Hibernate.isInitialized( book ) );
118+
assertFalse( Hibernate.isInitialized( reference ) );
119+
return Stage.fetch( book ).thenCompose( v -> Stage.fetch( book.getAuthors() ) );
120+
} ) )
121+
.thenAccept( optionalAssociation -> {
122+
assertTrue( Hibernate.isInitialized( optionalAssociation ) );
123+
assertNotNull( optionalAssociation );
124+
assertEquals( 2, optionalAssociation.size() );
125+
} )
126+
);
127+
}
128+
129+
@Test
130+
public void testRemoveDetachedProxy(VertxTestContext context) {
131+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
132+
test( context, getSessionFactory()
133+
.withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) )
134+
.thenCompose( v -> getSessionFactory().withSession( sess -> {
135+
Book reference = sess.getReference( goodOmens );
136+
assertFalse( Hibernate.isInitialized( reference ) );
137+
return completedFuture( reference );
138+
} ) )
139+
.thenCompose( reference -> getSessionFactory().withSession( s -> s
140+
.remove( s.getReference( reference ) ).thenCompose( v -> s.flush() ) )
141+
)
142+
.thenCompose( reference -> getSessionFactory().withSession( s -> s
143+
.find( Book.class, goodOmens.getId() )
144+
.thenAccept( Assertions::assertNull )
145+
) )
146+
);
147+
}
148+
149+
@Test
150+
public void testRemoveWithTransaction(VertxTestContext context) {
151+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
152+
test( context, getMutinySessionFactory()
153+
.withTransaction( s -> s.persist( goodOmens ) )
154+
.call( () -> getMutinySessionFactory()
155+
.withSession( s -> s.find( Book.class, goodOmens.getId() ) )
156+
.invoke( book -> assertEquals( goodOmens, book ) ) )
157+
.call( () -> getMutinySessionFactory().withTransaction( s -> s
158+
.remove( s.getReference( Book.class, goodOmens.getId() ) ) ) )
159+
.call( () -> getMutinySessionFactory().withSession( s -> s
160+
.find( Book.class, goodOmens.getId() ) ) )
161+
.invoke( Assertions::assertNull )
162+
);
163+
}
164+
165+
@Test
166+
public void testLockDetachedProxy(VertxTestContext context) {
167+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
168+
test( context, getSessionFactory().withSession( s -> s
169+
.persist( goodOmens ).thenCompose( v -> s.flush() )
170+
)
171+
.thenCompose( v -> getSessionFactory().withSession( s -> {
172+
Book reference = s.getReference( goodOmens );
173+
assertFalse( Hibernate.isInitialized( reference ) );
174+
return completedFuture( reference );
175+
} ) )
176+
.thenCompose( reference -> getSessionFactory().withSession( s -> s
177+
.lock( s.getReference( reference ), LockMode.PESSIMISTIC_FORCE_INCREMENT )
178+
.thenCompose( v -> s.flush() ) )
179+
)
180+
.thenCompose( v -> getSessionFactory().withSession( s -> s
181+
.find( Book.class, goodOmens.getId() ) )
182+
.thenAccept( book -> {
183+
assertNotNull( book );
184+
assertEquals( 2, book.getVersion() );
185+
} )
186+
)
187+
);
188+
}
189+
190+
@Test
191+
public void testRefreshDetachedProxy(VertxTestContext context) {
192+
final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" );
193+
test( context, getSessionFactory().withSession( s -> s
194+
.persist( goodOmens ).thenCompose( v -> s.flush() )
195+
)
196+
.thenCompose( v -> getSessionFactory().withSession( s -> {
197+
Book reference = s.getReference( goodOmens );
198+
assertFalse( Hibernate.isInitialized( reference ) );
199+
return completedFuture( reference );
200+
} ) )
201+
.thenCompose( reference -> getSessionFactory().withSession( s -> s
202+
.refresh( s.getReference( reference ) )
203+
.thenAccept( v -> assertTrue( Hibernate.isInitialized( s.getReference( reference ) ) ) )
204+
.thenCompose( v -> s.flush() ) )
205+
)
206+
.thenCompose( v -> getSessionFactory().withSession( s -> s
207+
.find( Book.class, goodOmens.getId() )
208+
.thenAccept( book -> {
209+
assertNotNull( book );
210+
assertEquals( 1, book.getVersion() );
211+
} )
212+
) )
213+
);
214+
}
215+
}

0 commit comments

Comments
 (0)