40
40
import org .hibernate .annotations .Columns ;
41
41
import org .hibernate .annotations .Comment ;
42
42
import org .hibernate .annotations .DialectOverride .OverridesAnnotation ;
43
+ import org .hibernate .annotations .DefaultSchema ;
43
44
import org .hibernate .annotations .DiscriminatorFormula ;
44
45
import org .hibernate .annotations .DiscriminatorOptions ;
45
46
import org .hibernate .annotations .EmbeddableInstantiatorRegistration ;
@@ -595,9 +596,9 @@ public static void bindClass(
595
596
bindQueries ( clazzToProcess , context );
596
597
bindFilterDefs ( clazzToProcess , context );
597
598
598
- String schema = "" ;
599
- String table = "" ; //might be no @Table annotation on the annotated class
600
- String catalog = "" ;
599
+ String schema ;
600
+ String catalog ;
601
+ String table ;
601
602
List <UniqueConstraintHolder > uniqueConstraints = new ArrayList <>();
602
603
jakarta .persistence .Table tabAnn = null ;
603
604
if ( clazzToProcess .isAnnotationPresent ( jakarta .persistence .Table .class ) ) {
@@ -607,6 +608,19 @@ public static void bindClass(
607
608
catalog = tabAnn .catalog ();
608
609
uniqueConstraints = TableBinder .buildUniqueConstraintHolders ( tabAnn .uniqueConstraints () );
609
610
}
611
+ else {
612
+ //might be no @Table annotation on the annotated class
613
+ schema = "" ;
614
+ catalog = "" ;
615
+ table = "" ;
616
+ }
617
+
618
+ if ( schema .isEmpty () ) {
619
+ schema = defaultSchema ( clazzToProcess );
620
+ }
621
+ if ( catalog .isEmpty () ) {
622
+ catalog = defaultCatalog ( clazzToProcess );
623
+ }
610
624
611
625
AnnotatedJoinColumn [] inheritanceJoinedColumns = makeInheritanceJoinColumns (
612
626
clazzToProcess ,
@@ -899,6 +913,36 @@ public static <T extends Annotation> T getOverridableAnnotation(
899
913
return element .getAnnotation ( annotationType );
900
914
}
901
915
916
+ public static String defaultSchema (XClass clazzToProcess ) {
917
+ XAnnotatedElement annotatedElement = clazzToProcess ;
918
+ while (true ) {
919
+ if ( annotatedElement .isAnnotationPresent (DefaultSchema .class ) ) {
920
+ return annotatedElement .getAnnotation (DefaultSchema .class ).schema ();
921
+ }
922
+ else if ( annotatedElement instanceof XClass ) {
923
+ annotatedElement = ((XClass ) annotatedElement ).getContainingElement ();
924
+ }
925
+ else {
926
+ return "" ;
927
+ }
928
+ }
929
+ }
930
+
931
+ public static String defaultCatalog (XClass clazzToProcess ) {
932
+ XAnnotatedElement annotatedElement = clazzToProcess ;
933
+ while (true ) {
934
+ if ( annotatedElement .isAnnotationPresent (DefaultSchema .class ) ) {
935
+ return annotatedElement .getAnnotation (DefaultSchema .class ).catalog ();
936
+ }
937
+ else if ( annotatedElement instanceof XClass ) {
938
+ annotatedElement = ((XClass ) annotatedElement ).getContainingElement ();
939
+ }
940
+ else {
941
+ return "" ;
942
+ }
943
+ }
944
+ }
945
+
902
946
private static void handleTypeDescriptorRegistrations (XAnnotatedElement annotatedElement , MetadataBuildingContext context ) {
903
947
final ManagedBeanRegistry managedBeanRegistry = context .getBootstrapContext ()
904
948
.getServiceRegistry ()
@@ -2707,9 +2751,15 @@ private static void bindJoinedTableAssociation(
2707
2751
if ( !BinderHelper .isEmptyAnnotationValue ( schema ) ) {
2708
2752
associationTableBinder .setSchema ( schema );
2709
2753
}
2754
+ else {
2755
+ associationTableBinder .setSchema ( defaultSchema ( property .getDeclaringClass () ) );
2756
+ }
2710
2757
if ( !BinderHelper .isEmptyAnnotationValue ( catalog ) ) {
2711
2758
associationTableBinder .setCatalog ( catalog );
2712
2759
}
2760
+ else {
2761
+ associationTableBinder .setCatalog ( defaultCatalog ( property .getDeclaringClass () ) );
2762
+ }
2713
2763
if ( !BinderHelper .isEmptyAnnotationValue ( tableName ) ) {
2714
2764
associationTableBinder .setName ( tableName );
2715
2765
}
@@ -2722,6 +2772,8 @@ private static void bindJoinedTableAssociation(
2722
2772
else {
2723
2773
annJoins = null ;
2724
2774
annInverseJoins = null ;
2775
+ associationTableBinder .setSchema ( defaultSchema ( property .getDeclaringClass () ) );
2776
+ associationTableBinder .setCatalog ( defaultCatalog ( property .getDeclaringClass () ) );
2725
2777
}
2726
2778
AnnotatedJoinColumn [] joinColumns = AnnotatedJoinColumn .buildJoinTableJoinColumns (
2727
2779
annJoins ,
0 commit comments