Skip to content

Use UPDATE event type for generator where applicable #2172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.hibernate.tuple.entity.EntityMetamodel;

import static org.hibernate.engine.jdbc.mutation.internal.ModelMutationHelper.identifiedResultsCheck;
import static org.hibernate.generator.EventType.INSERT;
import static org.hibernate.generator.EventType.UPDATE;
import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_INT_ARRAY;
import static org.hibernate.internal.util.collections.ArrayHelper.trim;
import static org.hibernate.reactive.persister.entity.mutation.GeneratorValueUtil.generateValue;
Expand Down Expand Up @@ -193,7 +193,7 @@ private CompletionStage<int[]> reactivePreUpdateInMemoryValueGeneration(
&& generator.generatesOnUpdate() ) {
final Object currentValue = currentValues[i];
final BeforeExecutionGenerator beforeGenerator = (BeforeExecutionGenerator) generator;
result = result.thenCompose( v -> generateValue( session, entity, currentValue, beforeGenerator, INSERT )
result = result.thenCompose( v -> generateValue( session, entity, currentValue, beforeGenerator, UPDATE )
.thenAccept( generatedValue -> {
currentValues[index] = generatedValue;
entityPersister().setValue( entity, index, generatedValue );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.hibernate.reactive;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;
Expand All @@ -18,6 +19,9 @@
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Expand All @@ -28,12 +32,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

@Timeout(value = 10, timeUnit = MINUTES)

public class TimestampTest extends BaseReactiveTest {

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Record.class );
return List.of( Record.class, Event.class );
}

@Test
Expand All @@ -56,6 +59,30 @@ public void test(VertxTestContext context) {
);
}

@Test
public void testEmbedded(VertxTestContext context) {
Event event = new Event();
History history = new History();
event.name = "Concert";
test( context, getMutinySessionFactory()
.withSession( session -> session.persist( event )
.chain( session::flush )
.invoke( () -> {
history.created = event.history.created;
history.updated = event.history.updated;
assertEquals(
event.history.created.truncatedTo( ChronoUnit.HOURS ),
event.history.updated.truncatedTo( ChronoUnit.HOURS )
); })
.invoke( () -> event.name = "Conference" )
.chain( session::flush )
.invoke( () -> assertInstants( event, history ) ) )
.chain( () -> getMutinySessionFactory().withSession( session -> session
.find( Record.class, event.id ) ) )
.invoke( r -> assertInstants( event, history ) )
);
}

private static void assertInstants(Record r) {
assertNotNull( r.created );
assertNotNull( r.updated );
Expand All @@ -66,6 +93,18 @@ private static void assertInstants(Record r) {
);
}

private static void assertInstants(Event e, History h) {
assertNotNull( e.history.created );
assertNotNull( e.history.updated );
// Sometimes, when the test suite is fast enough, they might be the same:
assertTrue(
!e.history.updated.isBefore( e.history.created ),
"Updated instant is before created. Updated[" + e.history.updated + "], Created[" + e.history.created + "]"
);
assertEquals( h.created, e.history.created );

}

@Entity(name = "Record")
static class Record {
@GeneratedValue
Expand All @@ -78,4 +117,30 @@ static class Record {
@UpdateTimestamp
Instant updated;
}

@Entity(name = "Event")
static class Event {

@Id
@GeneratedValue
public Long id;

public String name;

@Embedded
public History history;

}

@Embeddable
static class History {
@Column
@CreationTimestamp
public LocalDateTime created;

@Column
@UpdateTimestamp
public LocalDateTime updated;

}
}
Loading