Skip to content

[2.4] Use UPDATE event type for generator where applicable #2199

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 2 commits into from
May 14, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Hibernate Reactive has been tested with:
- CockroachDB v24
- MS SQL Server 2022
- Oracle 23
- [Hibernate ORM][] 6.6.13.Final
- [Hibernate ORM][] 6.6.15.Final
- [Vert.x Reactive PostgreSQL Client](https://vertx.io/docs/vertx-pg-client/java/) 4.5.14
- [Vert.x Reactive MySQL Client](https://vertx.io/docs/vertx-mysql-client/java/) 4.5.14
- [Vert.x Reactive Db2 Client](https://vertx.io/docs/vertx-db2-client/java/) 4.5.14
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ org.gradle.java.installations.auto-download=false
#enableMavenLocalRepo = true

# The default Hibernate ORM version (override using `-PhibernateOrmVersion=the.version.you.want`)
hibernateOrmVersion = 6.6.13.Final
hibernateOrmVersion = 6.6.15.Final

# Override default Hibernate ORM Gradle plugin version
# Using the stable version because I don't know how to configure the build to download the snapshot version from
# a remote repository
#hibernateOrmGradlePluginVersion = 6.6.13.Final
#hibernateOrmGradlePluginVersion = 6.6.15.Final

# If set to true, skip Hibernate ORM version parsing (default is true, if set to null)
# this is required when using intervals or weird versions or the build will fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 @@ -194,7 +194,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