Skip to content

Commit 9dc3d15

Browse files
marko-bekhtaDavideD
authored andcommitted
[#2138] Use UPDATE event type for generator where applicable
1 parent 83276a5 commit 9dc3d15

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/persister/entity/mutation/ReactiveUpdateCoordinatorStandard.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.hibernate.tuple.entity.EntityMetamodel;
3232

3333
import static org.hibernate.engine.jdbc.mutation.internal.ModelMutationHelper.identifiedResultsCheck;
34-
import static org.hibernate.generator.EventType.INSERT;
34+
import static org.hibernate.generator.EventType.UPDATE;
3535
import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_INT_ARRAY;
3636
import static org.hibernate.internal.util.collections.ArrayHelper.trim;
3737
import static org.hibernate.reactive.persister.entity.mutation.GeneratorValueUtil.generateValue;
@@ -194,7 +194,7 @@ private CompletionStage<int[]> reactivePreUpdateInMemoryValueGeneration(
194194
&& generator.generatesOnUpdate() ) {
195195
final Object currentValue = currentValues[i];
196196
final BeforeExecutionGenerator beforeGenerator = (BeforeExecutionGenerator) generator;
197-
result = result.thenCompose( v -> generateValue( session, entity, currentValue, beforeGenerator, INSERT )
197+
result = result.thenCompose( v -> generateValue( session, entity, currentValue, beforeGenerator, UPDATE )
198198
.thenAccept( generatedValue -> {
199199
currentValues[index] = generatedValue;
200200
entityPersister().setValue( entity, index, generatedValue );

hibernate-reactive-core/src/test/java/org/hibernate/reactive/TimestampTest.java

+67-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.hibernate.reactive;
77

88
import java.time.Instant;
9+
import java.time.LocalDateTime;
910
import java.time.temporal.ChronoUnit;
1011
import java.util.Collection;
1112
import java.util.List;
@@ -18,6 +19,9 @@
1819
import io.vertx.junit5.Timeout;
1920
import io.vertx.junit5.VertxTestContext;
2021
import jakarta.persistence.Basic;
22+
import jakarta.persistence.Column;
23+
import jakarta.persistence.Embeddable;
24+
import jakarta.persistence.Embedded;
2125
import jakarta.persistence.Entity;
2226
import jakarta.persistence.GeneratedValue;
2327
import jakarta.persistence.Id;
@@ -28,12 +32,11 @@
2832
import static org.junit.jupiter.api.Assertions.assertTrue;
2933

3034
@Timeout(value = 10, timeUnit = MINUTES)
31-
3235
public 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

Comments
 (0)