6
6
package org .hibernate .reactive ;
7
7
8
8
import java .time .Instant ;
9
+ import java .time .LocalDateTime ;
9
10
import java .time .temporal .ChronoUnit ;
10
11
import java .util .Collection ;
11
12
import java .util .List ;
18
19
import io .vertx .junit5 .Timeout ;
19
20
import io .vertx .junit5 .VertxTestContext ;
20
21
import jakarta .persistence .Basic ;
22
+ import jakarta .persistence .Column ;
23
+ import jakarta .persistence .Embeddable ;
24
+ import jakarta .persistence .Embedded ;
21
25
import jakarta .persistence .Entity ;
22
26
import jakarta .persistence .GeneratedValue ;
23
27
import jakarta .persistence .Id ;
28
32
import static org .junit .jupiter .api .Assertions .assertTrue ;
29
33
30
34
@ Timeout (value = 10 , timeUnit = MINUTES )
31
-
32
35
public class TimestampTest extends BaseReactiveTest {
33
36
34
37
@ Override
35
38
protected Collection <Class <?>> annotatedEntities () {
36
- return List .of ( Record .class );
39
+ return List .of ( Record .class , Event . class );
37
40
}
38
41
39
42
@ Test
@@ -56,6 +59,30 @@ public void test(VertxTestContext context) {
56
59
);
57
60
}
58
61
62
+ @ Test
63
+ public void testEmbedded (VertxTestContext context ) {
64
+ Event event = new Event ();
65
+ History history = new History ();
66
+ event .name = "Concert" ;
67
+ test ( context , getMutinySessionFactory ()
68
+ .withSession ( session -> session .persist ( event )
69
+ .chain ( session ::flush )
70
+ .invoke ( () -> {
71
+ history .created = event .history .created ;
72
+ history .updated = event .history .updated ;
73
+ assertEquals (
74
+ event .history .created .truncatedTo ( ChronoUnit .HOURS ),
75
+ event .history .updated .truncatedTo ( ChronoUnit .HOURS )
76
+ ); })
77
+ .invoke ( () -> event .name = "Conference" )
78
+ .chain ( session ::flush )
79
+ .invoke ( () -> assertInstants ( event , history ) ) )
80
+ .chain ( () -> getMutinySessionFactory ().withSession ( session -> session
81
+ .find ( Record .class , event .id ) ) )
82
+ .invoke ( r -> assertInstants ( event , history ) )
83
+ );
84
+ }
85
+
59
86
private static void assertInstants (Record r ) {
60
87
assertNotNull ( r .created );
61
88
assertNotNull ( r .updated );
@@ -66,6 +93,18 @@ private static void assertInstants(Record r) {
66
93
);
67
94
}
68
95
96
+ private static void assertInstants (Event e , History h ) {
97
+ assertNotNull ( e .history .created );
98
+ assertNotNull ( e .history .updated );
99
+ // Sometimes, when the test suite is fast enough, they might be the same
100
+ assertTrue (
101
+ !e .history .updated .isBefore ( e .history .created ),
102
+ "Updated instant is before created. Updated[" + e .history .updated + "], Created[" + e .history .created + "]"
103
+ );
104
+ assertEquals ( h .created , e .history .created );
105
+
106
+ }
107
+
69
108
@ Entity (name = "Record" )
70
109
static class Record {
71
110
@ GeneratedValue
@@ -78,4 +117,30 @@ static class Record {
78
117
@ UpdateTimestamp
79
118
Instant updated ;
80
119
}
120
+
121
+ @ Entity (name = "Event" )
122
+ static class Event {
123
+
124
+ @ Id
125
+ @ GeneratedValue
126
+ public Long id ;
127
+
128
+ public String name ;
129
+
130
+ @ Embedded
131
+ public History history ;
132
+
133
+ }
134
+
135
+ @ Embeddable
136
+ static class History {
137
+ @ Column
138
+ @ CreationTimestamp
139
+ public LocalDateTime created ;
140
+
141
+ @ Column
142
+ @ UpdateTimestamp
143
+ public LocalDateTime updated ;
144
+
145
+ }
81
146
}
0 commit comments