@@ -892,39 +892,53 @@ private void handleHibernateORMWithNoPersistenceXml(
892
892
&& hibernateOrmConfig .namedPersistenceUnits ().isEmpty ())
893
893
|| hibernateOrmConfig .defaultPersistenceUnit ().isAnyPropertySet ();
894
894
895
- Map < String , Set < String >> modelClassesAndPackagesPerPersistencesUnits = getModelClassesAndPackagesPerPersistenceUnits (
895
+ var modelPerPersistencesUnit = getModelPerPersistenceUnit (
896
896
hibernateOrmConfig , additionalJpaModelBuildItems , jpaModel , index .getIndex (), enableDefaultPersistenceUnit );
897
- Set <String > modelClassesAndPackagesForDefaultPersistenceUnit = modelClassesAndPackagesPerPersistencesUnits
898
- .getOrDefault (PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME , Collections .emptySet ());
897
+ var modelForDefaultPersistenceUnit = modelPerPersistencesUnit .get (PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME );
898
+ if (modelForDefaultPersistenceUnit == null ) {
899
+ modelForDefaultPersistenceUnit = new JpaPersistenceUnitModel ();
900
+ }
899
901
900
902
Set <String > storageEngineCollector = new HashSet <>();
901
903
902
904
if (enableDefaultPersistenceUnit ) {
903
905
producePersistenceUnitDescriptorFromConfig (
904
906
hibernateOrmConfig , jpaModel , PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME ,
905
907
hibernateOrmConfig .defaultPersistenceUnit (),
906
- modelClassesAndPackagesForDefaultPersistenceUnit ,
908
+ modelForDefaultPersistenceUnit . allModelClassAndPackageNames () ,
907
909
jpaModel .getXmlMappings (PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME ),
908
910
jdbcDataSources , applicationArchivesBuildItem , launchMode , capabilities ,
909
911
systemProperties , nativeImageResources , hotDeploymentWatchedFiles , persistenceUnitDescriptors ,
910
912
reflectiveMethods , unremovableBeans , storageEngineCollector , dbKindMetadataBuildItems );
911
- } else if (!modelClassesAndPackagesForDefaultPersistenceUnit .isEmpty ()
913
+ } else if (!modelForDefaultPersistenceUnit . entityClassNames () .isEmpty ()
912
914
&& (!hibernateOrmConfig .defaultPersistenceUnit ().datasource ().isPresent ()
913
915
|| DataSourceUtil .isDefault (hibernateOrmConfig .defaultPersistenceUnit ().datasource ().get ()))
914
916
&& !defaultJdbcDataSource .isPresent ()) {
917
+ // We're not enable the default PU, meaning there is no explicit configuration for it,
918
+ // and we couldn't find a default datasource.
919
+ // But there are entities assigned to it, meaning these entities can never work properly.
920
+ // This looks like a mistake, so we'll error out.
915
921
String persistenceUnitName = PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME ;
916
922
String dataSourceName = DataSourceUtil .DEFAULT_DATASOURCE_NAME ;
917
- throw PersistenceUnitUtil .unableToFindDataSource (persistenceUnitName , dataSourceName ,
918
- DataSourceUtil .dataSourceNotConfigured (dataSourceName ));
923
+ var cause = DataSourceUtil .dataSourceNotConfigured (dataSourceName );
924
+ throw new ConfigurationException (String .format (Locale .ROOT ,
925
+ "Persistence unit '%s' defines entities %s, but its datasource '%s' cannot be found: %s"
926
+ + " Alternatively, disable Hibernate ORM by setting '%s=false', and the entities will be ignored." ,
927
+ persistenceUnitName , modelForDefaultPersistenceUnit .entityClassNames (),
928
+ dataSourceName ,
929
+ cause .getMessage (),
930
+ HibernateOrmRuntimeConfig .extensionPropertyKey ("enabled" )),
931
+ cause );
919
932
}
920
933
921
934
for (Entry <String , HibernateOrmConfigPersistenceUnit > persistenceUnitEntry : hibernateOrmConfig .namedPersistenceUnits ()
922
935
.entrySet ()) {
936
+ var persistenceUnitName = persistenceUnitEntry .getKey ();
937
+ var model = modelPerPersistencesUnit .get (persistenceUnitEntry .getKey ());
923
938
producePersistenceUnitDescriptorFromConfig (
924
- hibernateOrmConfig , jpaModel , persistenceUnitEntry .getKey (), persistenceUnitEntry .getValue (),
925
- modelClassesAndPackagesPerPersistencesUnits .getOrDefault (persistenceUnitEntry .getKey (),
926
- Collections .emptySet ()),
927
- jpaModel .getXmlMappings (persistenceUnitEntry .getKey ()),
939
+ hibernateOrmConfig , jpaModel , persistenceUnitName , persistenceUnitEntry .getValue (),
940
+ model == null ? Collections .emptySet () : model .allModelClassAndPackageNames (),
941
+ jpaModel .getXmlMappings (persistenceUnitName ),
928
942
jdbcDataSources , applicationArchivesBuildItem , launchMode , capabilities ,
929
943
systemProperties , nativeImageResources , hotDeploymentWatchedFiles , persistenceUnitDescriptors ,
930
944
reflectiveMethods , unremovableBeans , storageEngineCollector , dbKindMetadataBuildItems );
@@ -1136,10 +1150,10 @@ private void enhanceEntities(final JpaModelBuildItem jpaModel,
1136
1150
}
1137
1151
}
1138
1152
1139
- public static Map <String , Set < String >> getModelClassesAndPackagesPerPersistenceUnits (HibernateOrmConfig hibernateOrmConfig ,
1153
+ public static Map <String , JpaPersistenceUnitModel > getModelPerPersistenceUnit (HibernateOrmConfig hibernateOrmConfig ,
1140
1154
List <AdditionalJpaModelBuildItem > additionalJpaModelBuildItems , JpaModelBuildItem jpaModel ,
1141
1155
IndexView index , boolean enableDefaultPersistenceUnit ) {
1142
- Map <String , Set < String >> modelClassesAndPackagesPerPersistenceUnits = new HashMap <>();
1156
+ Map <String , JpaPersistenceUnitModel > modelPerPersistenceUnit = new HashMap <>();
1143
1157
1144
1158
boolean hasPackagesInQuarkusConfig = hasPackagesInQuarkusConfig (hibernateOrmConfig );
1145
1159
Collection <AnnotationInstance > packageLevelPersistenceUnitAnnotations = getPackageLevelPersistenceUnitAnnotations (
@@ -1205,9 +1219,11 @@ public static Map<String, Set<String>> getModelClassesAndPackagesPerPersistenceU
1205
1219
// No .packages configuration, no package-level persistence unit annotations,
1206
1220
// and no named persistence units: all the entities will be associated with the default one
1207
1221
// so we don't need to split them
1208
- Set <String > allModelClassesAndPackages = new HashSet <>(jpaModel .getAllModelClassNames ());
1209
- allModelClassesAndPackages .addAll (jpaModel .getAllModelPackageNames ());
1210
- return Collections .singletonMap (PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME , allModelClassesAndPackages );
1222
+ var model = new JpaPersistenceUnitModel ();
1223
+ model .entityClassNames ().addAll (jpaModel .getEntityClassNames ());
1224
+ model .allModelClassAndPackageNames ().addAll (jpaModel .getAllModelClassNames ());
1225
+ model .allModelClassAndPackageNames ().addAll (jpaModel .getAllModelPackageNames ());
1226
+ return Map .of (PersistenceUnitUtil .DEFAULT_PERSISTENCE_UNIT_NAME , model );
1211
1227
}
1212
1228
1213
1229
Set <String > modelClassesWithPersistenceUnitAnnotations = new TreeSet <>();
@@ -1225,13 +1241,18 @@ public static Map<String, Set<String>> getModelClassesAndPackagesPerPersistenceU
1225
1241
for (Entry <String , Set <String >> packageRuleEntry : packageRules .entrySet ()) {
1226
1242
if (modelClassName .startsWith (packageRuleEntry .getKey ())) {
1227
1243
for (String persistenceUnitName : packageRuleEntry .getValue ()) {
1228
- modelClassesAndPackagesPerPersistenceUnits .putIfAbsent (persistenceUnitName , new HashSet <>());
1229
- modelClassesAndPackagesPerPersistenceUnits .get (persistenceUnitName ).add (modelClassName );
1244
+ var model = modelPerPersistenceUnit .computeIfAbsent (persistenceUnitName ,
1245
+ ignored -> new JpaPersistenceUnitModel ());
1246
+
1247
+ if (jpaModel .getEntityClassNames ().contains (modelClassName )) {
1248
+ model .entityClassNames ().add (modelClassName );
1249
+ }
1250
+ model .allModelClassAndPackageNames ().add (modelClassName );
1230
1251
1231
1252
// also add the hierarchy to the persistence unit
1232
1253
// we would need to add all the underlying model to it but adding the hierarchy
1233
1254
// is necessary for Panache as we need to add PanacheEntity to the PU
1234
- modelClassesAndPackagesPerPersistenceUnits . get ( persistenceUnitName ).addAll (relatedModelClassNames );
1255
+ model . allModelClassAndPackageNames ( ).addAll (relatedModelClassNames );
1235
1256
}
1236
1257
}
1237
1258
}
@@ -1247,8 +1268,13 @@ public static Map<String, Set<String>> getModelClassesAndPackagesPerPersistenceU
1247
1268
}
1248
1269
assignedModelClasses .add (className ); // Even if persistenceUnits is empty, the class is still assigned (to nothing)
1249
1270
for (String persistenceUnitName : persistenceUnits ) {
1250
- modelClassesAndPackagesPerPersistenceUnits .putIfAbsent (persistenceUnitName , new HashSet <>());
1251
- modelClassesAndPackagesPerPersistenceUnits .get (persistenceUnitName ).add (className );
1271
+ var model = modelPerPersistenceUnit .computeIfAbsent (persistenceUnitName ,
1272
+ ignored -> new JpaPersistenceUnitModel ());
1273
+
1274
+ if (jpaModel .getEntityClassNames ().contains (className )) {
1275
+ model .entityClassNames ().add (className );
1276
+ }
1277
+ model .allModelClassAndPackageNames ().add (className );
1252
1278
}
1253
1279
}
1254
1280
@@ -1258,7 +1284,8 @@ public static Map<String, Set<String>> getModelClassesAndPackagesPerPersistenceU
1258
1284
String .join ("\n \t - " , modelClassesWithPersistenceUnitAnnotations )));
1259
1285
}
1260
1286
1261
- assignedModelClasses .addAll (modelClassesAndPackagesPerPersistenceUnits .values ().stream ().flatMap (Set ::stream ).toList ());
1287
+ assignedModelClasses .addAll (modelPerPersistenceUnit .values ().stream ()
1288
+ .map (JpaPersistenceUnitModel ::allModelClassAndPackageNames ).flatMap (Set ::stream ).toList ());
1262
1289
Set <String > unaffectedModelClasses = jpaModel .getAllModelClassNames ().stream ()
1263
1290
.filter (c -> !assignedModelClasses .contains (c ))
1264
1291
.collect (Collectors .toCollection (TreeSet ::new ));
@@ -1274,12 +1301,13 @@ public static Map<String, Set<String>> getModelClassesAndPackagesPerPersistenceU
1274
1301
continue ;
1275
1302
}
1276
1303
for (String persistenceUnitName : persistenceUnitNames ) {
1277
- modelClassesAndPackagesPerPersistenceUnits .putIfAbsent (persistenceUnitName , new HashSet <>());
1278
- modelClassesAndPackagesPerPersistenceUnits .get (persistenceUnitName ).add (modelPackageName );
1304
+ var model = modelPerPersistenceUnit .computeIfAbsent (persistenceUnitName ,
1305
+ ignored -> new JpaPersistenceUnitModel ());
1306
+ model .allModelClassAndPackageNames ().add (modelPackageName );
1279
1307
}
1280
1308
}
1281
1309
1282
- return modelClassesAndPackagesPerPersistenceUnits ;
1310
+ return modelPerPersistenceUnit ;
1283
1311
}
1284
1312
1285
1313
private static Set <String > getRelatedModelClassNames (IndexView index , Set <String > knownModelClassNames ,
0 commit comments