-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- version - tenancy
- Loading branch information
Showing
6 changed files
with
316 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/org/hibernate/boot/models/bind/internal/binders/TenantIdBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.boot.models.bind.internal.binders; | ||
|
||
import org.hibernate.MappingException; | ||
import org.hibernate.boot.models.bind.spi.BindingContext; | ||
import org.hibernate.boot.models.bind.spi.BindingOptions; | ||
import org.hibernate.boot.models.bind.spi.BindingState; | ||
import org.hibernate.boot.models.categorize.spi.AttributeMetadata; | ||
import org.hibernate.boot.models.categorize.spi.EntityTypeMetadata; | ||
import org.hibernate.boot.spi.InFlightMetadataCollector; | ||
import org.hibernate.engine.spi.FilterDefinition; | ||
import org.hibernate.mapping.BasicValue; | ||
import org.hibernate.mapping.Property; | ||
import org.hibernate.mapping.RootClass; | ||
import org.hibernate.models.spi.MemberDetails; | ||
import org.hibernate.type.BasicType; | ||
import org.hibernate.type.descriptor.java.JavaType; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
import static java.util.Collections.singletonMap; | ||
import static org.hibernate.boot.models.bind.internal.binders.AttributeBinder.bindConversion; | ||
import static org.hibernate.boot.models.bind.internal.binders.AttributeBinder.bindImplicitJavaType; | ||
import static org.hibernate.boot.models.bind.internal.binders.AttributeBinder.processColumn; | ||
import static org.hibernate.boot.models.bind.internal.binders.BasicValueBinder.bindEnumerated; | ||
import static org.hibernate.boot.models.bind.internal.binders.BasicValueBinder.bindJavaType; | ||
import static org.hibernate.boot.models.bind.internal.binders.BasicValueBinder.bindJdbcType; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
public class TenantIdBinder { | ||
public static final String FILTER_NAME = "_tenantId"; | ||
public static final String PARAMETER_NAME = "tenantId"; | ||
|
||
public static void bindTenantId( | ||
AttributeMetadata attributeMetadata, | ||
EntityTypeMetadata managedType, | ||
RootClass typeBinding, | ||
BindingOptions bindingOptions, | ||
BindingState bindingState, | ||
BindingContext bindingContext) { | ||
final InFlightMetadataCollector collector = bindingState.getMetadataBuildingContext().getMetadataCollector(); | ||
final TypeConfiguration typeConfiguration = collector.getTypeConfiguration(); | ||
|
||
final MemberDetails memberDetails = attributeMetadata.getMember(); | ||
final String returnedClassName = memberDetails.getType().getClassName(); | ||
final BasicType<Object> tenantIdType = typeConfiguration | ||
.getBasicTypeRegistry() | ||
.getRegisteredType( returnedClassName ); | ||
|
||
final FilterDefinition filterDefinition = collector.getFilterDefinition( FILTER_NAME ); | ||
if ( filterDefinition == null ) { | ||
collector.addFilterDefinition( new FilterDefinition( | ||
FILTER_NAME, | ||
"", | ||
singletonMap( PARAMETER_NAME, tenantIdType ) | ||
) ); | ||
} | ||
else { | ||
final JavaType<?> tenantIdTypeJtd = tenantIdType.getJavaTypeDescriptor(); | ||
final JavaType<?> parameterJtd = filterDefinition | ||
.getParameterJdbcMapping( PARAMETER_NAME ) | ||
.getJavaTypeDescriptor(); | ||
if ( !parameterJtd.getJavaTypeClass().equals( tenantIdTypeJtd.getJavaTypeClass() ) ) { | ||
throw new MappingException( | ||
"all @TenantId fields must have the same type: " | ||
+ parameterJtd.getJavaType().getTypeName() | ||
+ " differs from " | ||
+ tenantIdTypeJtd.getJavaType().getTypeName() | ||
); | ||
} | ||
} | ||
|
||
final Property property = new Property(); | ||
typeBinding.addProperty( property ); | ||
property.setName( attributeMetadata.getName() ); | ||
|
||
final BasicValue basicValue = new BasicValue( bindingState.getMetadataBuildingContext(), typeBinding.getRootTable() ); | ||
property.setValue( basicValue ); | ||
|
||
bindImplicitJavaType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
bindJavaType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
bindJdbcType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
|
||
bindConversion( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
bindEnumerated( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
|
||
processColumn( | ||
memberDetails, | ||
property, | ||
basicValue, | ||
typeBinding.getRootTable(), | ||
bindingOptions, | ||
bindingState, | ||
bindingContext | ||
); | ||
|
||
property.resetUpdateable( false ); | ||
property.resetOptional( false ); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/hibernate/boot/models/bind/internal/binders/VersionBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.boot.models.bind.internal.binders; | ||
|
||
import org.hibernate.boot.models.bind.spi.BindingContext; | ||
import org.hibernate.boot.models.bind.spi.BindingOptions; | ||
import org.hibernate.boot.models.bind.spi.BindingState; | ||
import org.hibernate.boot.models.categorize.spi.AttributeMetadata; | ||
import org.hibernate.boot.models.categorize.spi.EntityTypeMetadata; | ||
import org.hibernate.mapping.BasicValue; | ||
import org.hibernate.mapping.Property; | ||
import org.hibernate.mapping.RootClass; | ||
import org.hibernate.models.spi.MemberDetails; | ||
|
||
import static org.hibernate.boot.models.bind.internal.binders.AttributeBinder.bindImplicitJavaType; | ||
import static org.hibernate.boot.models.bind.internal.binders.AttributeBinder.processColumn; | ||
import static org.hibernate.boot.models.bind.internal.binders.BasicValueBinder.bindJavaType; | ||
import static org.hibernate.boot.models.bind.internal.binders.BasicValueBinder.bindJdbcType; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
public class VersionBinder { | ||
public static void bindVersion( | ||
AttributeMetadata attributeMetadata, | ||
EntityTypeMetadata managedType, | ||
RootClass typeBinding, | ||
BindingOptions bindingOptions, | ||
BindingState bindingState, | ||
BindingContext bindingContext) { | ||
final Property property = new Property(); | ||
property.setName( attributeMetadata.getName() ); | ||
typeBinding.setVersion( property ); | ||
|
||
final BasicValue basicValue = new BasicValue( | ||
bindingState.getMetadataBuildingContext(), | ||
typeBinding.getRootTable() | ||
); | ||
property.setValue( basicValue ); | ||
|
||
final MemberDetails memberDetails = attributeMetadata.getMember(); | ||
bindImplicitJavaType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
bindJavaType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
bindJdbcType( memberDetails, property, basicValue, bindingOptions, bindingState, bindingContext ); | ||
|
||
final org.hibernate.mapping.Column column = processColumn( | ||
memberDetails, | ||
property, | ||
basicValue, | ||
typeBinding.getRootTable(), | ||
bindingOptions, | ||
bindingState, | ||
bindingContext | ||
); | ||
// force it to be non-nullable | ||
column.setNullable( false ); | ||
} | ||
} |
Oops, something went wrong.