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 ;
1819import io .vertx .junit5 .Timeout ;
1920import io .vertx .junit5 .VertxTestContext ;
2021import jakarta .persistence .Basic ;
22+ import jakarta .persistence .Column ;
23+ import jakarta .persistence .Embeddable ;
24+ import jakarta .persistence .Embedded ;
2125import jakarta .persistence .Entity ;
2226import jakarta .persistence .GeneratedValue ;
2327import jakarta .persistence .Id ;
2832import static org .junit .jupiter .api .Assertions .assertTrue ;
2933
3034@ Timeout (value = 10 , timeUnit = MINUTES )
31-
3235public class TimestampTest extends BaseReactiveTest {
3336
3437 @ Override
3538 protected Collection <Class <?>> annotatedEntities () {
36- return List .of ( Record .class );
39+ return List .of ( Record .class , Event . class );
3740 }
3841
3942 @ Test
@@ -56,6 +59,30 @@ public void test(VertxTestContext context) {
5659 );
5760 }
5861
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+
5986 private static void assertInstants (Record r ) {
6087 assertNotNull ( r .created );
6188 assertNotNull ( r .updated );
@@ -66,6 +93,18 @@ private static void assertInstants(Record r) {
6693 );
6794 }
6895
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+
69108 @ Entity (name = "Record" )
70109 static class Record {
71110 @ GeneratedValue
@@ -78,4 +117,30 @@ static class Record {
78117 @ UpdateTimestamp
79118 Instant updated ;
80119 }
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+ }
81146}
0 commit comments