6
6
7
7
import java .util .Collections ;
8
8
9
+ import org .hibernate .AssertionFailure ;
9
10
import org .hibernate .MappingException ;
10
11
import org .hibernate .annotations .TenantId ;
11
12
import org .hibernate .binder .AttributeBinder ;
17
18
import org .hibernate .mapping .PersistentClass ;
18
19
import org .hibernate .mapping .Property ;
19
20
import org .hibernate .mapping .Selectable ;
21
+ import org .hibernate .metamodel .mapping .JdbcMapping ;
20
22
import org .hibernate .type .BasicType ;
21
23
import org .hibernate .type .descriptor .java .JavaType ;
22
- import org .hibernate .type .spi .TypeConfiguration ;
23
24
24
25
import static java .util .Collections .emptyMap ;
25
26
import static java .util .Collections .singletonMap ;
@@ -41,12 +42,11 @@ public void bind(
41
42
PersistentClass persistentClass ,
42
43
Property property ) {
43
44
final InFlightMetadataCollector collector = buildingContext .getMetadataCollector ();
44
- final TypeConfiguration typeConfiguration = collector .getTypeConfiguration ();
45
45
46
46
final String returnedClassName = property .getReturnedClassName ();
47
- final BasicType <Object > tenantIdType = typeConfiguration
48
- .getBasicTypeRegistry ()
49
- .getRegisteredType ( returnedClassName );
47
+ final BasicType <Object > tenantIdType =
48
+ collector . getTypeConfiguration () .getBasicTypeRegistry ()
49
+ .getRegisteredType ( returnedClassName );
50
50
51
51
final FilterDefinition filterDefinition = collector .getFilterDefinition ( FILTER_NAME );
52
52
if ( filterDefinition == null ) {
@@ -63,13 +63,13 @@ public void bind(
63
63
}
64
64
else {
65
65
final JavaType <?> tenantIdTypeJtd = tenantIdType .getJavaTypeDescriptor ();
66
- final JavaType <?> parameterJtd = filterDefinition
67
- . getParameterJdbcMapping ( PARAMETER_NAME )
68
- .getJavaTypeDescriptor ();
69
- if ( !parameterJtd .getJavaTypeClass ().equals ( tenantIdTypeJtd .getJavaTypeClass () ) ) {
66
+ final JdbcMapping jdbcMapping = filterDefinition . getParameterJdbcMapping ( PARAMETER_NAME );
67
+ assert jdbcMapping != null ;
68
+ final JavaType <?> parameterJavaType = jdbcMapping .getJavaTypeDescriptor ();
69
+ if ( !parameterJavaType .getJavaTypeClass ().equals ( tenantIdTypeJtd .getJavaTypeClass () ) ) {
70
70
throw new MappingException (
71
71
"all @TenantId fields must have the same type: "
72
- + parameterJtd .getTypeName ()
72
+ + parameterJavaType .getTypeName ()
73
73
+ " differs from "
74
74
+ tenantIdTypeJtd .getTypeName ()
75
75
);
@@ -93,10 +93,16 @@ private String columnNameOrFormula(Property property) {
93
93
if ( property .getColumnSpan () != 1 ) {
94
94
throw new MappingException ( "@TenantId attribute must be mapped to a single column or formula" );
95
95
}
96
- Selectable selectable = property .getSelectables ().get ( 0 );
97
- return selectable .isFormula ()
98
- ? ( (Formula ) selectable ).getFormula ()
99
- : ( (Column ) selectable ).getName ();
96
+ final Selectable selectable = property .getSelectables ().get ( 0 );
97
+ if ( selectable instanceof Formula formula ) {
98
+ return formula .getFormula ();
99
+ }
100
+ else if ( selectable instanceof Column column ) {
101
+ return column .getName ();
102
+ }
103
+ else {
104
+ throw new AssertionFailure ( "@TenantId attribute must be mapped to a column or formula" );
105
+ }
100
106
}
101
107
102
108
}
0 commit comments