36
36
import org .hibernate .annotations .CollectionTypeRegistrations ;
37
37
import org .hibernate .annotations .Columns ;
38
38
import org .hibernate .annotations .Comment ;
39
+ import org .hibernate .annotations .DefaultSchema ;
39
40
import org .hibernate .annotations .DiscriminatorFormula ;
40
41
import org .hibernate .annotations .DiscriminatorOptions ;
41
42
import org .hibernate .annotations .EmbeddableInstantiatorRegistration ;
@@ -589,9 +590,9 @@ public static void bindClass(
589
590
bindQueries ( clazzToProcess , context );
590
591
bindFilterDefs ( clazzToProcess , context );
591
592
592
- String schema = "" ;
593
- String table = "" ; //might be no @Table annotation on the annotated class
594
- String catalog = "" ;
593
+ String schema ;
594
+ String catalog ;
595
+ String table ;
595
596
List <UniqueConstraintHolder > uniqueConstraints = new ArrayList <>();
596
597
jakarta .persistence .Table tabAnn = null ;
597
598
if ( clazzToProcess .isAnnotationPresent ( jakarta .persistence .Table .class ) ) {
@@ -601,6 +602,19 @@ public static void bindClass(
601
602
catalog = tabAnn .catalog ();
602
603
uniqueConstraints = TableBinder .buildUniqueConstraintHolders ( tabAnn .uniqueConstraints () );
603
604
}
605
+ else {
606
+ //might be no @Table annotation on the annotated class
607
+ schema = "" ;
608
+ catalog = "" ;
609
+ table = "" ;
610
+ }
611
+
612
+ if ( schema .isEmpty () ) {
613
+ schema = defaultSchema ( clazzToProcess );
614
+ }
615
+ if ( catalog .isEmpty () ) {
616
+ catalog = defaultCatalog ( clazzToProcess );
617
+ }
604
618
605
619
AnnotatedJoinColumn [] inheritanceJoinedColumns = makeInheritanceJoinColumns (
606
620
clazzToProcess ,
@@ -842,6 +856,34 @@ else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
842
856
bindCallbacks ( clazzToProcess , persistentClass , context );
843
857
}
844
858
859
+ public static String defaultSchema (XClass clazzToProcess ) {
860
+ XAnnotatedElement annotatedElement = clazzToProcess ;
861
+ while (annotatedElement instanceof XClass ) {
862
+ if ( clazzToProcess .isAnnotationPresent (DefaultSchema .class ) ) {
863
+ return clazzToProcess .getAnnotation (DefaultSchema .class ).schema ();
864
+ }
865
+ annotatedElement = clazzToProcess .getContainingElement ();
866
+ }
867
+ if ( annotatedElement .isAnnotationPresent (DefaultSchema .class ) ) {
868
+ return annotatedElement .getAnnotation (DefaultSchema .class ).schema ();
869
+ }
870
+ return "" ;
871
+ }
872
+
873
+ public static String defaultCatalog (XClass clazzToProcess ) {
874
+ XAnnotatedElement annotatedElement = clazzToProcess ;
875
+ while (annotatedElement instanceof XClass ) {
876
+ if ( clazzToProcess .isAnnotationPresent (DefaultSchema .class ) ) {
877
+ return clazzToProcess .getAnnotation (DefaultSchema .class ).catalog ();
878
+ }
879
+ annotatedElement = clazzToProcess .getContainingElement ();
880
+ }
881
+ if ( annotatedElement .isAnnotationPresent (DefaultSchema .class ) ) {
882
+ return annotatedElement .getAnnotation (DefaultSchema .class ).catalog ();
883
+ }
884
+ return "" ;
885
+ }
886
+
845
887
private static void handleTypeDescriptorRegistrations (XAnnotatedElement annotatedElement , MetadataBuildingContext context ) {
846
888
final ManagedBeanRegistry managedBeanRegistry = context .getBootstrapContext ()
847
889
.getServiceRegistry ()
@@ -2654,9 +2696,15 @@ private static void bindJoinedTableAssociation(
2654
2696
if ( !BinderHelper .isEmptyAnnotationValue ( schema ) ) {
2655
2697
associationTableBinder .setSchema ( schema );
2656
2698
}
2699
+ else {
2700
+ associationTableBinder .setSchema ( defaultSchema ( property .getDeclaringClass () ) );
2701
+ }
2657
2702
if ( !BinderHelper .isEmptyAnnotationValue ( catalog ) ) {
2658
2703
associationTableBinder .setCatalog ( catalog );
2659
2704
}
2705
+ else {
2706
+ associationTableBinder .setCatalog ( defaultCatalog ( property .getDeclaringClass () ) );
2707
+ }
2660
2708
if ( !BinderHelper .isEmptyAnnotationValue ( tableName ) ) {
2661
2709
associationTableBinder .setName ( tableName );
2662
2710
}
@@ -2669,6 +2717,8 @@ private static void bindJoinedTableAssociation(
2669
2717
else {
2670
2718
annJoins = null ;
2671
2719
annInverseJoins = null ;
2720
+ associationTableBinder .setSchema ( defaultSchema ( property .getDeclaringClass () ) );
2721
+ associationTableBinder .setCatalog ( defaultCatalog ( property .getDeclaringClass () ) );
2672
2722
}
2673
2723
AnnotatedJoinColumn [] joinColumns = AnnotatedJoinColumn .buildJoinTableJoinColumns (
2674
2724
annJoins ,
0 commit comments