Skip to content

Commit fcef126

Browse files
committed
minor cleanups to TenantIdBinder
1 parent 63af9cd commit fcef126

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Collections;
88

9+
import org.hibernate.AssertionFailure;
910
import org.hibernate.MappingException;
1011
import org.hibernate.annotations.TenantId;
1112
import org.hibernate.binder.AttributeBinder;
@@ -17,9 +18,9 @@
1718
import org.hibernate.mapping.PersistentClass;
1819
import org.hibernate.mapping.Property;
1920
import org.hibernate.mapping.Selectable;
21+
import org.hibernate.metamodel.mapping.JdbcMapping;
2022
import org.hibernate.type.BasicType;
2123
import org.hibernate.type.descriptor.java.JavaType;
22-
import org.hibernate.type.spi.TypeConfiguration;
2324

2425
import static java.util.Collections.emptyMap;
2526
import static java.util.Collections.singletonMap;
@@ -41,12 +42,11 @@ public void bind(
4142
PersistentClass persistentClass,
4243
Property property) {
4344
final InFlightMetadataCollector collector = buildingContext.getMetadataCollector();
44-
final TypeConfiguration typeConfiguration = collector.getTypeConfiguration();
4545

4646
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 );
5050

5151
final FilterDefinition filterDefinition = collector.getFilterDefinition( FILTER_NAME );
5252
if ( filterDefinition == null ) {
@@ -63,13 +63,13 @@ public void bind(
6363
}
6464
else {
6565
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() ) ) {
7070
throw new MappingException(
7171
"all @TenantId fields must have the same type: "
72-
+ parameterJtd.getTypeName()
72+
+ parameterJavaType.getTypeName()
7373
+ " differs from "
7474
+ tenantIdTypeJtd.getTypeName()
7575
);
@@ -93,10 +93,16 @@ private String columnNameOrFormula(Property property) {
9393
if ( property.getColumnSpan() != 1 ) {
9494
throw new MappingException( "@TenantId attribute must be mapped to a single column or formula" );
9595
}
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+
}
100106
}
101107

102108
}

0 commit comments

Comments
 (0)