9
9
import java .lang .annotation .Annotation ;
10
10
import java .lang .reflect .InvocationTargetException ;
11
11
import java .lang .reflect .Member ;
12
+ import java .lang .reflect .Method ;
12
13
import java .util .ArrayList ;
13
14
import java .util .Arrays ;
14
15
import java .util .Collections ;
47
48
import org .hibernate .annotations .FilterDef ;
48
49
import org .hibernate .annotations .FilterDefs ;
49
50
import org .hibernate .annotations .Filters ;
51
+ import org .hibernate .annotations .ForDialect ;
50
52
import org .hibernate .annotations .ForeignKey ;
51
53
import org .hibernate .annotations .Formula ;
52
54
import org .hibernate .annotations .GenericGenerator ;
104
106
import org .hibernate .cfg .annotations .QueryBinder ;
105
107
import org .hibernate .cfg .annotations .TableBinder ;
106
108
import org .hibernate .cfg .internal .NullableDiscriminatorColumnSecondPass ;
109
+ import org .hibernate .dialect .Dialect ;
107
110
import org .hibernate .engine .OptimisticLockStyle ;
108
111
import org .hibernate .engine .spi .FilterDefinition ;
109
112
import org .hibernate .id .IdentifierGenerator ;
@@ -632,15 +635,15 @@ else if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) ) {
632
635
633
636
entityBinder .setProxy ( clazzToProcess .getAnnotation ( Proxy .class ) );
634
637
entityBinder .setBatchSize ( clazzToProcess .getAnnotation ( BatchSize .class ) );
635
- entityBinder .setWhere ( clazzToProcess . getAnnotation ( Where .class ) );
638
+ entityBinder .setWhere ( getOverridableAnnotation ( clazzToProcess , Where .class , context ) );
636
639
applyCacheSettings ( entityBinder , clazzToProcess , context );
637
640
638
- bindFilters ( clazzToProcess , entityBinder , context );
641
+ bindFiltersAndFilterDefs ( clazzToProcess , entityBinder , context );
639
642
640
643
entityBinder .bindEntity ();
641
644
642
645
if ( inheritanceState .hasTable () ) {
643
- Check checkAnn = clazzToProcess . getAnnotation ( Check .class );
646
+ Check checkAnn = getOverridableAnnotation ( clazzToProcess , Check .class , context );
644
647
String constraints = checkAnn == null
645
648
? null
646
649
: checkAnn .constraints ();
@@ -842,6 +845,33 @@ else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
842
845
bindCallbacks ( clazzToProcess , persistentClass , context );
843
846
}
844
847
848
+ public static <T extends Annotation > T getOverridableAnnotation (
849
+ XAnnotatedElement element ,
850
+ Class <T > annotationType ,
851
+ MetadataBuildingContext context ) {
852
+ Dialect dialect = context .getMetadataCollector ().getDatabase ().getDialect ();
853
+ for ( Annotation annotation : element .getAnnotations () ) {
854
+ if (annotation instanceof ForDialect ) {
855
+ ForDialect forDialect = (ForDialect ) annotation ;
856
+ if ( forDialect .dialect ().isAssignableFrom ( dialect .getClass () ) ) {
857
+ for ( Method method : ForDialect .class .getDeclaredMethods () ) {
858
+ if ( method .getReturnType ().equals ( annotationType ) ) {
859
+ T result ;
860
+ try {
861
+ result = (T ) method .invoke ( forDialect );
862
+ if ( result .toString ().indexOf ('?' )>0 ) break ; //the "null" value
863
+ return result ;
864
+ } catch (Exception e ) {
865
+ break ;
866
+ }
867
+ }
868
+ }
869
+ }
870
+ }
871
+ }
872
+ return element .getAnnotation ( annotationType );
873
+ }
874
+
845
875
private static void handleTypeDescriptorRegistrations (XAnnotatedElement annotatedElement , MetadataBuildingContext context ) {
846
876
final ManagedBeanRegistry managedBeanRegistry = context .getBootstrapContext ()
847
877
.getServiceRegistry ()
@@ -958,9 +988,7 @@ private static AnnotatedDiscriminatorColumn processSingleTableDiscriminatorPrope
958
988
? discAnn .discriminatorType ()
959
989
: DiscriminatorType .STRING ;
960
990
961
- DiscriminatorFormula discFormulaAnn = clazzToProcess .getAnnotation (
962
- DiscriminatorFormula .class
963
- );
991
+ DiscriminatorFormula discFormulaAnn = getOverridableAnnotation ( clazzToProcess , DiscriminatorFormula .class , context );
964
992
if ( isRoot ) {
965
993
discriminatorColumn = AnnotatedDiscriminatorColumn .buildDiscriminatorColumn (
966
994
discriminatorType ,
@@ -1402,18 +1430,18 @@ private static boolean isEntityClassType(XClass clazzToProcess, AnnotatedClassTy
1402
1430
* on the MappedSuperclass(s) in the inheritance hierarchy
1403
1431
*/
1404
1432
1405
- private static void bindFilters (
1433
+ private static void bindFiltersAndFilterDefs (
1406
1434
XClass annotatedClass ,
1407
1435
EntityBinder entityBinder ,
1408
1436
MetadataBuildingContext context ) {
1409
1437
1410
- bindFilters ( annotatedClass , entityBinder );
1438
+ bindFilters ( annotatedClass , entityBinder , context );
1411
1439
1412
1440
XClass classToProcess = annotatedClass .getSuperclass ();
1413
1441
while ( classToProcess != null ) {
1414
1442
AnnotatedClassType classType = context .getMetadataCollector ().getClassType ( classToProcess );
1415
1443
if ( AnnotatedClassType .EMBEDDABLE_SUPERCLASS .equals ( classType ) ) {
1416
- bindFilters ( classToProcess , entityBinder );
1444
+ bindFilters ( classToProcess , entityBinder , context );
1417
1445
}
1418
1446
else {
1419
1447
break ;
@@ -1423,9 +1451,8 @@ private static void bindFilters(
1423
1451
1424
1452
}
1425
1453
1426
- private static void bindFilters (XAnnotatedElement annotatedElement , EntityBinder entityBinder ) {
1427
-
1428
- Filters filtersAnn = annotatedElement .getAnnotation ( Filters .class );
1454
+ private static void bindFilters (XAnnotatedElement annotatedElement , EntityBinder entityBinder , MetadataBuildingContext context ) {
1455
+ Filters filtersAnn = getOverridableAnnotation ( annotatedElement , Filters .class , context );
1429
1456
if ( filtersAnn != null ) {
1430
1457
for ( Filter filter : filtersAnn .value () ) {
1431
1458
entityBinder .addFilter (filter );
@@ -1440,7 +1467,7 @@ private static void bindFilters(XAnnotatedElement annotatedElement, EntityBinder
1440
1467
1441
1468
private static void bindFilterDefs (XAnnotatedElement annotatedElement , MetadataBuildingContext context ) {
1442
1469
FilterDef defAnn = annotatedElement .getAnnotation ( FilterDef .class );
1443
- FilterDefs defsAnn = annotatedElement . getAnnotation ( FilterDefs .class );
1470
+ FilterDefs defsAnn = getOverridableAnnotation ( annotatedElement , FilterDefs .class , context );
1444
1471
if ( defAnn != null ) {
1445
1472
bindFilterDef ( defAnn , context );
1446
1473
}
@@ -2038,7 +2065,7 @@ else if ( property.isAnnotationPresent( OneToMany.class )
2038
2065
collectionBinder .setBatchSize ( property .getAnnotation ( BatchSize .class ) );
2039
2066
2040
2067
collectionBinder .setJpaOrderBy ( property .getAnnotation ( jakarta .persistence .OrderBy .class ) );
2041
- collectionBinder .setSqlOrderBy ( property . getAnnotation ( OrderBy .class ) );
2068
+ collectionBinder .setSqlOrderBy ( getOverridableAnnotation ( property , OrderBy .class , context ) );
2042
2069
2043
2070
collectionBinder .setNaturalSort ( property .getAnnotation ( SortNatural .class ) );
2044
2071
collectionBinder .setComparatorSort ( property .getAnnotation ( SortComparator .class ) );
@@ -2064,7 +2091,7 @@ else if ( property.isAnnotationPresent( OneToMany.class )
2064
2091
Formula .class
2065
2092
) ) {
2066
2093
Column ann = property .getAnnotation ( Column .class );
2067
- Formula formulaAnn = property . getAnnotation ( Formula .class );
2094
+ Formula formulaAnn = getOverridableAnnotation ( property , Formula .class , context );
2068
2095
elementColumns = AnnotatedColumn .buildColumnFromAnnotation (
2069
2096
new Column [] { ann },
2070
2097
formulaAnn ,
0 commit comments