Skip to content

Commit ba56ff4

Browse files
dreab8DavideD
authored andcommitted
[#2154] ReactiveEntityMetamodel#buildIdGenerator() refactoring
1 parent 4c59eb2 commit ba56ff4

File tree

1 file changed

+71
-31
lines changed

1 file changed

+71
-31
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/tuple/entity/ReactiveEntityMetamodel.java

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.function.Function;
99

1010

11+
import org.hibernate.boot.model.relational.Database;
12+
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1113
import org.hibernate.generator.Generator;
1214
import org.hibernate.generator.GeneratorCreationContext;
1315
import org.hibernate.id.CompositeNestedGeneratedValueGenerator;
@@ -19,9 +21,10 @@
1921
import org.hibernate.id.enhanced.SequenceStyleGenerator;
2022
import org.hibernate.id.enhanced.TableGenerator;
2123
import org.hibernate.id.enhanced.TableStructure;
22-
import org.hibernate.mapping.Component;
23-
import org.hibernate.mapping.GeneratorCreator;
24+
import org.hibernate.mapping.GeneratorSettings;
2425
import org.hibernate.mapping.PersistentClass;
26+
import org.hibernate.mapping.Property;
27+
import org.hibernate.mapping.RootClass;
2528
import org.hibernate.mapping.SimpleValue;
2629
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
2730
import org.hibernate.persister.entity.EntityPersister;
@@ -32,7 +35,9 @@
3235
import org.hibernate.reactive.id.impl.ReactiveSequenceIdentifierGenerator;
3336
import org.hibernate.reactive.id.impl.TableReactiveIdentifierGenerator;
3437
import org.hibernate.reactive.logging.impl.Log;
38+
import org.hibernate.service.ServiceRegistry;
3539
import org.hibernate.tuple.entity.EntityMetamodel;
40+
import org.hibernate.type.Type;
3641

3742
import static java.lang.invoke.MethodHandles.lookup;
3843
import static org.hibernate.reactive.logging.impl.LoggerFactory.make;
@@ -71,44 +76,26 @@ private static Generator buildIdGenerator(
7176
}
7277
else {
7378
final SimpleValue identifier = (SimpleValue) persistentClass.getIdentifier();
74-
setCustomIdGenerator( persistentClass, creationContext, identifier );
75-
76-
final Generator idgenerator = identifier
77-
// returns the cached Generator if it was already created
78-
.createGenerator(
79+
final Generator idgenerator = augmentWithReactiveGenerator(
80+
identifier.createGenerator(
7981
creationContext.getDialect(),
8082
persistentClass.getRootClass(),
8183
persistentClass.getIdentifierProperty(),
8284
creationContext.getGeneratorSettings()
83-
);
85+
),
86+
new IdGeneratorCreationContext(
87+
persistentClass.getRootClass(),
88+
persistentClass.getIdentifierProperty(),
89+
creationContext.getGeneratorSettings(),
90+
identifier,
91+
creationContext
92+
),
93+
creationContext );
8494
creationContext.getGenerators().put( rootName, idgenerator );
8595
return idgenerator;
8696
}
8797
}
8898

89-
private static void setCustomIdGenerator(
90-
PersistentClass persistentClass,
91-
RuntimeModelCreationContext creationContext,
92-
SimpleValue identifier) {
93-
final GeneratorCreator customIdGeneratorCreator = identifier.getCustomIdGeneratorCreator();
94-
if ( identifier instanceof Component component ) {
95-
final Generator componentIdentifierGenerator = component.createGenerator(
96-
creationContext.getDialect(),
97-
persistentClass.getRootClass(),
98-
persistentClass.getIdentifierProperty(),
99-
creationContext.getGeneratorSettings()
100-
);
101-
identifier.setCustomIdGeneratorCreator( context ->
102-
augmentWithReactiveGenerator( componentIdentifierGenerator, context, creationContext )
103-
);
104-
}
105-
else {
106-
identifier.setCustomIdGeneratorCreator( context ->
107-
augmentWithReactiveGenerator( customIdGeneratorCreator.createGenerator( context ), context, creationContext )
108-
);
109-
}
110-
}
111-
11299
public static Generator augmentWithReactiveGenerator(
113100
Generator generator,
114101
GeneratorCreationContext creationContext,
@@ -156,4 +143,57 @@ private static Generator initialize(
156143
( (Configurable) reactiveIdGenerator ).initialize( creationContext.getSqlStringGenerationContext() );
157144
return new ReactiveGeneratorWrapper( reactiveIdGenerator, idGenerator );
158145
}
146+
147+
private record IdGeneratorCreationContext(
148+
RootClass rootClass,
149+
Property property,
150+
GeneratorSettings defaults,
151+
SimpleValue identifier,
152+
RuntimeModelCreationContext buildingContext) implements GeneratorCreationContext {
153+
154+
@Override
155+
public Database getDatabase() {
156+
return buildingContext.getBootModel().getDatabase();
157+
}
158+
159+
@Override
160+
public ServiceRegistry getServiceRegistry() {
161+
return buildingContext.getBootstrapContext().getServiceRegistry();
162+
}
163+
164+
@Override
165+
public SqlStringGenerationContext getSqlStringGenerationContext() {
166+
return defaults.getSqlStringGenerationContext();
167+
}
168+
169+
@Override
170+
public String getDefaultCatalog() {
171+
return defaults.getDefaultCatalog();
172+
}
173+
174+
@Override
175+
public String getDefaultSchema() {
176+
return defaults.getDefaultSchema();
177+
}
178+
179+
@Override
180+
public RootClass getRootClass() {
181+
return rootClass;
182+
}
183+
184+
@Override
185+
public PersistentClass getPersistentClass() {
186+
return rootClass;
187+
}
188+
189+
@Override
190+
public Property getProperty() {
191+
return property;
192+
}
193+
194+
@Override
195+
public Type getType() {
196+
return identifier.getType();
197+
}
198+
}
159199
}

0 commit comments

Comments
 (0)