|
8 | 8 | import java.util.function.Function; |
9 | 9 |
|
10 | 10 |
|
| 11 | +import org.hibernate.boot.model.relational.Database; |
| 12 | +import org.hibernate.boot.model.relational.SqlStringGenerationContext; |
11 | 13 | import org.hibernate.generator.Generator; |
12 | 14 | import org.hibernate.generator.GeneratorCreationContext; |
13 | 15 | import org.hibernate.id.CompositeNestedGeneratedValueGenerator; |
|
19 | 21 | import org.hibernate.id.enhanced.SequenceStyleGenerator; |
20 | 22 | import org.hibernate.id.enhanced.TableGenerator; |
21 | 23 | import org.hibernate.id.enhanced.TableStructure; |
22 | | -import org.hibernate.mapping.Component; |
23 | | -import org.hibernate.mapping.GeneratorCreator; |
| 24 | +import org.hibernate.mapping.GeneratorSettings; |
24 | 25 | import org.hibernate.mapping.PersistentClass; |
| 26 | +import org.hibernate.mapping.Property; |
| 27 | +import org.hibernate.mapping.RootClass; |
25 | 28 | import org.hibernate.mapping.SimpleValue; |
26 | 29 | import org.hibernate.metamodel.spi.RuntimeModelCreationContext; |
27 | 30 | import org.hibernate.persister.entity.EntityPersister; |
|
32 | 35 | import org.hibernate.reactive.id.impl.ReactiveSequenceIdentifierGenerator; |
33 | 36 | import org.hibernate.reactive.id.impl.TableReactiveIdentifierGenerator; |
34 | 37 | import org.hibernate.reactive.logging.impl.Log; |
| 38 | +import org.hibernate.service.ServiceRegistry; |
35 | 39 | import org.hibernate.tuple.entity.EntityMetamodel; |
| 40 | +import org.hibernate.type.Type; |
36 | 41 |
|
37 | 42 | import static java.lang.invoke.MethodHandles.lookup; |
38 | 43 | import static org.hibernate.reactive.logging.impl.LoggerFactory.make; |
@@ -71,44 +76,26 @@ private static Generator buildIdGenerator( |
71 | 76 | } |
72 | 77 | else { |
73 | 78 | 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( |
79 | 81 | creationContext.getDialect(), |
80 | 82 | persistentClass.getRootClass(), |
81 | 83 | persistentClass.getIdentifierProperty(), |
82 | 84 | creationContext.getGeneratorSettings() |
83 | | - ); |
| 85 | + ), |
| 86 | + new IdGeneratorCreationContext( |
| 87 | + persistentClass.getRootClass(), |
| 88 | + persistentClass.getIdentifierProperty(), |
| 89 | + creationContext.getGeneratorSettings(), |
| 90 | + identifier, |
| 91 | + creationContext |
| 92 | + ), |
| 93 | + creationContext ); |
84 | 94 | creationContext.getGenerators().put( rootName, idgenerator ); |
85 | 95 | return idgenerator; |
86 | 96 | } |
87 | 97 | } |
88 | 98 |
|
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 | | - |
112 | 99 | public static Generator augmentWithReactiveGenerator( |
113 | 100 | Generator generator, |
114 | 101 | GeneratorCreationContext creationContext, |
@@ -156,4 +143,57 @@ private static Generator initialize( |
156 | 143 | ( (Configurable) reactiveIdGenerator ).initialize( creationContext.getSqlStringGenerationContext() ); |
157 | 144 | return new ReactiveGeneratorWrapper( reactiveIdGenerator, idGenerator ); |
158 | 145 | } |
| 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 | + } |
159 | 199 | } |
0 commit comments