Skip to content

Commit e544f97

Browse files
committed
[hibernate#2138] Use UPDATE event type for generator where applicable
1 parent aea9239 commit e544f97

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-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
@@ -30,7 +30,7 @@
3030
import org.hibernate.tuple.entity.EntityMetamodel;
3131

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

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

+69-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
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;
1213

1314
import org.hibernate.annotations.CreationTimestamp;
1415
import org.hibernate.annotations.UpdateTimestamp;
1516

17+
import org.junit.jupiter.api.Disabled;
1618
import org.junit.jupiter.api.Test;
1719

1820
import io.vertx.junit5.Timeout;
1921
import io.vertx.junit5.VertxTestContext;
2022
import jakarta.persistence.Basic;
23+
import jakarta.persistence.Column;
24+
import jakarta.persistence.Embeddable;
25+
import jakarta.persistence.Embedded;
2126
import jakarta.persistence.Entity;
2227
import jakarta.persistence.GeneratedValue;
2328
import jakarta.persistence.Id;
@@ -28,12 +33,11 @@
2833
import static org.junit.jupiter.api.Assertions.assertTrue;
2934

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

Comments
 (0)