some misc cleanups to mapping package

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-11-04 17:01:58 +01:00
parent 4dcfdb57e5
commit c0a14334d1
8 changed files with 86 additions and 111 deletions

View File

@ -819,7 +819,7 @@ public final class StringHelper {
return names;
}
else {
String[] unquoted = new String[length];
final String[] unquoted = new String[length];
System.arraycopy( names, 0, unquoted, 0, failedIndex );
for ( int i = failedIndex; i < length; i++ ) {
unquoted[i] = unquote( names[i], dialect );
@ -889,8 +889,8 @@ public final class StringHelper {
* if both {@code firstExpression} and {@code secondExpression} are empty, then null is returned.
*/
public static String getNonEmptyOrConjunctionIfBothNonEmpty( String firstExpression, String secondExpression ) {
final boolean isFirstExpressionNonEmpty = StringHelper.isNotEmpty( firstExpression );
final boolean isSecondExpressionNonEmpty = StringHelper.isNotEmpty( secondExpression );
final boolean isFirstExpressionNonEmpty = isNotEmpty( firstExpression );
final boolean isSecondExpressionNonEmpty = isNotEmpty( secondExpression );
if ( isFirstExpressionNonEmpty && isSecondExpressionNonEmpty ) {
return "( " + firstExpression + " ) and ( " + secondExpression + " )";
}
@ -918,12 +918,7 @@ public final class StringHelper {
* @return The interned string.
*/
public static String safeInterning(final String string) {
if ( string == null ) {
return null;
}
else {
return string.intern();
}
return string == null ? null : string.intern();
}
}

View File

@ -17,6 +17,7 @@ import org.hibernate.TimeZoneStorageStrategy;
import org.hibernate.annotations.SoftDelete;
import org.hibernate.annotations.SoftDeleteType;
import org.hibernate.annotations.TimeZoneStorageType;
import org.hibernate.boot.internal.ClassmateContext;
import org.hibernate.boot.model.TypeDefinition;
import org.hibernate.boot.model.convert.internal.AutoApplicableConverterDescriptorBypassedImpl;
import org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor;
@ -33,6 +34,7 @@ import org.hibernate.boot.model.process.internal.VersionResolution;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.Size;
@ -172,10 +174,8 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
if ( resolution != null ) {
throw new IllegalStateException( "BasicValue already resolved" );
}
this.ownerName = className;
this.propertyName = propertyName;
super.setTypeUsingReflection( className, propertyName );
}
@ -197,7 +197,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
public void setJpaAttributeConverterDescriptor(ConverterDescriptor descriptor) {
setAttributeConverterDescriptor( descriptor );
super.setJpaAttributeConverterDescriptor( descriptor );
}
@ -249,8 +248,10 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
if ( temporalPrecision != null ) {
return temporalPrecision;
}
final Integer precision = column.getPrecision();
return precision == null ? NO_COLUMN_PRECISION : precision;
else {
final Integer precision = column.getPrecision();
return precision == null ? NO_COLUMN_PRECISION : precision;
}
}
else {
return NO_COLUMN_PRECISION;
@ -313,7 +314,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
@Override
public void addFormula(Formula formula) {
super.addFormula( formula );
checkSelectable( formula );
}
@ -324,7 +324,6 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
public Type getType() throws MappingException {
resolve();
assert getResolution() != null;
return getResolution().getLegacyResolvedBasicType();
}
@ -343,32 +342,30 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
if ( resolution != null ) {
return resolution;
}
resolution = buildResolution();
if ( resolution == null ) {
throw new IllegalStateException( "Unable to resolve BasicValue : " + this );
}
final Selectable selectable = getColumn();
final Size size;
if ( selectable instanceof Column column ) {
resolveColumn( column, getDialect() );
size = column.calculateColumnSize( getDialect(), getBuildingContext().getMetadataCollector() );
}
else {
size = Size.nil();
resolution = buildResolution();
if ( resolution == null ) {
throw new IllegalStateException( "Unable to resolve BasicValue : " + this );
}
else {
final Size size;
if ( getColumn() instanceof Column column ) {
resolveColumn( column, getDialect() );
size = column.calculateColumnSize( getDialect(), getMetadataCollector() );
}
else {
size = Size.nil();
}
resolution.getJdbcType().addAuxiliaryDatabaseObjects(
resolution.getRelationalJavaType(),
resolution.getValueConverter(),
size,
getMetadataCollector().getDatabase(),
this
);
return resolution;
}
}
resolution.getJdbcType().addAuxiliaryDatabaseObjects(
resolution.getRelationalJavaType(),
resolution.getValueConverter(),
size,
getBuildingContext().getMetadataCollector().getDatabase(),
this
);
return resolution;
}
@Override
@ -378,7 +375,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
resolution.getRelationalJavaType(),
getColumn().getText(),
getTable().getName(),
getBuildingContext().getMetadataCollector().getDatabase()
getMetadataCollector().getDatabase()
);
}
@ -483,26 +480,18 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
SoftDelete.UnspecifiedConversion.class.equals( attributeConverterDescriptor.getAttributeConverterClass() );
if ( conversionWasUnspecified ) {
final JdbcType jdbcType = BooleanJdbcType.INSTANCE.resolveIndicatedType( this, javaType);
final ClassmateContext classmateContext = getBuildingContext().getBootstrapContext().getClassmateContext();
if ( jdbcType.isNumber() ) {
return new InstanceBasedConverterDescriptor(
NumericBooleanConverter.INSTANCE,
getBuildingContext().getBootstrapContext().getClassmateContext()
);
return new InstanceBasedConverterDescriptor( NumericBooleanConverter.INSTANCE, classmateContext );
}
else if ( jdbcType.isString() ) {
// here we pick 'T' / 'F' storage, though 'Y' / 'N' is equally valid - its 50/50
return new InstanceBasedConverterDescriptor(
TrueFalseConverter.INSTANCE,
getBuildingContext().getBootstrapContext().getClassmateContext()
);
return new InstanceBasedConverterDescriptor( TrueFalseConverter.INSTANCE, classmateContext );
}
else {
// should indicate BIT or BOOLEAN == no conversion needed
// - we still create the converter to properly set up JDBC type, etc
return new InstanceBasedConverterDescriptor(
PassThruSoftDeleteConverter.INSTANCE,
getBuildingContext().getBootstrapContext().getClassmateContext()
);
return new InstanceBasedConverterDescriptor( PassThruSoftDeleteConverter.INSTANCE, classmateContext );
}
}
else {

View File

@ -68,17 +68,10 @@ public class FetchProfile {
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
FetchProfile that = ( FetchProfile ) o;
return name.equals( that.name );
public boolean equals(Object that) {
return this == that
|| that instanceof FetchProfile profile
&& name.equals( profile.name );
}
@Override

View File

@ -94,9 +94,9 @@ public class Formula implements Selectable, Serializable {
}
@Override
public boolean equals(Object obj) {
return obj instanceof Formula
&& ( (Formula) obj ).formula.equals( formula );
public boolean equals(Object that) {
return that instanceof Formula other
&& formula.equals( other.formula );
}
@Override

View File

@ -149,12 +149,15 @@ public abstract class SimpleValue implements KeyValue {
return metadata;
}
InFlightMetadataCollector getMetadataCollector() {
return getBuildingContext().getMetadataCollector();
}
@Override
public ServiceRegistry getServiceRegistry() {
return getMetadata().getMetadataBuildingOptions().getServiceRegistry();
}
public TypeConfiguration getTypeConfiguration() {
return getBuildingContext().getBootstrapContext().getTypeConfiguration();
}
@ -234,8 +237,8 @@ public abstract class SimpleValue implements KeyValue {
for ( int i = 0; i < originalOrder.length; i++ ) {
final int originalIndex = originalOrder[i];
final Selectable selectable = originalColumns[i];
if ( selectable instanceof Column ) {
( (Column) selectable ).setTypeIndex( originalIndex );
if ( selectable instanceof Column column ) {
column.setTypeIndex( originalIndex );
}
columns.set( originalIndex, selectable );
insertability.set( originalIndex, originalInsertability[i] );
@ -422,9 +425,8 @@ public abstract class SimpleValue implements KeyValue {
if ( getColumnSpan() != 1 ) {
throw new MappingException( "Identity generation requires exactly one column" );
}
final Selectable column = getColumn(0);
if ( column instanceof Column ) {
( (Column) column).setIdentity( true );
else if ( getColumn(0) instanceof Column column ) {
column.setIdentity( true );
}
else {
throw new MappingException( "Identity generation requires a column" );
@ -513,10 +515,12 @@ public abstract class SimpleValue implements KeyValue {
// considered nullable
return true;
}
else if ( !( (Column) selectable ).isNullable() ) {
// if there is a single non-nullable column, the Value
// overall is considered non-nullable.
return false;
else if ( selectable instanceof Column column ) {
if ( !column.isNullable() ) {
// if there is a single non-nullable column, the Value
// overall is considered non-nullable.
return false;
}
}
}
// nullable by default
@ -755,7 +759,7 @@ public abstract class SimpleValue implements KeyValue {
public void setTypeParameters(Map<String, ?> parameters) {
if ( parameters != null ) {
Properties properties = new Properties();
final Properties properties = new Properties();
properties.putAll( parameters );
setTypeParameters( properties );
}
@ -891,10 +895,9 @@ public abstract class SimpleValue implements KeyValue {
// todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = getAnnotations( attributeMember );
final ClassLoaderService classLoaderService = getMetadata()
.getMetadataBuildingOptions()
.getServiceRegistry()
.requireService( ClassLoaderService.class );
final ClassLoaderService classLoaderService =
getMetadata().getMetadataBuildingOptions().getServiceRegistry()
.requireService( ClassLoaderService.class );
typeParameters.put(
DynamicParameterizedType.PARAMETER_TYPE,
new ParameterTypeImpl(
@ -906,7 +909,7 @@ public abstract class SimpleValue implements KeyValue {
table.getCatalog(),
table.getSchema(),
table.getName(),
parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)),
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
columnNames,
columnLengths
)
@ -944,19 +947,18 @@ public abstract class SimpleValue implements KeyValue {
// todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = getAnnotations( attributeMember );
final ClassLoaderService classLoaderService = getMetadata()
.getMetadataBuildingOptions()
.getServiceRegistry()
.requireService( ClassLoaderService.class );
final ClassLoaderService classLoaderService =
getMetadata().getMetadataBuildingOptions().getServiceRegistry()
.requireService( ClassLoaderService.class );
return new ParameterTypeImpl(
classLoaderService.classForTypeName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)),
classLoaderService.classForTypeName( typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS) ),
attributeMember != null ? attributeMember.getType() : null,
annotations,
table.getCatalog(),
table.getSchema(),
table.getName(),
parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)),
parseBoolean( typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY) ),
columnNames,
columnLengths
);

View File

@ -416,7 +416,8 @@ public class Table implements Serializable, ContributableDatabaseObject {
@Override
public boolean equals(Object object) {
return object instanceof Table && equals((Table) object);
return object instanceof Table table
&& equals( table );
}
public boolean equals(Table table) {

View File

@ -90,9 +90,9 @@ public abstract class ToOne extends SimpleValue implements Fetchable, SortableVa
@Override
public void setTypeUsingReflection(String className, String propertyName) throws MappingException {
if ( referencedEntityName == null ) {
final ClassLoaderService cls = getMetadata().getMetadataBuildingOptions()
.getServiceRegistry()
.requireService( ClassLoaderService.class );
final ClassLoaderService cls =
getMetadata().getMetadataBuildingOptions().getServiceRegistry()
.requireService( ClassLoaderService.class );
referencedEntityName = ReflectHelper.reflectedPropertyClass( className, propertyName, cls ).getName();
}
}

View File

@ -91,13 +91,10 @@ public interface Value extends Serializable {
final Type[] subtypes = compositeType.getSubtypes();
for ( int i = 0; i < subtypes.length; i++ ) {
final Type subtype = subtypes[i];
final int columnSpan;
if ( subtype instanceof EntityType entityType ) {
columnSpan = getIdType( entityType ).getColumnSpan( factory );
}
else {
columnSpan = subtype.getColumnSpan( factory );
}
final int columnSpan =
subtype instanceof EntityType entityType
? getIdType( entityType ).getColumnSpan( factory )
: subtype.getColumnSpan( factory );
if ( columnSpan < index ) {
index -= columnSpan;
}
@ -114,20 +111,18 @@ public interface Value extends Serializable {
else if ( elementType instanceof MetaType metaType ) {
return (JdbcMapping) metaType.getBaseType();
}
return (JdbcMapping) elementType;
else {
return (JdbcMapping) elementType;
}
}
private Type getIdType(EntityType entityType) {
final PersistentClass entityBinding = getBuildingContext().getMetadataCollector()
.getEntityBinding( entityType.getAssociatedEntityName() );
final Type idType;
if ( entityType.isReferenceToPrimaryKey() ) {
idType = entityBinding.getIdentifier().getType();
}
else {
idType = entityBinding.getProperty( entityType.getRHSUniqueKeyPropertyName() ).getType();
}
return idType;
final PersistentClass entityBinding =
getBuildingContext().getMetadataCollector()
.getEntityBinding( entityType.getAssociatedEntityName() );
return entityType.isReferenceToPrimaryKey()
? entityBinding.getIdentifier().getType()
: entityBinding.getProperty( entityType.getRHSUniqueKeyPropertyName() ).getType();
}
FetchMode getFetchMode();