66package org .hibernate .reactive ;
77
88import java .time .Instant ;
9+ import java .time .LocalDateTime ;
910import java .time .temporal .ChronoUnit ;
1011import java .util .Collection ;
1112import java .util .List ;
1213
1314import org .hibernate .annotations .CreationTimestamp ;
1415import org .hibernate .annotations .UpdateTimestamp ;
1516
17+ import org .junit .jupiter .api .Disabled ;
1618import org .junit .jupiter .api .Test ;
1719
1820import io .vertx .junit5 .Timeout ;
1921import io .vertx .junit5 .VertxTestContext ;
2022import jakarta .persistence .Basic ;
23+ import jakarta .persistence .Column ;
24+ import jakarta .persistence .Embeddable ;
25+ import jakarta .persistence .Embedded ;
2126import jakarta .persistence .Entity ;
2227import jakarta .persistence .GeneratedValue ;
2328import jakarta .persistence .Id ;
2833import static org .junit .jupiter .api .Assertions .assertTrue ;
2934
3035@ Timeout (value = 10 , timeUnit = MINUTES )
31-
3236public class TimestampTest extends BaseReactiveTest {
3337
3438 @ Override
3539 protected Collection <Class <?>> annotatedEntities () {
36- return List .of ( Record .class );
40+ return List .of ( Record .class , Event . class );
3741 }
3842
3943 @ Test
@@ -56,6 +60,31 @@ public void test(VertxTestContext context) {
5660 );
5761 }
5862
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+
5988 private static void assertInstants (Record r ) {
6089 assertNotNull ( r .created );
6190 assertNotNull ( r .updated );
@@ -66,6 +95,18 @@ private static void assertInstants(Record r) {
6695 );
6796 }
6897
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+
69110 @ Entity (name = "Record" )
70111 static class Record {
71112 @ GeneratedValue
@@ -78,4 +119,30 @@ static class Record {
78119 @ UpdateTimestamp
79120 Instant updated ;
80121 }
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+ }
81148}
0 commit comments