-
-
Notifications
You must be signed in to change notification settings - Fork 102
Support @IdGeneratorType #2407
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
Draft
DavideD
wants to merge
2
commits into
hibernate:main
Choose a base branch
from
DavideD:issue#1906
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−3
Draft
Support @IdGeneratorType #2407
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
24 changes: 24 additions & 0 deletions
24
...e-reactive-core/src/main/java/org/hibernate/reactive/id/ReactiveOnExecutionGenerator.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,24 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.generator.OnExecutionGenerator; | ||
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate; | ||
import org.hibernate.persister.entity.EntityPersister; | ||
import org.hibernate.reactive.id.insert.ReactiveInsertReturningDelegate; | ||
|
||
public interface ReactiveOnExecutionGenerator extends OnExecutionGenerator { | ||
|
||
@Override | ||
default InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(EntityPersister persister) { | ||
Dialect dialect = persister.getFactory().getJdbcServices().getDialect(); | ||
// Hibernate ORM allows the selection of different strategies based on the property `hibernate.jdbc.use_get_generated_keys`. | ||
// But that's a specific JDBC property and with Vert.x we only have one viable option for each supported database. | ||
return new ReactiveInsertReturningDelegate( persister, dialect ); | ||
} | ||
|
||
} |
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
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
156 changes: 156 additions & 0 deletions
156
...eactive-core/src/test/java/org/hibernate/reactive/BeforeExecutionIdGeneratorTypeTest.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,156 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import org.hibernate.annotations.IdGeneratorType; | ||
import org.hibernate.generator.EventType; | ||
import org.hibernate.reactive.id.ReactiveIdentifierGenerator; | ||
import org.hibernate.reactive.session.ReactiveConnectionSupplier; | ||
import org.hibernate.reactive.util.impl.CompletionStages; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.Tuple; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@Timeout(value = 10, timeUnit = MINUTES) | ||
public class BeforeExecutionIdGeneratorTypeTest extends BaseReactiveTest { | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Person.class ); | ||
} | ||
|
||
@Test | ||
public void testPersistWithoutTransaction(VertxTestContext context) { | ||
final Person person = new Person( "Janet" ); | ||
// The id should be set by the persist | ||
assertThat( person.getId() ).isNull(); | ||
test( context, getMutinySessionFactory() | ||
// The value won't be persisted on the database, but the id should have been assigned anyway | ||
.withSession( session -> session.persist( person ) ) | ||
.invoke( () -> assertThat( person.getId() ).isGreaterThan( 0 ) ) | ||
// Check that the value has not been saved | ||
.chain( () -> getMutinySessionFactory().withTransaction( s -> s | ||
.createNativeQuery( "select * from Person", Tuple.class ).getSingleResultOrNull() ) | ||
) | ||
.invoke( result -> assertThat( result ).isNull() ) | ||
); | ||
} | ||
|
||
@Test | ||
public void testPersistWithTransaction(VertxTestContext context) { | ||
final Person person = new Person( "Baldrick" ); | ||
// The id should be set by the persist | ||
assertThat( person.getId() ).isNull(); | ||
test( context, getMutinySessionFactory() | ||
.withTransaction( session -> session.persist( person ) ) | ||
.invoke( () -> assertThat( person.getId() ).isGreaterThan( 0 ) ) | ||
// Check that the value has been saved | ||
.chain( () -> getMutinySessionFactory().withTransaction( s -> s | ||
.createNativeQuery( "select id,name from Person", Object[].class ).getSingleResult() ) | ||
) | ||
.invoke( row -> { | ||
// The raw type might not be a Long, so we have to cast it | ||
assertThat( (Long) row[0] ).isEqualTo( person.id ); | ||
assertThat( row[1] ).isEqualTo( person.name ); | ||
} ) | ||
|
||
); | ||
} | ||
|
||
@Entity(name = "Person") | ||
@Table(name = "Person") | ||
public static class Person { | ||
@Id | ||
@SimpleId | ||
Long id; | ||
|
||
String name; | ||
|
||
public Person() { | ||
} | ||
|
||
public Person(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if ( o == null || getClass() != o.getClass() ) { | ||
return false; | ||
} | ||
Person person = (Person) o; | ||
return Objects.equals( name, person.name ); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode( name ); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return id + ":" + name; | ||
} | ||
} | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@IdGeneratorType(SimpleGenerator.class) | ||
public @interface SimpleId { | ||
} | ||
|
||
public static class SimpleGenerator implements ReactiveIdentifierGenerator<Long> { | ||
|
||
private AtomicLong sequence = new AtomicLong( 1 ); | ||
|
||
public SimpleGenerator() { | ||
} | ||
|
||
@Override | ||
public boolean generatedOnExecution() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public EnumSet<EventType> getEventTypes() { | ||
return EnumSet.of( EventType.INSERT ); | ||
} | ||
|
||
|
||
@Override | ||
public CompletionStage<Long> generate(ReactiveConnectionSupplier session, Object entity) { | ||
return CompletionStages.completedFuture( sequence.getAndIncrement() ); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...nate-reactive-core/src/test/java/org/hibernate/reactive/OnExecutionGeneratorTypeTest.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 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
|
||
import org.hibernate.annotations.IdGeneratorType; | ||
import org.hibernate.annotations.ValueGenerationType; | ||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.generator.EventType; | ||
import org.hibernate.generator.EventTypeSets; | ||
import org.hibernate.reactive.id.ReactiveOnExecutionGenerator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@Timeout(value = 10, timeUnit = MINUTES) | ||
public class OnExecutionGeneratorTypeTest extends BaseReactiveTest { | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Tournament.class ); | ||
} | ||
|
||
@Test | ||
public void testPersist(VertxTestContext context) { | ||
Tournament tournament = new Tournament( "Tekken World Tour" ); | ||
test( | ||
context, getSessionFactory() | ||
.withTransaction( session -> session.persist( tournament ) ) | ||
.thenAccept( v -> { | ||
assertThat( tournament.getId() ).isNotNull(); | ||
assertThat( tournament.getCreated() ).isNotNull(); | ||
} ) | ||
); | ||
} | ||
|
||
@Entity(name = "Tournament") | ||
public static class Tournament { | ||
@Id | ||
@FunctionCreatedValueId | ||
Date id; | ||
|
||
String name; | ||
|
||
@FunctionCreatedValue | ||
Date created; | ||
|
||
public Tournament() { | ||
} | ||
|
||
public Tournament(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Date getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Date getCreated() { | ||
return created; | ||
} | ||
} | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@IdGeneratorType(FunctionCreationValueGeneration.class) | ||
public @interface FunctionCreatedValueId { | ||
} | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class) | ||
public @interface FunctionCreatedValue { | ||
} | ||
|
||
public static class FunctionCreationValueGeneration | ||
implements ReactiveOnExecutionGenerator { | ||
|
||
@Override | ||
public boolean referenceColumnsInSql(Dialect dialect) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean writePropertyValue() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String[] getReferencedColumnValues(Dialect dialect) { | ||
return new String[] { dialect.currentTimestamp() }; | ||
} | ||
|
||
@Override | ||
public EnumSet<EventType> getEventTypes() { | ||
return EventTypeSets.INSERT_ONLY; | ||
} | ||
} | ||
|
||
} |
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.
Check notice
Code scanning / CodeQL
Confusing overloading of methods Note