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