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

Open
wants to merge 2 commits into
base: 2.4
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ org.gradle.java.installations.auto-download=false
#db = MSSQL

# Enable the SonatypeOS maven repository (mainly for Vert.x snapshots) when present (value ignored)
#enableSonatypeOpenSourceSnapshotsRep = true
enableSonatypeOpenSourceSnapshotsRep = true

# Enable the maven local repository (for local development when needed) when present (value ignored)
#enableMavenLocalRepo = true

# The default Hibernate ORM version (override using `-PhibernateOrmVersion=the.version.you.want`)
hibernateOrmVersion = 6.6.13.Final
# I'm not setting a version window here, because the examples will end up using the latest stable version
# instead of the latest snapshot
hibernateOrmVersion = 6.6.14-SNAPSHOT

# 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.13.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
#skipOrmVersionParsing = true
skipOrmVersionParsing = true

# Override default Vert.x Sql client version
#vertxSqlClientVersion = 4.5.14-SNAPSHOT
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