Skip to content

Commit 6e111ca

Browse files
committed
Test compound natural id in NaturalIdTest
1 parent cd7851a commit 6e111ca

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

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

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,130 @@
77

88
import java.util.Collection;
99
import java.util.List;
10-
11-
import io.vertx.ext.unit.TestContext;
10+
import java.util.Objects;
1211

1312
import org.hibernate.annotations.NaturalId;
1413

1514
import org.junit.Test;
1615

16+
import io.vertx.ext.unit.TestContext;
1717
import jakarta.persistence.Entity;
1818
import jakarta.persistence.GeneratedValue;
1919
import jakarta.persistence.Id;
2020

21+
import static org.assertj.core.api.Assertions.assertThat;
2122
import static org.hibernate.reactive.common.Identifier.composite;
2223
import static org.hibernate.reactive.common.Identifier.id;
2324

2425
public class NaturalIdTest extends BaseReactiveTest {
2526

2627
@Override
2728
protected Collection<Class<?>> annotatedEntities() {
28-
return List.of( Thing.class );
29+
return List.of( SimpleThing.class, CompoundThing.class );
2930
}
3031

3132
@Test
32-
public void test(TestContext context) {
33-
Thing thing1 = new Thing();
33+
public void testSimpleNaturalIdSingleLoad(TestContext context) {
34+
SimpleThing thing1 = new SimpleThing();
3435
thing1.naturalKey = "abc123";
36+
SimpleThing thing2 = new SimpleThing();
37+
thing2.naturalKey = "def456";
38+
test(
39+
context,
40+
getSessionFactory()
41+
.withSession( session -> session.persist( thing1, thing2 ).thenCompose( v -> session.flush() ) )
42+
.thenCompose( v -> getSessionFactory().withSession(
43+
session -> session.find( SimpleThing.class, id( "naturalKey", "abc123" ) )
44+
) )
45+
.thenAccept( t -> assertThat( t ).isEqualTo( thing1 ) )
46+
.thenCompose( v -> getSessionFactory().withSession(
47+
session -> session.find( SimpleThing.class, id( SimpleThing.class, "naturalKey", "not an id" ) )
48+
) )
49+
.thenAccept( context::assertNull )
50+
);
51+
}
52+
53+
@Test
54+
public void testCompoundNaturalIdSingleLoad(TestContext context) {
55+
CompoundThing thing1 = new CompoundThing();
56+
thing1.naturalKey = "xyz666";
3557
thing1.version = 1;
36-
Thing thing2 = new Thing();
37-
thing2.naturalKey = "abc123";
58+
CompoundThing thing2 = new CompoundThing();
59+
thing2.naturalKey = "xyz666";
3860
thing2.version = 2;
3961
test(
4062
context,
4163
getSessionFactory()
4264
.withSession( session -> session.persist( thing1, thing2 ).thenCompose( v -> session.flush() ) )
4365
.thenCompose( v -> getSessionFactory().withSession(
44-
session -> session.find( Thing.class, composite(
45-
id( "naturalKey", "abc123" ),
66+
session -> session.find( CompoundThing.class, composite(
67+
id( "naturalKey", "xyz666" ),
4668
id( "version", 1 )
4769
) )
4870
) )
49-
.thenAccept( t -> {
50-
context.assertNotNull( t );
51-
context.assertEquals( thing1.id, t.id );
52-
} )
71+
72+
.thenAccept( t -> assertThat( t ).isEqualTo( thing1 ) )
5373
.thenCompose( v -> getSessionFactory().withSession(
54-
session -> session.find( Thing.class, composite(
55-
id( Thing.class, "naturalKey", "abc123" ),
56-
id( Thing.class, "version", 3 )
74+
session -> session.find( CompoundThing.class, composite(
75+
id( CompoundThing.class, "naturalKey", "xyz666" ),
76+
id( CompoundThing.class, "version", 3 )
5777
) )
5878
) )
5979
.thenAccept( context::assertNull )
6080
);
6181
}
6282

63-
@Entity(name = "Thing")
64-
static class Thing {
83+
@Entity(name = "SimpleThing")
84+
static class SimpleThing {
85+
@Id
86+
@GeneratedValue
87+
long id;
88+
@NaturalId
89+
String naturalKey;
90+
91+
@Override
92+
public boolean equals(Object o) {
93+
if ( this == o ) {
94+
return true;
95+
}
96+
if ( o == null || getClass() != o.getClass() ) {
97+
return false;
98+
}
99+
SimpleThing that = (SimpleThing) o;
100+
return Objects.equals( naturalKey, that.naturalKey );
101+
}
102+
103+
@Override
104+
public int hashCode() {
105+
return Objects.hash( naturalKey );
106+
}
107+
}
108+
109+
@Entity(name = "CompoundThing")
110+
static class CompoundThing {
65111
@Id
66112
@GeneratedValue
67113
long id;
68114
@NaturalId
69115
String naturalKey;
70116
@NaturalId
71117
int version;
118+
119+
@Override
120+
public boolean equals(Object o) {
121+
if ( this == o ) {
122+
return true;
123+
}
124+
if ( o == null || getClass() != o.getClass() ) {
125+
return false;
126+
}
127+
CompoundThing that = (CompoundThing) o;
128+
return version == that.version && Objects.equals( naturalKey, that.naturalKey );
129+
}
130+
131+
@Override
132+
public int hashCode() {
133+
return Objects.hash( naturalKey, version );
134+
}
72135
}
73136
}

0 commit comments

Comments
 (0)