2121import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
2222import org .hibernate .dialect .Dialect ;
2323import org .hibernate .engine .jdbc .Size ;
24+ import org .hibernate .engine .jdbc .env .spi .NameQualifierSupport ;
2425import org .hibernate .generator .Generator ;
2526import org .hibernate .id .OptimizableGenerator ;
2627import org .hibernate .id .enhanced .Optimizer ;
@@ -534,6 +535,8 @@ private static void forEachTemporaryTableColumn(RuntimeModelCreationContext runt
534535 }
535536
536537 private static Table findTable (RuntimeModelCreationContext runtimeModelCreationContext , String tableName ) {
538+ final NameQualifierSupport nameQualifierSupport = runtimeModelCreationContext .getJdbcServices ()
539+ .getJdbcEnvironment ().getNameQualifierSupport ();
537540 final Database database = runtimeModelCreationContext .getMetadata ().getDatabase ();
538541 final QualifiedNameParser .NameParts nameParts = QualifiedNameParser .INSTANCE .parse ( tableName );
539542 // Strip off the default catalog and schema names since these are not reflected in the Database#namespaces
@@ -551,6 +554,41 @@ private static Table findTable(RuntimeModelCreationContext runtimeModelCreationC
551554 if ( schema != null && catalog == null ) {
552555 final Identifier alternativeCatalog = schema .equals ( sqlContext .getDefaultCatalog () ) ? null : schema ;
553556 namespace = database .findNamespace ( alternativeCatalog , null );
557+
558+ if ( namespace == null && nameQualifierSupport == NameQualifierSupport .CATALOG ) {
559+ Namespace candidateNamespace = null ;
560+ for ( Namespace databaseNamespace : database .getNamespaces () ) {
561+ if ( schema .equals ( databaseNamespace .getName ().catalog () ) ) {
562+ if ( candidateNamespace != null ) {
563+ // Two namespaces with the same catalog, but different schema names
564+ candidateNamespace = null ;
565+ break ;
566+ }
567+ candidateNamespace = databaseNamespace ;
568+ }
569+ }
570+ if ( candidateNamespace != null ) {
571+ namespace = candidateNamespace ;
572+ }
573+ }
574+ }
575+ if ( namespace == null ) {
576+ if ( nameQualifierSupport == NameQualifierSupport .SCHEMA && schema != null ) {
577+ Namespace candidateNamespace = null ;
578+ for ( Namespace databaseNamespace : database .getNamespaces () ) {
579+ if ( schema .equals ( databaseNamespace .getName ().schema () ) ) {
580+ if ( candidateNamespace != null ) {
581+ // Two namespaces with the same schema, but different catalog names
582+ candidateNamespace = null ;
583+ break ;
584+ }
585+ candidateNamespace = databaseNamespace ;
586+ }
587+ }
588+ if ( candidateNamespace != null ) {
589+ namespace = candidateNamespace ;
590+ }
591+ }
554592 }
555593 if ( namespace == null ) {
556594 throw new IllegalArgumentException ( "Unable to find namespace for " + tableName );
0 commit comments