Get rid of Type#dictatedSizes and Type#defaultSizes and move methods from Sized as default methods to UserType

This commit is contained in:
Christian Beikov 2022-01-20 10:53:47 +01:00
parent 5fcacce3b1
commit ffd72a7d60
15 changed files with 18 additions and 271 deletions

View File

@ -15,7 +15,6 @@ import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -193,16 +192,6 @@ public class DiscriminatorType<T> extends AbstractType implements BasicType<T>,
return underlyingType.getSqlTypeCodes( mapping );
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return underlyingType.dictatedSizes( mapping );
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return underlyingType.defaultSizes( mapping );
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return underlyingType.getColumnSpan( mapping );

View File

@ -40,9 +40,6 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
public abstract class AbstractStandardBasicType<T>
implements BasicType<T>, ProcedureParameterExtractionAware<T>, ProcedureParameterNamedBinder<T> {
private static final Size DEFAULT_SIZE = new Size( 19, 2, 255, Size.LobMultiplier.NONE ); // to match legacy behavior
private final Size dictatedSize = new Size();
private final JdbcType jdbcType;
private final JavaType<T> javaType;
private final int[] sqlTypes;
@ -97,14 +94,6 @@ public abstract class AbstractStandardBasicType<T>
return false;
}
protected static Size getDefaultSize() {
return DEFAULT_SIZE;
}
protected Size getDictatedSize() {
return dictatedSize;
}
// final implementations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public final JavaType<T> getJavaTypeDescriptor() {
@ -130,16 +119,6 @@ public abstract class AbstractStandardBasicType<T>
return sqlTypes;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] { getDictatedSize() };
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] { getDefaultSize() };
}
@Override
public final boolean isAssociationType() {
return false;

View File

@ -24,7 +24,6 @@ import org.hibernate.PropertyNotFoundException;
import org.hibernate.TransientObjectException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadeStyles;
import org.hibernate.engine.spi.Mapping;
@ -83,16 +82,6 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT
return ArrayHelper.join( discriminatorType.getSqlTypeCodes( mapping ), identifierType.getSqlTypeCodes( mapping ) );
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return ArrayHelper.join( discriminatorType.dictatedSizes( mapping ), identifierType.dictatedSizes( mapping ) );
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return ArrayHelper.join( discriminatorType.defaultSizes( mapping ), identifierType.defaultSizes( mapping ) );
}
@Override
public Object[] getPropertyValues(Object component) {
throw new UnsupportedOperationException();

View File

@ -25,7 +25,6 @@ import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.collection.spi.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.CollectionKey;
import org.hibernate.engine.spi.EntityEntry;
@ -158,16 +157,6 @@ public abstract class CollectionType extends AbstractType implements Association
return ArrayHelper.EMPTY_INT_ARRAY;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] { LEGACY_DICTATED_SIZE };
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] { LEGACY_DEFAULT_SIZE };
}
@Override
public int getColumnSpan(Mapping session) throws MappingException {
return 0;

View File

@ -24,7 +24,6 @@ import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -135,32 +134,6 @@ public class ComponentType extends AbstractType implements CompositeTypeImplemen
return sqlTypes;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
//Not called at runtime so doesn't matter if it's slow :)
final Size[] sizes = new Size[getColumnSpan( mapping )];
int soFar = 0;
for ( Type propertyType : propertyTypes ) {
final Size[] propertySizes = propertyType.dictatedSizes( mapping );
System.arraycopy( propertySizes, 0, sizes, soFar, propertySizes.length );
soFar += propertySizes.length;
}
return sizes;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
//Not called at runtime so doesn't matter if it's slow :)
final Size[] sizes = new Size[getColumnSpan( mapping )];
int soFar = 0;
for ( Type propertyType : propertyTypes ) {
final Size[] propertySizes = propertyType.defaultSizes( mapping );
System.arraycopy( propertySizes, 0, sizes, soFar, propertySizes.length );
soFar += propertySizes.length;
}
return sizes;
}
@Override
public final boolean isComponentType() {

View File

@ -32,7 +32,6 @@ import org.hibernate.type.internal.UserTypeSqlTypeAdapter;
import org.hibernate.type.internal.UserTypeVersionJavaTypeWrapper;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.Sized;
import org.hibernate.usertype.UserType;
import org.hibernate.usertype.UserVersionType;
@ -65,9 +64,6 @@ public class CustomType<J>
private final ValueExtractor<J> valueExtractor;
private final ValueBinder<J> valueBinder;
private final Size dictatedSize;
private final Size defaultSize;
public CustomType(UserType<J> userType, TypeConfiguration typeConfiguration) throws MappingException {
this( userType, ArrayHelper.EMPTY_STRING_ARRAY, typeConfiguration );
}
@ -96,17 +92,6 @@ public class CustomType<J>
this.valueExtractor = jdbcType.getExtractor( mappedJavaType );
this.valueBinder = jdbcType.getBinder( mappedJavaType );
if ( userType instanceof Sized ) {
final Sized sized = (Sized) userType;
this.dictatedSize = sized.dictatedSizes()[0];
this.defaultSize = sized.defaultSizes()[0];
}
else {
this.dictatedSize = null;
this.defaultSize = null;
}
this.registrationKeys = registrationKeys;
}
@ -139,16 +124,6 @@ public class CustomType<J>
return registrationKeys;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] {dictatedSize};
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] {defaultSize};
}
@Override
public int getColumnSpan(Mapping session) {
return 1;
@ -169,22 +144,6 @@ public class CustomType<J>
return getUserType().hashCode( x);
}
private Object nullSafeGet(
ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner) throws SQLException {
throw new UnsupportedOperationException( "Reading from ResultSet by name is no longer supported" );
}
private Object nullSafeGet(
ResultSet rs,
String columnName,
SharedSessionContractImplementor session,
Object owner) throws SQLException {
throw new UnsupportedOperationException( "Reading from ResultSet by name is no longer supported" );
}
@Override
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) {
return getUserType().assemble( cached, owner);

View File

@ -13,7 +13,6 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.*;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.spi.TypeConfiguration;
@ -109,16 +108,6 @@ public class ManyToOneType extends EntityType {
return requireIdentifierOrUniqueKeyType( mapping ).getSqlTypeCodes( mapping );
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return requireIdentifierOrUniqueKeyType( mapping ).dictatedSizes( mapping );
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return requireIdentifierOrUniqueKeyType( mapping ).defaultSizes( mapping );
}
@Override
public ForeignKeyDirection getForeignKeyDirection() {
return ForeignKeyDirection.FROM_PARENT;

View File

@ -7,14 +7,12 @@
package org.hibernate.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -55,16 +53,6 @@ public class MetaType extends AbstractType {
return baseType.getSqlTypeCodes(mapping);
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return baseType.dictatedSizes( mapping );
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return baseType.defaultSizes( mapping );
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return baseType.getColumnSpan(mapping);

View File

@ -88,16 +88,6 @@ public class OneToOneType extends EntityType {
private static final Size[] SIZES = new Size[0];
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return SIZES;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return SIZES;
}
@Override
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return ArrayHelper.EMPTY_BOOLEAN_ARRAY;

View File

@ -172,16 +172,6 @@ public class SerializableToBlobType<T extends Serializable> implements BasicType
return sqlTypes;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] { getDictatedSize() };
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] { DEFAULT_SIZE };
}
@Override
public final boolean isAssociationType() {
return false;

View File

@ -13,7 +13,6 @@ import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.spi.TypeConfiguration;
@ -62,16 +61,6 @@ public class SpecialOneToOneType extends OneToOneType {
return super.getIdentifierOrUniqueKeyType( mapping ).getSqlTypeCodes( mapping );
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return super.getIdentifierOrUniqueKeyType( mapping ).dictatedSizes( mapping );
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return super.getIdentifierOrUniqueKeyType( mapping ).defaultSizes( mapping );
}
public boolean useLHSPrimaryKey() {
return false;
}

View File

@ -110,36 +110,6 @@ public interface Type extends Serializable {
*/
int[] getSqlTypeCodes(Mapping mapping) throws MappingException;
/**
* Return the column sizes dictated by this type. For example, the mapping for a {@code char}/{@link Character} would
* have a dictated length limit of 1; for a string-based {@link java.util.UUID} would have a size limit of 36; etc.
* <p/>
* NOTE: The number of elements in this array matches the return from {@link #getColumnSpan}.
*
* @param mapping The mapping object :/
* @todo Would be much much better to have this aware of Dialect once the service/metamodel split is done
*
* @return The dictated sizes.
*
* @throws MappingException Generally indicates an issue accessing the passed mapping object.
*/
Size[] dictatedSizes(Mapping mapping) throws MappingException;
/**
* Defines the column sizes to use according to this type if the user did not explicitly say (and if no
* {@link #dictatedSizes} were given).
* <p/>
* NOTE: The number of elements in this array matches the return from {@link #getColumnSpan}.
*
* @param mapping The mapping object :/
* @todo Would be much much better to have this aware of Dialect once the service/metamodel split is done
*
* @return The default sizes.
*
* @throws MappingException Generally indicates an issue accessing the passed mapping object.
*/
Size[] defaultSizes(Mapping mapping) throws MappingException;
/**
* The class returned by {@link #nullSafeGet} methods. This is used to establish the class of an array of
* this type.

View File

@ -20,7 +20,6 @@ import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.MutabilityPlanExposer;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.usertype.Sized;
import org.hibernate.usertype.UserType;
/**
@ -32,7 +31,6 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
private final MutabilityPlan<J> mutabilityPlan;
private final Comparator<J> comparator;
private final Sized sized;
public UserTypeJavaTypeWrapper(UserType<J> userType) {
this.userType = userType;
@ -66,13 +64,6 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
else {
this.comparator = this::compare;
}
if ( userType instanceof Sized ) {
this.sized = (Sized) userType;
}
else {
this.sized = null;
}
}
private int compare(J first, J second) {
@ -94,29 +85,17 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
@Override
public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) {
if ( sized != null ) {
return sized.defaultSizes()[0].getLength();
}
return Size.DEFAULT_LENGTH;
return userType.getDefaultSqlLength( dialect, jdbcType );
}
@Override
public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
if ( sized != null ) {
return sized.defaultSizes()[0].getPrecision();
}
return Size.DEFAULT_PRECISION;
return userType.getDefaultSqlPrecision( dialect, jdbcType );
}
@Override
public int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) {
if ( sized != null ) {
return sized.defaultSizes()[0].getScale();
}
return Size.DEFAULT_SCALE;
return userType.getDefaultSqlScale( dialect, jdbcType );
}
@Override

View File

@ -1,41 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.usertype;
import org.hibernate.engine.jdbc.Size;
/**
* Extends dictated/default column size declarations from {@link org.hibernate.type.Type} to the {@link UserType}
* hierarchy as well via an optional interface.
*
* @author Steve Ebersole
*/
public interface Sized {
/**
* Return the column sizes dictated by this type. For example, the mapping for a {@code char}/{@link Character} would
* have a dictated length limit of 1; for a string-based {@link java.util.UUID} would have a size limit of 36; etc.
*
* @todo Would be much much better to have this aware of Dialect once the service/metamodel split is done
*
* @return The dictated sizes.
*
* @see org.hibernate.type.Type#dictatedSizes
*/
public Size[] dictatedSizes();
/**
* Defines the column sizes to use according to this type if the user did not explicitly say (and if no
* {@link #dictatedSizes} were given).
*
* @todo Would be much much better to have this aware of Dialect once the service/metamodel split is done
*
* @return The default sizes.
*
* @see org.hibernate.type.Type#defaultSizes
*/
public Size[] defaultSizes();
}

View File

@ -11,7 +11,10 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.jdbc.JdbcType;
/**
* This interface should be implemented by user-defined "types".
@ -140,4 +143,16 @@ public interface UserType<J> {
* @return the value to be merged
*/
Object replace(Object detached, Object managed, Object owner);
default long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) {
return Size.DEFAULT_LENGTH;
}
default int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
return Size.DEFAULT_PRECISION;
}
default int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) {
return Size.DEFAULT_SCALE;
}
}