From 992b390fce6ee531f3b0300b865f70c8503892d1 Mon Sep 17 00:00:00 2001 From: gavinking Date: Mon, 27 Jan 2020 20:10:57 +0100 Subject: [PATCH] Get type-specific column length/precision/scale defaulting working again And fix resulting breakage to envers --- .../internal/hbm/RelationalObjectBinder.java | 9 ---- .../java/org/hibernate/cfg/Ejb3Column.java | 34 ++++++-------- .../cfg/Ejb3DiscriminatorColumn.java | 4 +- .../org/hibernate/cfg/Ejb3JoinColumn.java | 5 +- .../java/org/hibernate/cfg/IndexColumn.java | 2 +- .../cfg/annotations/CollectionBinder.java | 1 - .../hibernate/cfg/annotations/MapBinder.java | 3 +- .../java/org/hibernate/mapping/Column.java | 47 ++++++++++--------- .../java/org/hibernate/type/DoubleType.java | 2 +- .../hibernate/dialect/DB2DialectTestCase.java | 7 ++- .../annotations/beanvalidation/DDLTest.java | 8 ++-- .../nested/NestedEmbeddableMetadataTest.java | 2 +- ...dAccessedNestedEmbeddableMetadataTest.java | 2 +- .../internal/metadata/MetadataTools.java | 4 +- 14 files changed, 60 insertions(+), 70 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalObjectBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalObjectBinder.java index 0736f96edf..477d349172 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalObjectBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/RelationalObjectBinder.java @@ -143,23 +143,14 @@ public class RelationalObjectBinder { if ( columnSource.getSizeSource().getLength() != null ) { column.setLength( columnSource.getSizeSource().getLength() ); } - else { - column.setLength( Column.DEFAULT_LENGTH ); - } if ( columnSource.getSizeSource().getScale() != null ) { column.setScale( columnSource.getSizeSource().getScale() ); } - else { - column.setScale( Column.DEFAULT_SCALE ); - } if ( columnSource.getSizeSource().getPrecision() != null ) { column.setPrecision( columnSource.getSizeSource().getPrecision() ); } - else { - column.setPrecision( Column.DEFAULT_PRECISION ); - } } column.setNullable( interpretNullability( columnSource.isNullable(), areColumnsNullableByDefault ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java index 691dccc094..65b4b9695c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java @@ -53,11 +53,10 @@ public class Ejb3Column { protected Map joins; protected PropertyHolder propertyHolder; private boolean isImplicit; - public static final int DEFAULT_COLUMN_LENGTH = 255; public String sqlType; - private int length = DEFAULT_COLUMN_LENGTH; - private int precision; - private int scale; + private Long length; + private Integer precision; + private Integer scale; private String logicalColumnName; private String propertyName; private boolean unique; @@ -82,15 +81,15 @@ public class Ejb3Column { return sqlType; } - public int getLength() { + public Long getLength() { return length; } - public int getPrecision() { + public Integer getPrecision() { return precision; } - public int getScale() { + public Integer getScale() { return scale; } @@ -153,15 +152,15 @@ public class Ejb3Column { this.sqlType = sqlType; } - public void setLength(int length) { + public void setLength(Long length) { this.length = length; } - public void setPrecision(int precision) { + public void setPrecision(Integer precision) { this.precision = precision; } - public void setScale(int scale) { + public void setScale(Integer scale) { this.scale = scale; } @@ -182,7 +181,7 @@ public class Ejb3Column { } public boolean isNullable() { - return isFormula() ? true : mappingColumn.isNullable(); + return isFormula() || mappingColumn.isNullable(); } public String getDefaultValue() { @@ -218,9 +217,9 @@ public class Ejb3Column { protected void initMappingColumn( String columnName, String propertyName, - int length, - int precision, - int scale, + Long length, + Integer precision, + Integer scale, boolean nullable, String sqlType, boolean unique, @@ -233,7 +232,7 @@ public class Ejb3Column { this.mappingColumn = new Column(); redefineColumnName( columnName, propertyName, applyNamingStrategy ); this.mappingColumn.setLength( length ); - if ( precision > 0 ) { //revelent precision + if ( precision!=null ) { //relevant precision this.mappingColumn.setPrecision( precision ); this.mappingColumn.setScale( scale ); } @@ -536,8 +535,6 @@ public class Ejb3Column { final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer(); final Database database = context.getMetadataCollector().getDatabase(); - final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy(); - final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy(); javax.persistence.Column col = actualCols[index]; @@ -585,7 +582,7 @@ public class Ejb3Column { column.setImplicit( false ); column.setSqlType( sqlType ); - column.setLength( col.length() ); + column.setLength( (long) col.length() ); column.setPrecision( col.precision() ); column.setScale( col.scale() ); if ( StringHelper.isEmpty( columnName ) && ! StringHelper.isEmpty( suffixForDefaultColumnName ) ) { @@ -688,7 +685,6 @@ public class Ejb3Column { && !inferredData.getProperty().isArray() ) { column.setNullable( false ); } - column.setLength( DEFAULT_COLUMN_LENGTH ); final String propertyName = inferredData.getPropertyName(); column.setPropertyName( BinderHelper.getRelativePath( propertyHolder, propertyName ) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3DiscriminatorColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3DiscriminatorColumn.java index 595064a96d..9ab3c30480 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3DiscriminatorColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3DiscriminatorColumn.java @@ -20,7 +20,7 @@ import org.hibernate.boot.spi.MetadataBuildingContext; public class Ejb3DiscriminatorColumn extends Ejb3Column { public static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "DTYPE"; public static final String DEFAULT_DISCRIMINATOR_TYPE = "string"; - private static final int DEFAULT_DISCRIMINATOR_LENGTH = 31; + private static final long DEFAULT_DISCRIMINATOR_LENGTH = 31; private String discriminatorTypeName; @@ -73,7 +73,7 @@ public class Ejb3DiscriminatorColumn extends Ejb3Column { discriminatorColumn.setImplicit( false ); } else if ( DiscriminatorType.STRING.equals( type ) || type == null ) { - if ( discAnn != null ) discriminatorColumn.setLength( discAnn.length() ); + if ( discAnn != null ) discriminatorColumn.setLength( (long) discAnn.length() ); discriminatorColumn.setDiscriminatorTypeName( "string" ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java index 5fe60fd0d8..ab6e09489a 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java @@ -46,7 +46,6 @@ import org.hibernate.mapping.Value; * * @author Emmanuel Bernard */ -@SuppressWarnings("unchecked") public class Ejb3JoinColumn extends Ejb3Column { /** * property name related to this column @@ -474,9 +473,9 @@ public class Ejb3JoinColumn extends Ejb3Column { null, referencedColumn.getLength(), referencedColumn.getPrecision(), referencedColumn.getScale(), - getMappingColumn() != null ? getMappingColumn().isNullable() : false, + getMappingColumn() != null && getMappingColumn().isNullable(), referencedColumn.getSqlType(), - getMappingColumn() != null ? getMappingColumn().isUnique() : false, + getMappingColumn() != null && getMappingColumn().isUnique(), false ); linkWithValue( value ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java index ca4210697b..1bb7365fa7 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java @@ -23,7 +23,7 @@ public class IndexColumn extends Ejb3Column { public IndexColumn( boolean isImplicit, String sqlType, - int length, + long length, int precision, int scale, String name, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 04dfb45900..f78fe4e309 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -1620,7 +1620,6 @@ public abstract class CollectionBinder { column.setImplicit( false ); //not following the spec but more clean column.setNullable( true ); - column.setLength( Ejb3Column.DEFAULT_COLUMN_LENGTH ); column.setLogicalColumnName( Collection.DEFAULT_ELEMENT_COLUMN_NAME ); //TODO create an EMPTY_JOINS collection column.setJoins( new HashMap<>() ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java index 44d304fa0a..02d16969c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java @@ -41,6 +41,7 @@ import org.hibernate.cfg.PropertyHolderBuilder; import org.hibernate.cfg.PropertyPreloadedData; import org.hibernate.cfg.SecondPass; import org.hibernate.dialect.HSQLDialect; +import org.hibernate.engine.jdbc.Size; import org.hibernate.internal.util.StringHelper; import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Collection; @@ -310,7 +311,7 @@ public class MapBinder extends CollectionBinder { Ejb3Column column = new Ejb3Column(); column.setImplicit( false ); column.setNullable( true ); - column.setLength( Ejb3Column.DEFAULT_COLUMN_LENGTH ); + column.setLength( Size.DEFAULT_LENGTH ); column.setLogicalColumnName( Collection.DEFAULT_KEY_COLUMN_NAME ); //TODO create an EMPTY_JOINS collection column.setJoins( new HashMap() ); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java index 2415214d1c..c65503b71a 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java @@ -19,6 +19,7 @@ import org.hibernate.loader.internal.AliasConstantsHelper; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.sql.Template; +import org.hibernate.type.EntityType; import org.hibernate.type.Type; import static org.hibernate.internal.util.StringHelper.safeInterning; @@ -30,13 +31,9 @@ import static org.hibernate.internal.util.StringHelper.safeInterning; */ public class Column implements Selectable, Serializable, Cloneable { - public static final int DEFAULT_LENGTH = 255; - public static final int DEFAULT_PRECISION = 19; - public static final int DEFAULT_SCALE = 2; - - private int length; - private int precision; - private int scale; + private Long length; + private Integer precision; + private Integer scale; private Value value; private int typeIndex; private String name; @@ -59,14 +56,18 @@ public class Column implements Selectable, Serializable, Cloneable { setName( columnName ); } - public int getLength() { + public Long getLength() { return length; } - public void setLength(int length) { + public void setLength(Long length) { this.length = length; } + public void setLength(Integer length) { + this.length = length.longValue(); + } + public Value getValue() { return value; } @@ -241,14 +242,18 @@ public class Column implements Selectable, Serializable, Cloneable { Size size = new Size( getPrecision(), getScale(), getLength(), null ); Type type = getValue().getType(); if ( size.getLength() == null - && size.getScale() == null && size.getPrecision() == null - && type instanceof JdbcMapping) { - size = dialect.getDefaultSizeStrategy().resolveDefaultSize( - ((JdbcMapping) type).getSqlTypeDescriptor(), - ((JdbcMapping) type).getJavaTypeDescriptor() - ); + && size.getScale() == null && size.getPrecision() == null ) { + if ( type instanceof EntityType ) { + //ManyToOneType doesn't implement JdbcMapping + type = mapping.getIdentifierType( ((EntityType) type).getAssociatedEntityName() ); + } + if ( type instanceof JdbcMapping ) { + size = dialect.getDefaultSizeStrategy().resolveDefaultSize( + ((JdbcMapping) type).getSqlTypeDescriptor(), + ((JdbcMapping) type).getJavaTypeDescriptor() + ); + } } - sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), size ); } return sqlType; @@ -324,19 +329,19 @@ public class Column implements Selectable, Serializable, Cloneable { return getName(); } - public int getPrecision() { + public Integer getPrecision() { return precision; } - public void setPrecision(int scale) { - this.precision = scale; + public void setPrecision(Integer precision) { + this.precision = precision; } - public int getScale() { + public Integer getScale() { return scale; } - public void setScale(int scale) { + public void setScale(Integer scale) { this.scale = scale; } diff --git a/hibernate-core/src/main/java/org/hibernate/type/DoubleType.java b/hibernate-core/src/main/java/org/hibernate/type/DoubleType.java index 9ddde57b28..af25b9c891 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/DoubleType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/DoubleType.java @@ -23,7 +23,7 @@ public class DoubleType extends AbstractSingleColumnStandardBasicType im public static final Double ZERO = 0.0; public DoubleType() { - super( org.hibernate.type.descriptor.sql.DoubleTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE ); + super( org.hibernate.type.descriptor.sql.FloatTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE ); } @Override public String getName() { diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java b/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java index 5955231cc7..824412ac76 100644 --- a/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java @@ -12,7 +12,6 @@ import org.hibernate.engine.jdbc.Size; import org.hibernate.engine.spi.RowSelection; import org.junit.Test; -import org.hibernate.mapping.Column; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -43,7 +42,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase { @TestForIssue(jiraKey = "HHH-6866") public void testGetExplicitBinaryTypeName() { // lower bound - String actual = dialect.getTypeName( Types.BINARY, new Size(1, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE, null) ); + String actual = dialect.getTypeName( Types.BINARY, Size.length(1) ); assertEquals( "Wrong binary type", "char(1) for bit data", @@ -51,7 +50,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase { ); // upper bound - actual = dialect.getTypeName( Types.BINARY, new Size(254, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE, null) ); + actual = dialect.getTypeName( Types.BINARY, Size.length(254) ); assertEquals( "Wrong binary type. 254 is the max length in DB2", "char(254) for bit data", @@ -59,7 +58,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase { ); // exceeding upper bound - actual = dialect.getTypeName( Types.BINARY, new Size(255, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE, null) ); + actual = dialect.getTypeName( Types.BINARY, Size.length(255) ); assertEquals( "Wrong binary type. Should be varchar for length > 254", "varchar(255) for bit data", diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java index d70be411ee..cbb85c4b03 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java @@ -31,9 +31,9 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase { public void testBasicDDL() { PersistentClass classMapping = metadata().getEntityBinding( Address.class.getName() ); Column stateColumn = (Column) classMapping.getProperty( "state" ).getColumnIterator().next(); - assertEquals( stateColumn.getLength(), 3 ); + assertEquals( stateColumn.getLength(), (Long) 3L ); Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next(); - assertEquals( zipColumn.getLength(), 5 ); + assertEquals( zipColumn.getLength(), (Long) 5L ); assertFalse( zipColumn.isNullable() ); } @@ -41,7 +41,7 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase { public void testApplyOnIdColumn() throws Exception { PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() ); Column serialColumn = (Column) classMapping.getIdentifierProperty().getColumnIterator().next(); - assertEquals( "Validator annotation not applied on ids", 2, serialColumn.getLength() ); + assertEquals( "Validator annotation not applied on ids", (Long) 2L, serialColumn.getLength() ); } @Test @@ -49,7 +49,7 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase { public void testLengthConstraint() throws Exception { PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() ); Column modelColumn = (Column) classMapping.getProperty( "model" ).getColumnIterator().next(); - assertEquals( modelColumn.getLength(), 5 ); + assertEquals( modelColumn.getLength(), (Long) 5L ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java index 5c705968db..3562e01c24 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java @@ -51,7 +51,7 @@ public class NestedEmbeddableMetadataTest extends BaseUnitTestCase { Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue(); assertEquals( 1, descriptionValue.getColumnSpan() ); Column selectable = (Column) descriptionValue.getColumnIterator().next(); - assertEquals( 500, selectable.getLength() ); + assertEquals( (Long) 500L, selectable.getLength() ); Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue(); SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue(); CustomType currencyType = (CustomType) currencyMetadata.getType(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/fieldaccess/FieldAccessedNestedEmbeddableMetadataTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/fieldaccess/FieldAccessedNestedEmbeddableMetadataTest.java index f5fe4b9e72..a0558f8161 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/fieldaccess/FieldAccessedNestedEmbeddableMetadataTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/nested/fieldaccess/FieldAccessedNestedEmbeddableMetadataTest.java @@ -49,7 +49,7 @@ public class FieldAccessedNestedEmbeddableMetadataTest extends BaseUnitTestCase Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue(); assertEquals( 1, descriptionValue.getColumnSpan() ); Column selectable = (Column) descriptionValue.getColumnIterator().next(); - assertEquals( 500, selectable.getLength() ); + assertEquals( (Long) 500L, selectable.getLength() ); Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue(); SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue(); CustomType currencyType = (CustomType) currencyMetadata.getType(); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/MetadataTools.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/MetadataTools.java index 77f86d6c84..277985d007 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/MetadataTools.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/MetadataTools.java @@ -154,7 +154,7 @@ public final class MetadataTools { public static Element addColumn( Element parent, String name, - Integer length, + Long length, Integer scale, Integer precision, String sqlType, @@ -166,7 +166,7 @@ public final class MetadataTools { public static Element addColumn( Element parent, String name, - Integer length, + Long length, Integer scale, Integer precision, String sqlType,