-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19840 JDBC batching is not working with @CreationTimestamp and @UpdateTimestamp from Hibernate 7 #11087
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
HHH-19840 JDBC batching is not working with @CreationTimestamp and @UpdateTimestamp from Hibernate 7 #11087
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...rc/test/java/org/hibernate/orm/test/annotations/InMemoryTimestampGenerationBatchTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.annotations; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.SourceType; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.generator.internal.CurrentTimestampGeneration; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.Jira; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.hibernate.testing.orm.junit.SettingProvider; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static java.lang.Thread.sleep; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @DomainModel(annotatedClasses = InMemoryTimestampGenerationBatchTest.Person.class) | ||
| @SessionFactory(generateStatistics = true) | ||
| @ServiceRegistry(settings = @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "5"), | ||
| settingProviders = @SettingProvider(settingName = CurrentTimestampGeneration.CLOCK_SETTING_NAME, | ||
| provider = InMemoryTimestampGenerationBatchTest.MutableClockProvider.class)) | ||
| @Jira("https://hibernate.atlassian.net/browse/HHH-19840") | ||
| public class InMemoryTimestampGenerationBatchTest { | ||
| private static final MutableClock clock = new MutableClock(); | ||
|
|
||
| private static final int PERSON_COUNT = 8; | ||
|
|
||
| @Test | ||
| public void test(SessionFactoryScope scope) throws InterruptedException { | ||
| final var statistics = scope.getSessionFactory().getStatistics(); | ||
| scope.inTransaction( session -> { | ||
| Person person = null; | ||
| for ( int i = 1; i <= PERSON_COUNT; i++ ) { | ||
| person = new Person(); | ||
| person.setId( (long) i ); | ||
| person.setName( "person_" + i ); | ||
| session.persist( person ); | ||
| } | ||
|
|
||
| statistics.clear(); | ||
| session.flush(); | ||
|
|
||
| assertEquals( 1, statistics.getPrepareStatementCount(), "Expected updates to execute in batches" ); | ||
|
|
||
| assertNotNull( person.getCreatedOn() ); | ||
| assertNotNull( person.getUpdatedOn() ); | ||
| } ); | ||
|
|
||
|
|
||
| clock.tick(); | ||
| sleep( 1 ); | ||
|
|
||
| scope.inTransaction( session -> { | ||
| final var persons = session.findMultiple( Person.class, | ||
| IntStream.rangeClosed( 1, PERSON_COUNT ) | ||
| .mapToObj( i -> (long) i ) | ||
| .toList() ); | ||
|
|
||
| assertThat( persons ).hasSize( PERSON_COUNT ); | ||
| assertThat( persons ).doesNotContainNull(); | ||
|
|
||
| Person person = null; | ||
| for ( final Person p : persons ) { | ||
| p.setName( p.getName() + "_updated" ); | ||
| person = p; | ||
| } | ||
|
|
||
| final var createdOn = person.getCreatedOn(); | ||
| final var updatedOn = person.getUpdatedOn(); | ||
|
|
||
| statistics.clear(); | ||
| session.flush(); | ||
|
|
||
| assertEquals( 1, statistics.getPrepareStatementCount(), "Expected updates to execute in batches" ); | ||
|
|
||
| assertEquals( person.getCreatedOn(), createdOn ); | ||
| assertTrue( person.getUpdatedOn().isAfter( updatedOn ) ); | ||
| } ); | ||
| } | ||
|
|
||
| public static class MutableClockProvider implements SettingProvider.Provider<Object> { | ||
| @Override | ||
| public Object getSetting() { | ||
| return clock; | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "Person") | ||
| public static class Person { | ||
| @Id | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| @Column(nullable = false) | ||
| @CreationTimestamp(source = SourceType.VM) | ||
| private Instant createdOn; | ||
|
|
||
| @Column(nullable = false) | ||
| @UpdateTimestamp(source = SourceType.VM) | ||
| private Instant updatedOn; | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Instant getCreatedOn() { | ||
| return createdOn; | ||
| } | ||
|
|
||
| public Instant getUpdatedOn() { | ||
| return updatedOn; | ||
| } | ||
| } | ||
| } | ||
122 changes: 122 additions & 0 deletions
122
...ore/src/test/java/org/hibernate/orm/test/annotations/InMemoryTimestampGenerationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.annotations; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.SourceType; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
| import org.hibernate.generator.internal.CurrentTimestampGeneration; | ||
| import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
| import org.hibernate.testing.orm.junit.Jira; | ||
| import org.hibernate.testing.orm.junit.Jpa; | ||
| import org.hibernate.testing.orm.junit.SettingProvider; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import static java.lang.Thread.sleep; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @Jpa(annotatedClasses = InMemoryTimestampGenerationTest.Person.class, | ||
| settingProviders = @SettingProvider(settingName = CurrentTimestampGeneration.CLOCK_SETTING_NAME, | ||
| provider = InMemoryTimestampGenerationTest.MutableClockProvider.class)) | ||
| @Jira("https://hibernate.atlassian.net/browse/HHH-19840") | ||
| public class InMemoryTimestampGenerationTest { | ||
| private static final MutableClock clock = new MutableClock(); | ||
|
|
||
| @Test | ||
| public void test(EntityManagerFactoryScope scope) throws InterruptedException { | ||
| scope.inTransaction( entityManager -> { | ||
| Person person = new Person(); | ||
| person.setId( 1L ); | ||
| person.setFirstName( "Jon" ); | ||
| person.setLastName( "Doe" ); | ||
| entityManager.persist( person ); | ||
|
|
||
| entityManager.flush(); | ||
|
|
||
| assertNotNull( person.getCreatedOn() ); | ||
| assertNotNull( person.getUpdatedOn() ); | ||
| } ); | ||
|
|
||
| clock.tick(); | ||
| sleep( 1 ); | ||
|
|
||
| scope.inTransaction( entityManager -> { | ||
| final Person person = entityManager.find( Person.class, 1L ); | ||
| person.setLastName( "Doe Jr." ); | ||
|
|
||
| final var updatedOn = person.getUpdatedOn(); | ||
| final var createdOn = person.getCreatedOn(); | ||
|
|
||
| entityManager.flush(); | ||
|
|
||
| assertEquals( person.getCreatedOn(), createdOn ); | ||
| assertTrue( person.getUpdatedOn().isAfter( updatedOn ) ); | ||
| } ); | ||
| } | ||
|
|
||
| static class MutableClockProvider implements SettingProvider.Provider<Object> { | ||
|
||
| @Override | ||
| public Object getSetting() { | ||
| return clock; | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "Person") | ||
| static class Person { | ||
| @Id | ||
| private Long id; | ||
|
|
||
| private String firstName; | ||
|
|
||
| private String lastName; | ||
|
|
||
| @Column(nullable = false) | ||
| @CreationTimestamp(source= SourceType.VM) | ||
| private Instant createdOn; | ||
|
|
||
| @Column(nullable = false) | ||
| @UpdateTimestamp(source= SourceType.VM) | ||
| private Instant updatedOn; | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public void setFirstName(String firstName) { | ||
| this.firstName = firstName; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| public void setLastName(String lastName) { | ||
| this.lastName = lastName; | ||
| } | ||
|
|
||
| public Instant getCreatedOn() { | ||
| return createdOn; | ||
| } | ||
|
|
||
| public Instant getUpdatedOn() { | ||
| return updatedOn; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.