Skip to content

Commit

Permalink
#63 - Flesh out BindingCoordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 4, 2023
1 parent b91f065 commit f49f60f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import org.hibernate.annotations.Mutability;
import org.hibernate.annotations.Nationalized;
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.annotations.TimeZoneColumn;
import org.hibernate.annotations.TimeZoneStorage;
import org.hibernate.annotations.TimeZoneStorageType;
import org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Property;
import org.hibernate.models.ModelsException;
Expand All @@ -41,8 +45,12 @@
import jakarta.persistence.Convert;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Lob;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;

import static jakarta.persistence.EnumType.ORDINAL;
import static org.hibernate.annotations.TimeZoneStorageType.AUTO;
import static org.hibernate.annotations.TimeZoneStorageType.COLUMN;
import static org.hibernate.models.orm.categorize.spi.AttributeMetadata.AttributeNature.BASIC;

/**
Expand Down Expand Up @@ -298,12 +306,61 @@ public boolean processValue() {
processNationalized( member, basicValue );
processEnumerated( member, basicValue );
processConversion( member, basicValue, bindingContext );
processImplicitJavaType( member, basicValue );
processJavaType( member, basicValue );
processJdbcType( member, basicValue );
processMutability( member, property, basicValue );
processOptimisticLocking( member, property, basicValue );
processTemporalPrecision( member, basicValue );
processTimeZoneStorage( member, property, basicValue );

return true;
}

private void processImplicitJavaType(MemberDetails member, BasicValue basicValue) {
basicValue.setImplicitJavaTypeAccess( (typeConfiguration) -> member.getType().toJavaClass() );
}

private void processTemporalPrecision(MemberDetails member, BasicValue basicValue) {
final AnnotationUsage<Temporal> temporalAnn = member.getAnnotationUsage( Temporal.class );
if ( temporalAnn == null ) {
return;
}

//noinspection deprecation
final TemporalType precision = temporalAnn.getEnum( "value" );
basicValue.setTemporalPrecision( precision );
}

private void processTimeZoneStorage(MemberDetails member, Property property, BasicValue basicValue) {
final AnnotationUsage<TimeZoneStorage> storageAnn = member.getAnnotationUsage( TimeZoneStorage.class );
final AnnotationUsage<TimeZoneColumn> columnAnn = member.getAnnotationUsage( TimeZoneColumn.class );
if ( storageAnn != null ) {
final TimeZoneStorageType strategy = storageAnn.getEnum( "value", AUTO );
if ( strategy != COLUMN && columnAnn != null ) {
throw new AnnotationPlacementException(
"Illegal combination of @TimeZoneStorage(" + strategy.name() + ") and @TimeZoneColumn"
);
}
basicValue.setTimeZoneStorageType( strategy );
}

if ( columnAnn != null ) {
final org.hibernate.mapping.Column column = (org.hibernate.mapping.Column) basicValue.getColumn();
column.setName( columnAnn.getString( "name", property.getName() + "_tz" ) );
column.setSqlType( columnAnn.getString( "columnDefinition", null ) );

final var tableName = columnAnn.getString( "table", null );
TableReference tableByName = null;
if ( tableName != null ) {
final Identifier identifier = Identifier.toIdentifier( tableName );
tableByName = bindingState.getTableByName( identifier.getCanonicalName() );
basicValue.setTable( tableByName.getBinding() );
}

property.setInsertable( columnAnn.getBoolean( "insertable", true ) );
property.setUpdateable( columnAnn.getBoolean( "updatable", true ) );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,21 @@ void testIt(ServiceRegistryScope scope) {
final RootClass entityBinding = (RootClass) context.getMetadataCollector().getEntityBinding( SimpleEntity.class.getName() );
final Property id = entityBinding.getProperty( "id" );
assertThat( id.getValue().getTable().getName() ).isEqualTo( "SIMPLETONS" );
assertThat( ( (Column) ( (BasicValue) id.getValue() ).getColumn() ).getCanonicalName() ).isEqualTo( "id" );
final BasicValue idValue = (BasicValue) id.getValue();
assertThat( ( (Column) (idValue).getColumn() ).getCanonicalName() ).isEqualTo( "id" );
assertThat( idValue.resolve().getDomainJavaType().getJavaType() ).isEqualTo( Integer.class );

final Property name = entityBinding.getProperty( "name" );
assertThat( id.getValue().getTable().getName() ).isEqualTo( "SIMPLETONS" );
assertThat( ( (Column) ( (BasicValue) name.getValue() ).getColumn() ).getCanonicalName() ).isEqualTo( "name" );
final BasicValue nameValue = (BasicValue) name.getValue();
assertThat( ( (Column) (nameValue).getColumn() ).getCanonicalName() ).isEqualTo( "name" );
assertThat( nameValue.resolve().getDomainJavaType().getJavaType() ).isEqualTo( String.class );

final Property data = entityBinding.getProperty( "data" );
assertThat( data.getValue().getTable().getName() ).isEqualTo( "SIMPLE_STUFF" );
assertThat( ( (Column) ( (BasicValue) data.getValue() ).getColumn() ).getCanonicalName() ).isEqualTo( "datum" );
final BasicValue dataValue = (BasicValue) data.getValue();
assertThat( ( (Column) (dataValue).getColumn() ).getCanonicalName() ).isEqualTo( "datum" );
assertThat( dataValue.resolve().getDomainJavaType().getJavaType() ).isEqualTo( String.class );
},
scope.getRegistry(),
SimpleEntity.class
Expand Down

0 comments on commit f49f60f

Please sign in to comment.