Skip to content

Commit a8c5994

Browse files
committed
[hibernate#1826] ReactiveIdentifierGenerator
1 parent dbfa29b commit a8c5994

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/ReactiveIdentifierGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.hibernate.reactive.id;
77

88
import org.hibernate.Incubating;
9+
import org.hibernate.generator.Generator;
910
import org.hibernate.id.IdentifierGenerator;
1011
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
1112

@@ -24,7 +25,7 @@
2425
* @see IdentifierGenerator
2526
*/
2627
@Incubating
27-
public interface ReactiveIdentifierGenerator<Id> {
28+
public interface ReactiveIdentifierGenerator<Id> extends Generator {
2829

2930
/**
3031
* Returns a generated identifier, via a {@link CompletionStage}.

hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomGeneratorTest.java

+39-30
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
package org.hibernate.reactive;
77

88
import java.util.Collection;
9+
import java.util.EnumSet;
910
import java.util.List;
1011
import java.util.Objects;
1112
import java.util.Properties;
1213
import java.util.concurrent.CompletionStage;
1314

15+
import org.hibernate.MappingException;
1416
import org.hibernate.annotations.GenericGenerator;
1517
import org.hibernate.annotations.Parameter;
18+
import org.hibernate.generator.EventType;
19+
import org.hibernate.generator.GeneratorCreationContext;
1620
import org.hibernate.id.Configurable;
1721
import org.hibernate.reactive.id.ReactiveIdentifierGenerator;
1822
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
19-
import org.hibernate.service.ServiceRegistry;
20-
import org.hibernate.type.Type;
2123

2224
import org.junit.jupiter.api.Test;
2325

@@ -44,36 +46,33 @@ protected Collection<Class<?>> annotatedEntities() {
4446

4547
@Test
4648
public void testSequenceGenerator(VertxTestContext context) {
47-
4849
CustomId b = new CustomId();
4950
b.string = "Hello World";
5051

51-
test(
52-
context,
53-
openSession()
54-
.thenCompose( s -> s.persist( b ).thenCompose( v -> s.flush() ) )
55-
.thenCompose( v -> openSession() )
56-
.thenCompose( s2 -> s2
57-
.find( CustomId.class, b.getId() )
58-
.thenAccept( bb -> {
59-
assertNotNull( bb );
60-
assertEquals( bb.id, 1100 );
61-
assertEquals( bb.string, b.string );
62-
assertEquals( bb.version, 0 );
63-
64-
bb.string = "Goodbye";
65-
} )
66-
.thenCompose( vv -> s2.flush() )
67-
.thenCompose( vv -> s2.find( CustomId.class, b.getId() ) )
68-
.thenAccept( bt -> {
69-
assertEquals( bt.version, 1 );
70-
} ) )
71-
.thenCompose( v -> openSession() )
72-
.thenCompose( s3 -> s3.find( CustomId.class, b.getId() ) )
52+
test( context, openSession()
53+
.thenCompose( s -> s.persist( b ).thenCompose( v -> s.flush() ) )
54+
.thenCompose( v -> openSession() )
55+
.thenCompose( s2 -> s2
56+
.find( CustomId.class, b.getId() )
7357
.thenAccept( bb -> {
74-
assertEquals( bb.version, 1 );
75-
assertEquals( bb.string, "Goodbye" );
58+
assertNotNull( bb );
59+
assertEquals( bb.id, 1100 );
60+
assertEquals( bb.string, b.string );
61+
assertEquals( bb.version, 0 );
62+
63+
bb.string = "Goodbye";
7664
} )
65+
.thenCompose( vv -> s2.flush() )
66+
.thenCompose( vv -> s2.find( CustomId.class, b.getId() ) )
67+
.thenAccept( bt -> {
68+
assertEquals( bt.version, 1 );
69+
} ) )
70+
.thenCompose( v -> openSession() )
71+
.thenCompose( s3 -> s3.find( CustomId.class, b.getId() ) )
72+
.thenAccept( bb -> {
73+
assertEquals( bb.version, 1 );
74+
assertEquals( bb.string, "Goodbye" );
75+
} )
7776
);
7877
}
7978

@@ -87,15 +86,25 @@ public CompletionStage<Integer> generate(ReactiveConnectionSupplier session, Obj
8786
}
8887

8988
@Override
90-
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) {
91-
current = Integer.parseInt( params.getProperty( "offset", "0" ) );
89+
public void configure(GeneratorCreationContext creationContext, Properties parameters) throws MappingException {
90+
current = Integer.parseInt( parameters.getProperty( "offset", "0" ) );
91+
}
92+
93+
@Override
94+
public boolean generatedOnExecution() {
95+
return false;
96+
}
97+
98+
@Override
99+
public EnumSet<EventType> getEventTypes() {
100+
return EnumSet.of( EventType.INSERT );
92101
}
93102
}
94103

95104
@Entity
96105
@GenericGenerator(
97106
name = "thousands",
98-
strategy = "org.hibernate.reactive.CustomGeneratorTest$Thousands",
107+
type = Thousands.class,
99108
parameters = @Parameter(name = "offset", value = "100")
100109
)
101110
public static class CustomId {

0 commit comments

Comments
 (0)