1
1
package io .quarkus .hibernate .orm .deployment ;
2
2
3
+ import static org .apache .commons .lang3 .BooleanUtils .isFalse ;
4
+
3
5
import java .util .ArrayList ;
4
6
import java .util .Arrays ;
5
7
import java .util .List ;
6
8
import java .util .Locale ;
7
9
import java .util .Set ;
10
+ import java .util .function .Function ;
8
11
import java .util .stream .Collectors ;
9
12
10
13
import jakarta .enterprise .context .ApplicationScoped ;
18
21
import org .hibernate .Session ;
19
22
import org .hibernate .SessionFactory ;
20
23
import org .hibernate .StatelessSession ;
24
+ import org .hibernate .query .criteria .HibernateCriteriaBuilder ;
25
+ import org .hibernate .relational .SchemaManager ;
21
26
import org .jboss .jandex .AnnotationInstance ;
22
27
import org .jboss .jandex .AnnotationTarget .Kind ;
28
+ import org .jboss .jandex .AnnotationTransformation ;
23
29
import org .jboss .jandex .AnnotationValue ;
24
30
import org .jboss .jandex .ClassType ;
25
31
import org .jboss .jandex .DotName ;
@@ -64,6 +70,13 @@ public class HibernateOrmCdiProcessor {
64
70
ClassNames .SESSION_FACTORY );
65
71
private static final List <DotName > SESSION_EXPOSED_TYPES = Arrays .asList (ClassNames .ENTITY_MANAGER , ClassNames .SESSION );
66
72
private static final List <DotName > STATELESS_SESSION_EXPOSED_TYPES = List .of (ClassNames .STATELESS_SESSION );
73
+ private static final List <DotName > CRITERIA_BUILDER_EXPOSED_TYPES = List .of (ClassNames .CRITERIA_BUILDER ,
74
+ ClassNames .HIBERNATE_CRITERIA_BUILDER );
75
+ private static final List <DotName > METAMODEL_EXPOSED_TYPES = List .of (ClassNames .METAMODEL , ClassNames .JPA_METAMODEL ,
76
+ ClassNames .HIBERNATE_METAMODEL );
77
+ private static final List <DotName > SCHEMA_MANAGER_EXPOSED_TYPES = List .of (ClassNames .SCHEMA_MANAGER );
78
+ private static final List <DotName > CACHE_EXPOSED_TYPES = List .of (ClassNames .CACHE , ClassNames .HIBERNATE_CACHE );
79
+ private static final List <DotName > PERSISTENCE_UNIT_UTIL_EXPOSED_TYPES = List .of (ClassNames .PERSISTENCE_UNIT_UTIL );
67
80
68
81
private static final Set <DotName > PERSISTENCE_UNIT_EXTENSION_VALID_TYPES = Set .of (
69
82
ClassNames .TENANT_RESOLVER ,
@@ -76,7 +89,7 @@ public class HibernateOrmCdiProcessor {
76
89
AnnotationsTransformerBuildItem convertJpaResourceAnnotationsToQualifier (
77
90
List <PersistenceUnitDescriptorBuildItem > persistenceUnitDescriptors ,
78
91
ImpliedBlockingPersistenceUnitTypeBuildItem impliedBlockingPersistenceUnitType ) {
79
- AnnotationsTransformer transformer = new AnnotationsTransformer () {
92
+ AnnotationTransformation transformer = new AnnotationsTransformer () {
80
93
81
94
@ Override
82
95
public boolean appliesTo (Kind kind ) {
@@ -178,34 +191,22 @@ void generateDataSourceBeans(HibernateOrmRecorder recorder,
178
191
return ;
179
192
}
180
193
194
+ Function <String , AnnotationInstance > createPersistenceUnitQualifier = (puName ) -> AnnotationInstance
195
+ .builder (PersistenceUnit .class ).add ("value" , puName ).build ();
196
+ AnnotationInstance defaultQualifierInstance = AnnotationInstance .builder (Default .class ).build ();
197
+
181
198
// we have only one persistence unit defined in a persistence.xml: we make it the default even if it has a name
182
199
// NOTE: In this case we know we're not using Hibernate Reactive, because it doesn't support persistence.xml.
183
200
if (persistenceUnitDescriptors .size () == 1 && persistenceUnitDescriptors .get (0 ).isFromPersistenceXml ()) {
184
201
String persistenceUnitName = persistenceUnitDescriptors .get (0 ).getPersistenceUnitName ();
185
202
186
- syntheticBeanBuildItemBuildProducer
187
- .produce (createSyntheticBean (persistenceUnitName ,
188
- true , true ,
189
- SessionFactory .class , SESSION_FACTORY_EXPOSED_TYPES , true )
190
- .createWith (recorder .sessionFactorySupplier (persistenceUnitName ))
191
- .addInjectionPoint (ClassType .create (DotName .createSimple (JPAConfig .class )))
192
- .done ());
193
-
194
- syntheticBeanBuildItemBuildProducer
195
- .produce (createSyntheticBean (persistenceUnitName ,
196
- true , true ,
197
- Session .class , SESSION_EXPOSED_TYPES , false )
198
- .createWith (recorder .sessionSupplier (persistenceUnitName ))
199
- .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
200
- .done ());
201
-
202
- syntheticBeanBuildItemBuildProducer
203
- .produce (createSyntheticBean (persistenceUnitName ,
204
- true , true ,
205
- StatelessSession .class , STATELESS_SESSION_EXPOSED_TYPES , false )
206
- .createWith (recorder .statelessSessionSupplier (persistenceUnitName ))
207
- .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
208
- .done ());
203
+ produceSessionFactoryBean (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName , true , true );
204
+
205
+ produceSessionBeans (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName , true , true );
206
+
207
+ produceFactoryDependentBeans (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName ,
208
+ true , true , defaultQualifierInstance );
209
+
209
210
return ;
210
211
}
211
212
@@ -223,34 +224,22 @@ void generateDataSourceBeans(HibernateOrmRecorder recorder,
223
224
// We will need to fix this at some point, see https://github.com/quarkusio/quarkus/issues/21110
224
225
String persistenceUnitConfigName = persistenceUnitDescriptor .getConfigurationName ();
225
226
boolean isDefaultPU = PersistenceUnitUtil .isDefaultPersistenceUnit (persistenceUnitConfigName );
226
- boolean isNamedPU = !isDefaultPU ;
227
-
228
- syntheticBeanBuildItemBuildProducer
229
- .produce (createSyntheticBean (persistenceUnitName ,
230
- isDefaultPU ,
231
- isNamedPU ,
232
- SessionFactory .class ,
233
- SESSION_FACTORY_EXPOSED_TYPES ,
234
- true )
235
- .createWith (recorder .sessionFactorySupplier (persistenceUnitName ))
236
- .addInjectionPoint (ClassType .create (DotName .createSimple (JPAConfig .class )))
237
- .done ());
238
-
239
- syntheticBeanBuildItemBuildProducer
240
- .produce (createSyntheticBean (persistenceUnitName ,
241
- isDefaultPU , isNamedPU ,
242
- Session .class , SESSION_EXPOSED_TYPES , false )
243
- .createWith (recorder .sessionSupplier (persistenceUnitName ))
244
- .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
245
- .done ());
246
-
247
- syntheticBeanBuildItemBuildProducer
248
- .produce (createSyntheticBean (persistenceUnitName ,
249
- isDefaultPU , isNamedPU ,
250
- StatelessSession .class , STATELESS_SESSION_EXPOSED_TYPES , false )
251
- .createWith (recorder .statelessSessionSupplier (persistenceUnitName ))
252
- .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
253
- .done ());
227
+ boolean isNamedPU = isFalse (isDefaultPU );
228
+ AnnotationInstance sessionFactoryQualifier ;
229
+ if (isDefaultPU ) {
230
+ sessionFactoryQualifier = defaultQualifierInstance ;
231
+ } else {
232
+ sessionFactoryQualifier = createPersistenceUnitQualifier .apply (persistenceUnitName );
233
+ }
234
+
235
+ produceSessionFactoryBean (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName , isDefaultPU ,
236
+ isNamedPU );
237
+
238
+ produceSessionBeans (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName , isDefaultPU , isNamedPU );
239
+
240
+ produceFactoryDependentBeans (syntheticBeanBuildItemBuildProducer , recorder , persistenceUnitName ,
241
+ isDefaultPU , isNamedPU , sessionFactoryQualifier );
242
+
254
243
}
255
244
}
256
245
@@ -352,4 +341,98 @@ private static <T> ExtendedBeanConfigurator createSyntheticBean(String persisten
352
341
353
342
return configurator ;
354
343
}
344
+
345
+ private void produceSessionBeans (
346
+ BuildProducer <SyntheticBeanBuildItem > producer ,
347
+ HibernateOrmRecorder recorder ,
348
+ String persistenceUnitName ,
349
+ boolean isDefaultPU ,
350
+ boolean isNamedPU ) {
351
+
352
+ // Create Session bean
353
+ producer .produce (createSyntheticBean (persistenceUnitName ,
354
+ isDefaultPU , isNamedPU ,
355
+ Session .class , SESSION_EXPOSED_TYPES , false )
356
+ .createWith (recorder .sessionSupplier (persistenceUnitName ))
357
+ .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
358
+ .done ());
359
+
360
+ // Create StatelessSession bean
361
+ producer .produce (createSyntheticBean (persistenceUnitName ,
362
+ isDefaultPU , isNamedPU ,
363
+ StatelessSession .class , STATELESS_SESSION_EXPOSED_TYPES , false )
364
+ .createWith (recorder .statelessSessionSupplier (persistenceUnitName ))
365
+ .addInjectionPoint (ClassType .create (DotName .createSimple (TransactionSessions .class )))
366
+ .done ());
367
+ }
368
+
369
+ private void produceSessionFactoryBean (
370
+ BuildProducer <SyntheticBeanBuildItem > producer ,
371
+ HibernateOrmRecorder recorder ,
372
+ String persistenceUnitName ,
373
+ boolean isDefaultPU ,
374
+ boolean isNamedPU ) {
375
+
376
+ producer .produce (createSyntheticBean (persistenceUnitName ,
377
+ isDefaultPU , isNamedPU ,
378
+ SessionFactory .class , SESSION_FACTORY_EXPOSED_TYPES , true )
379
+ .createWith (recorder .sessionFactorySupplier (persistenceUnitName ))
380
+ .addInjectionPoint (ClassType .create (DotName .createSimple (JPAConfig .class )))
381
+ .done ());
382
+ }
383
+
384
+ private void produceFactoryDependentBeans (
385
+ BuildProducer <SyntheticBeanBuildItem > producer ,
386
+ HibernateOrmRecorder recorder ,
387
+ String persistenceUnitName ,
388
+ boolean isDefaultPU ,
389
+ boolean isNamedPU ,
390
+ AnnotationInstance sessionFactoryQualifier ) {
391
+
392
+ // Create CriteriaBuilder bean
393
+ producer .produce (createSyntheticBean (persistenceUnitName ,
394
+ isDefaultPU , isNamedPU ,
395
+ HibernateCriteriaBuilder .class , CRITERIA_BUILDER_EXPOSED_TYPES , false )
396
+ .createWith (recorder .criteriaBuilderSupplier (persistenceUnitName ))
397
+ .addInjectionPoint (ClassType .create (DotName .createSimple (SessionFactory .class )),
398
+ sessionFactoryQualifier )
399
+ .done ());
400
+
401
+ // Create Metamodel bean
402
+ producer .produce (createSyntheticBean (persistenceUnitName ,
403
+ isDefaultPU , isNamedPU ,
404
+ org .hibernate .Metamodel .class , METAMODEL_EXPOSED_TYPES , false )
405
+ .createWith (recorder .metamodelSupplier (persistenceUnitName ))
406
+ .addInjectionPoint (ClassType .create (DotName .createSimple (SessionFactory .class )),
407
+ sessionFactoryQualifier )
408
+ .done ());
409
+
410
+ // Create SchemaManager bean
411
+ producer .produce (createSyntheticBean (persistenceUnitName ,
412
+ isDefaultPU , isNamedPU ,
413
+ SchemaManager .class , SCHEMA_MANAGER_EXPOSED_TYPES , false )
414
+ .createWith (recorder .schemaManagerSupplier (persistenceUnitName ))
415
+ .addInjectionPoint (ClassType .create (DotName .createSimple (SessionFactory .class )),
416
+ sessionFactoryQualifier )
417
+ .done ());
418
+
419
+ // Create Cache bean
420
+ producer .produce (createSyntheticBean (persistenceUnitName ,
421
+ isDefaultPU , isNamedPU ,
422
+ org .hibernate .Cache .class , CACHE_EXPOSED_TYPES , false )
423
+ .createWith (recorder .cacheSupplier (persistenceUnitName ))
424
+ .addInjectionPoint (ClassType .create (DotName .createSimple (SessionFactory .class )),
425
+ sessionFactoryQualifier )
426
+ .done ());
427
+
428
+ // Create PersistenceUnitUtil bean
429
+ producer .produce (createSyntheticBean (persistenceUnitName ,
430
+ isDefaultPU , isNamedPU ,
431
+ jakarta .persistence .PersistenceUnitUtil .class , PERSISTENCE_UNIT_UTIL_EXPOSED_TYPES , false )
432
+ .createWith (recorder .persistenceUnitUtilSupplier (persistenceUnitName ))
433
+ .addInjectionPoint (ClassType .create (DotName .createSimple (SessionFactory .class )),
434
+ sessionFactoryQualifier )
435
+ .done ());
436
+ }
437
+
355
438
}
0 commit comments