|
7 | 7 |
|
8 | 8 | import java.util.Collection;
|
9 | 9 | import java.util.List;
|
10 |
| - |
11 |
| -import io.vertx.ext.unit.TestContext; |
| 10 | +import java.util.Objects; |
12 | 11 |
|
13 | 12 | import org.hibernate.annotations.NaturalId;
|
14 | 13 |
|
15 | 14 | import org.junit.Test;
|
16 | 15 |
|
| 16 | +import io.vertx.ext.unit.TestContext; |
17 | 17 | import jakarta.persistence.Entity;
|
18 | 18 | import jakarta.persistence.GeneratedValue;
|
19 | 19 | import jakarta.persistence.Id;
|
20 | 20 |
|
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
21 | 22 | import static org.hibernate.reactive.common.Identifier.composite;
|
22 | 23 | import static org.hibernate.reactive.common.Identifier.id;
|
23 | 24 |
|
24 | 25 | public class NaturalIdTest extends BaseReactiveTest {
|
25 | 26 |
|
26 | 27 | @Override
|
27 | 28 | protected Collection<Class<?>> annotatedEntities() {
|
28 |
| - return List.of( Thing.class ); |
| 29 | + return List.of( SimpleThing.class, CompoundThing.class ); |
29 | 30 | }
|
30 | 31 |
|
31 | 32 | @Test
|
32 |
| - public void test(TestContext context) { |
33 |
| - Thing thing1 = new Thing(); |
| 33 | + public void testSimpleNaturalIdSingleLoad(TestContext context) { |
| 34 | + SimpleThing thing1 = new SimpleThing(); |
34 | 35 | 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"; |
35 | 57 | thing1.version = 1;
|
36 |
| - Thing thing2 = new Thing(); |
37 |
| - thing2.naturalKey = "abc123"; |
| 58 | + CompoundThing thing2 = new CompoundThing(); |
| 59 | + thing2.naturalKey = "xyz666"; |
38 | 60 | thing2.version = 2;
|
39 | 61 | test(
|
40 | 62 | context,
|
41 | 63 | getSessionFactory()
|
42 | 64 | .withSession( session -> session.persist( thing1, thing2 ).thenCompose( v -> session.flush() ) )
|
43 | 65 | .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" ), |
46 | 68 | id( "version", 1 )
|
47 | 69 | ) )
|
48 | 70 | ) )
|
49 |
| - .thenAccept( t -> { |
50 |
| - context.assertNotNull( t ); |
51 |
| - context.assertEquals( thing1.id, t.id ); |
52 |
| - } ) |
| 71 | + |
| 72 | + .thenAccept( t -> assertThat( t ).isEqualTo( thing1 ) ) |
53 | 73 | .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 ) |
57 | 77 | ) )
|
58 | 78 | ) )
|
59 | 79 | .thenAccept( context::assertNull )
|
60 | 80 | );
|
61 | 81 | }
|
62 | 82 |
|
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 { |
65 | 111 | @Id
|
66 | 112 | @GeneratedValue
|
67 | 113 | long id;
|
68 | 114 | @NaturalId
|
69 | 115 | String naturalKey;
|
70 | 116 | @NaturalId
|
71 | 117 | 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 | + } |
72 | 135 | }
|
73 | 136 | }
|
0 commit comments