21
21
import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
22
22
import org .hibernate .dialect .Dialect ;
23
23
import org .hibernate .engine .jdbc .Size ;
24
+ import org .hibernate .engine .jdbc .env .spi .NameQualifierSupport ;
24
25
import org .hibernate .generator .Generator ;
25
26
import org .hibernate .id .OptimizableGenerator ;
26
27
import org .hibernate .id .enhanced .Optimizer ;
@@ -534,6 +535,8 @@ private static void forEachTemporaryTableColumn(RuntimeModelCreationContext runt
534
535
}
535
536
536
537
private static Table findTable (RuntimeModelCreationContext runtimeModelCreationContext , String tableName ) {
538
+ final NameQualifierSupport nameQualifierSupport = runtimeModelCreationContext .getJdbcServices ()
539
+ .getJdbcEnvironment ().getNameQualifierSupport ();
537
540
final Database database = runtimeModelCreationContext .getMetadata ().getDatabase ();
538
541
final QualifiedNameParser .NameParts nameParts = QualifiedNameParser .INSTANCE .parse ( tableName );
539
542
// 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
551
554
if ( schema != null && catalog == null ) {
552
555
final Identifier alternativeCatalog = schema .equals ( sqlContext .getDefaultCatalog () ) ? null : schema ;
553
556
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
+ }
554
592
}
555
593
if ( namespace == null ) {
556
594
throw new IllegalArgumentException ( "Unable to find namespace for " + tableName );
0 commit comments