-
-
Notifications
You must be signed in to change notification settings - Fork 102
Support @IdGeneratorType #2380
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
base: main
Are you sure you want to change the base?
Support @IdGeneratorType #2380
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
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 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.concurrent.CompletionStage; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
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 testPersist(VertxTestContext context) { | ||
Person person = new Person(); | ||
test( | ||
context, openSession() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use Maybe we could also test without transactions, but we should at least add a comment to explain what's going on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to also read the value from the database to make sure that they match? Maybe it's overkill |
||
.thenCompose( session -> session.persist( person ) ) | ||
.thenAccept( v -> { | ||
assertThat( person.getId() ).isNotNull(); | ||
} ) | ||
); | ||
} | ||
|
||
@Entity(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; | ||
} | ||
} | ||
|
||
@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() ); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
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 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 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( Person.class ); | ||
} | ||
|
||
@Test | ||
public void testPersist(VertxTestContext context) { | ||
Person person = new Person( "Davide" ); | ||
test( context, getSessionFactory() | ||
.withTransaction( session -> session.persist( person ) ) | ||
.thenAccept( v -> { | ||
assertThat( person.getId() ).isNotNull(); | ||
assertThat( person.getCreated() ).isNotNull(); | ||
} ) | ||
); | ||
} | ||
|
||
@Entity(name = "Person") | ||
public static class Person { | ||
@Id | ||
@FunctionCreatedValueId | ||
Date id; | ||
|
||
String name; | ||
|
||
@FunctionCreatedValue | ||
Date created; | ||
|
||
public Person() { | ||
} | ||
|
||
public Person(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; | ||
} | ||
} | ||
|
||
} |
Check notice
Code scanning / CodeQL
Confusing overloading of methods Note