Skip to content

Commit a9ab99d

Browse files
geoandDavideD
authored andcommitted
Work around lazy eager loading issue in clean DB for EagerUniqueKeyTest
This is done because the actual test itself passes, what fails before this change is the loading of entities which occurs when the database is being cleared during test cleanup. Furthermore, fix erroneous assertion in EagerUniqueKeyTest
1 parent a311e38 commit a9ab99d

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.annotations.Fetch;
1212
import org.hibernate.annotations.FetchMode;
1313

14+
import org.junit.After;
1415
import org.junit.Test;
1516

1617
import jakarta.persistence.CascadeType;
@@ -32,6 +33,13 @@ protected Collection<Class<?>> annotatedEntities() {
3233
return List.of( Foo.class, Bar.class );
3334
}
3435

36+
@After
37+
public void cleanDb(TestContext context) {
38+
test( context, getSessionFactory()
39+
.withTransaction( s -> s.createQuery( "delete from Foo" ).executeUpdate()
40+
.thenCompose( v -> s.createQuery( "delete from Bar" ).executeUpdate() ) ) );
41+
}
42+
3543
@Test
3644
public void testFindJoin(TestContext context) {
3745
Foo foo = new Foo( new Bar( "unique" ) );
@@ -41,8 +49,8 @@ public void testFindJoin(TestContext context) {
4149
.thenAccept( v -> session.clear() )
4250
.thenCompose( v -> session.find( Foo.class, foo.id ) )
4351
.thenAccept( result -> {
44-
context.assertTrue( Hibernate.isInitialized( result.bar ) );
45-
context.assertEquals( "unique", result.bar.key );
52+
context.assertTrue( Hibernate.isInitialized( result.getBar() ) );
53+
context.assertEquals( "unique", result.getBar().getKey() );
4654
} )
4755
) );
4856
}
@@ -56,8 +64,8 @@ public void testMergeDetached(TestContext context) {
5664
.thenCompose( i -> getSessionFactory()
5765
.withTransaction( session -> session.merge( new Foo( bar ) ) ) )
5866
.thenCompose( result -> getSessionFactory()
59-
.withTransaction( session -> session.fetch( result.bar )
60-
.thenAccept( b -> context.assertEquals( "unique2", b.key ) )
67+
.withTransaction( session -> session.fetch( result.getBar() )
68+
.thenAccept( b -> context.assertEquals( "unique2", b.getKey() ) )
6169
) ) );
6270
}
6371

@@ -68,8 +76,8 @@ public void testMergeReference(TestContext context) {
6876
.withTransaction( session -> session.persist( bar ) )
6977
.thenCompose( i -> getSessionFactory()
7078
.withTransaction( session -> session.merge( new Foo( session.getReference( Bar.class, bar.id ) )) ) )
71-
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.bar )
72-
.thenAccept( b -> context.assertEquals( "unique3", b.key ) )
79+
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() )
80+
.thenAccept( b -> context.assertEquals( "unique3", b.getKey() ) )
7381
) ) );
7482
}
7583

@@ -89,6 +97,22 @@ static class Foo {
8997
@Fetch(FetchMode.JOIN)
9098
@JoinColumn(name = "bar_key", referencedColumnName = "nat_key")
9199
Bar bar;
100+
101+
public long getId() {
102+
return id;
103+
}
104+
105+
public void setId(long id) {
106+
this.id = id;
107+
}
108+
109+
public Bar getBar() {
110+
return bar;
111+
}
112+
113+
public void setBar(Bar bar) {
114+
this.bar = bar;
115+
}
92116
}
93117

94118
@Entity(name = "Bar")
@@ -105,5 +129,21 @@ static class Bar implements Serializable {
105129
long id;
106130
@Column(name = "nat_key", unique = true)
107131
String key;
132+
133+
public long getId() {
134+
return id;
135+
}
136+
137+
public void setId(long id) {
138+
this.id = id;
139+
}
140+
141+
public String getKey() {
142+
return key;
143+
}
144+
145+
public void setKey(String key) {
146+
this.key = key;
147+
}
108148
}
109149
}

0 commit comments

Comments
 (0)