Get type-specific column length/precision/scale defaulting working again

And fix resulting breakage to envers
This commit is contained in:
gavinking 2020-01-27 20:10:57 +01:00 committed by Steve Ebersole
parent f0d93200b5
commit 992b390fce
14 changed files with 60 additions and 70 deletions

View File

@ -143,23 +143,14 @@ public class RelationalObjectBinder {
if ( columnSource.getSizeSource().getLength() != null ) { if ( columnSource.getSizeSource().getLength() != null ) {
column.setLength( columnSource.getSizeSource().getLength() ); column.setLength( columnSource.getSizeSource().getLength() );
} }
else {
column.setLength( Column.DEFAULT_LENGTH );
}
if ( columnSource.getSizeSource().getScale() != null ) { if ( columnSource.getSizeSource().getScale() != null ) {
column.setScale( columnSource.getSizeSource().getScale() ); column.setScale( columnSource.getSizeSource().getScale() );
} }
else {
column.setScale( Column.DEFAULT_SCALE );
}
if ( columnSource.getSizeSource().getPrecision() != null ) { if ( columnSource.getSizeSource().getPrecision() != null ) {
column.setPrecision( columnSource.getSizeSource().getPrecision() ); column.setPrecision( columnSource.getSizeSource().getPrecision() );
} }
else {
column.setPrecision( Column.DEFAULT_PRECISION );
}
} }
column.setNullable( interpretNullability( columnSource.isNullable(), areColumnsNullableByDefault ) ); column.setNullable( interpretNullability( columnSource.isNullable(), areColumnsNullableByDefault ) );

View File

@ -53,11 +53,10 @@ public class Ejb3Column {
protected Map<String, Join> joins; protected Map<String, Join> joins;
protected PropertyHolder propertyHolder; protected PropertyHolder propertyHolder;
private boolean isImplicit; private boolean isImplicit;
public static final int DEFAULT_COLUMN_LENGTH = 255;
public String sqlType; public String sqlType;
private int length = DEFAULT_COLUMN_LENGTH; private Long length;
private int precision; private Integer precision;
private int scale; private Integer scale;
private String logicalColumnName; private String logicalColumnName;
private String propertyName; private String propertyName;
private boolean unique; private boolean unique;
@ -82,15 +81,15 @@ public class Ejb3Column {
return sqlType; return sqlType;
} }
public int getLength() { public Long getLength() {
return length; return length;
} }
public int getPrecision() { public Integer getPrecision() {
return precision; return precision;
} }
public int getScale() { public Integer getScale() {
return scale; return scale;
} }
@ -153,15 +152,15 @@ public class Ejb3Column {
this.sqlType = sqlType; this.sqlType = sqlType;
} }
public void setLength(int length) { public void setLength(Long length) {
this.length = length; this.length = length;
} }
public void setPrecision(int precision) { public void setPrecision(Integer precision) {
this.precision = precision; this.precision = precision;
} }
public void setScale(int scale) { public void setScale(Integer scale) {
this.scale = scale; this.scale = scale;
} }
@ -182,7 +181,7 @@ public class Ejb3Column {
} }
public boolean isNullable() { public boolean isNullable() {
return isFormula() ? true : mappingColumn.isNullable(); return isFormula() || mappingColumn.isNullable();
} }
public String getDefaultValue() { public String getDefaultValue() {
@ -218,9 +217,9 @@ public class Ejb3Column {
protected void initMappingColumn( protected void initMappingColumn(
String columnName, String columnName,
String propertyName, String propertyName,
int length, Long length,
int precision, Integer precision,
int scale, Integer scale,
boolean nullable, boolean nullable,
String sqlType, String sqlType,
boolean unique, boolean unique,
@ -233,7 +232,7 @@ public class Ejb3Column {
this.mappingColumn = new Column(); this.mappingColumn = new Column();
redefineColumnName( columnName, propertyName, applyNamingStrategy ); redefineColumnName( columnName, propertyName, applyNamingStrategy );
this.mappingColumn.setLength( length ); this.mappingColumn.setLength( length );
if ( precision > 0 ) { //revelent precision if ( precision!=null ) { //relevant precision
this.mappingColumn.setPrecision( precision ); this.mappingColumn.setPrecision( precision );
this.mappingColumn.setScale( scale ); this.mappingColumn.setScale( scale );
} }
@ -536,8 +535,6 @@ public class Ejb3Column {
final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer(); final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
final Database database = context.getMetadataCollector().getDatabase(); final Database database = context.getMetadataCollector().getDatabase();
final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
javax.persistence.Column col = actualCols[index]; javax.persistence.Column col = actualCols[index];
@ -585,7 +582,7 @@ public class Ejb3Column {
column.setImplicit( false ); column.setImplicit( false );
column.setSqlType( sqlType ); column.setSqlType( sqlType );
column.setLength( col.length() ); column.setLength( (long) col.length() );
column.setPrecision( col.precision() ); column.setPrecision( col.precision() );
column.setScale( col.scale() ); column.setScale( col.scale() );
if ( StringHelper.isEmpty( columnName ) && ! StringHelper.isEmpty( suffixForDefaultColumnName ) ) { if ( StringHelper.isEmpty( columnName ) && ! StringHelper.isEmpty( suffixForDefaultColumnName ) ) {
@ -688,7 +685,6 @@ public class Ejb3Column {
&& !inferredData.getProperty().isArray() ) { && !inferredData.getProperty().isArray() ) {
column.setNullable( false ); column.setNullable( false );
} }
column.setLength( DEFAULT_COLUMN_LENGTH );
final String propertyName = inferredData.getPropertyName(); final String propertyName = inferredData.getPropertyName();
column.setPropertyName( column.setPropertyName(
BinderHelper.getRelativePath( propertyHolder, propertyName ) BinderHelper.getRelativePath( propertyHolder, propertyName )

View File

@ -20,7 +20,7 @@ import org.hibernate.boot.spi.MetadataBuildingContext;
public class Ejb3DiscriminatorColumn extends Ejb3Column { public class Ejb3DiscriminatorColumn extends Ejb3Column {
public static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "DTYPE"; public static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "DTYPE";
public static final String DEFAULT_DISCRIMINATOR_TYPE = "string"; 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; private String discriminatorTypeName;
@ -73,7 +73,7 @@ public class Ejb3DiscriminatorColumn extends Ejb3Column {
discriminatorColumn.setImplicit( false ); discriminatorColumn.setImplicit( false );
} }
else if ( DiscriminatorType.STRING.equals( type ) || type == null ) { 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" ); discriminatorColumn.setDiscriminatorTypeName( "string" );
} }
else { else {

View File

@ -46,7 +46,6 @@ import org.hibernate.mapping.Value;
* *
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
@SuppressWarnings("unchecked")
public class Ejb3JoinColumn extends Ejb3Column { public class Ejb3JoinColumn extends Ejb3Column {
/** /**
* property name related to this column * property name related to this column
@ -474,9 +473,9 @@ public class Ejb3JoinColumn extends Ejb3Column {
null, referencedColumn.getLength(), null, referencedColumn.getLength(),
referencedColumn.getPrecision(), referencedColumn.getPrecision(),
referencedColumn.getScale(), referencedColumn.getScale(),
getMappingColumn() != null ? getMappingColumn().isNullable() : false, getMappingColumn() != null && getMappingColumn().isNullable(),
referencedColumn.getSqlType(), referencedColumn.getSqlType(),
getMappingColumn() != null ? getMappingColumn().isUnique() : false, getMappingColumn() != null && getMappingColumn().isUnique(),
false false
); );
linkWithValue( value ); linkWithValue( value );

View File

@ -23,7 +23,7 @@ public class IndexColumn extends Ejb3Column {
public IndexColumn( public IndexColumn(
boolean isImplicit, boolean isImplicit,
String sqlType, String sqlType,
int length, long length,
int precision, int precision,
int scale, int scale,
String name, String name,

View File

@ -1620,7 +1620,6 @@ public abstract class CollectionBinder {
column.setImplicit( false ); column.setImplicit( false );
//not following the spec but more clean //not following the spec but more clean
column.setNullable( true ); column.setNullable( true );
column.setLength( Ejb3Column.DEFAULT_COLUMN_LENGTH );
column.setLogicalColumnName( Collection.DEFAULT_ELEMENT_COLUMN_NAME ); column.setLogicalColumnName( Collection.DEFAULT_ELEMENT_COLUMN_NAME );
//TODO create an EMPTY_JOINS collection //TODO create an EMPTY_JOINS collection
column.setJoins( new HashMap<>() ); column.setJoins( new HashMap<>() );

View File

@ -41,6 +41,7 @@ import org.hibernate.cfg.PropertyHolderBuilder;
import org.hibernate.cfg.PropertyPreloadedData; import org.hibernate.cfg.PropertyPreloadedData;
import org.hibernate.cfg.SecondPass; import org.hibernate.cfg.SecondPass;
import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.HSQLDialect;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Collection; import org.hibernate.mapping.Collection;
@ -310,7 +311,7 @@ public class MapBinder extends CollectionBinder {
Ejb3Column column = new Ejb3Column(); Ejb3Column column = new Ejb3Column();
column.setImplicit( false ); column.setImplicit( false );
column.setNullable( true ); column.setNullable( true );
column.setLength( Ejb3Column.DEFAULT_COLUMN_LENGTH ); column.setLength( Size.DEFAULT_LENGTH );
column.setLogicalColumnName( Collection.DEFAULT_KEY_COLUMN_NAME ); column.setLogicalColumnName( Collection.DEFAULT_KEY_COLUMN_NAME );
//TODO create an EMPTY_JOINS collection //TODO create an EMPTY_JOINS collection
column.setJoins( new HashMap<String, Join>() ); column.setJoins( new HashMap<String, Join>() );

View File

@ -19,6 +19,7 @@ import org.hibernate.loader.internal.AliasConstantsHelper;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.sql.Template; import org.hibernate.sql.Template;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import static org.hibernate.internal.util.StringHelper.safeInterning; 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 class Column implements Selectable, Serializable, Cloneable {
public static final int DEFAULT_LENGTH = 255; private Long length;
public static final int DEFAULT_PRECISION = 19; private Integer precision;
public static final int DEFAULT_SCALE = 2; private Integer scale;
private int length;
private int precision;
private int scale;
private Value value; private Value value;
private int typeIndex; private int typeIndex;
private String name; private String name;
@ -59,14 +56,18 @@ public class Column implements Selectable, Serializable, Cloneable {
setName( columnName ); setName( columnName );
} }
public int getLength() { public Long getLength() {
return length; return length;
} }
public void setLength(int length) { public void setLength(Long length) {
this.length = length; this.length = length;
} }
public void setLength(Integer length) {
this.length = length.longValue();
}
public Value getValue() { public Value getValue() {
return value; return value;
} }
@ -241,14 +242,18 @@ public class Column implements Selectable, Serializable, Cloneable {
Size size = new Size( getPrecision(), getScale(), getLength(), null ); Size size = new Size( getPrecision(), getScale(), getLength(), null );
Type type = getValue().getType(); Type type = getValue().getType();
if ( size.getLength() == null if ( size.getLength() == null
&& size.getScale() == null && size.getPrecision() == null && size.getScale() == null && size.getPrecision() == null ) {
&& type instanceof JdbcMapping) { if ( type instanceof EntityType ) {
size = dialect.getDefaultSizeStrategy().resolveDefaultSize( //ManyToOneType doesn't implement JdbcMapping
((JdbcMapping) type).getSqlTypeDescriptor(), type = mapping.getIdentifierType( ((EntityType) type).getAssociatedEntityName() );
((JdbcMapping) type).getJavaTypeDescriptor() }
); if ( type instanceof JdbcMapping ) {
size = dialect.getDefaultSizeStrategy().resolveDefaultSize(
((JdbcMapping) type).getSqlTypeDescriptor(),
((JdbcMapping) type).getJavaTypeDescriptor()
);
}
} }
sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), size ); sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), size );
} }
return sqlType; return sqlType;
@ -324,19 +329,19 @@ public class Column implements Selectable, Serializable, Cloneable {
return getName(); return getName();
} }
public int getPrecision() { public Integer getPrecision() {
return precision; return precision;
} }
public void setPrecision(int scale) { public void setPrecision(Integer precision) {
this.precision = scale; this.precision = precision;
} }
public int getScale() { public Integer getScale() {
return scale; return scale;
} }
public void setScale(int scale) { public void setScale(Integer scale) {
this.scale = scale; this.scale = scale;
} }

View File

@ -23,7 +23,7 @@ public class DoubleType extends AbstractSingleColumnStandardBasicType<Double> im
public static final Double ZERO = 0.0; public static final Double ZERO = 0.0;
public DoubleType() { public DoubleType() {
super( org.hibernate.type.descriptor.sql.DoubleTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE ); super( org.hibernate.type.descriptor.sql.FloatTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE );
} }
@Override @Override
public String getName() { public String getName() {

View File

@ -12,7 +12,6 @@ import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.RowSelection; import org.hibernate.engine.spi.RowSelection;
import org.junit.Test; import org.junit.Test;
import org.hibernate.mapping.Column;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -43,7 +42,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase {
@TestForIssue(jiraKey = "HHH-6866") @TestForIssue(jiraKey = "HHH-6866")
public void testGetExplicitBinaryTypeName() { public void testGetExplicitBinaryTypeName() {
// lower bound // 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( assertEquals(
"Wrong binary type", "Wrong binary type",
"char(1) for bit data", "char(1) for bit data",
@ -51,7 +50,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase {
); );
// upper bound // 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( assertEquals(
"Wrong binary type. 254 is the max length in DB2", "Wrong binary type. 254 is the max length in DB2",
"char(254) for bit data", "char(254) for bit data",
@ -59,7 +58,7 @@ public class DB2DialectTestCase extends BaseUnitTestCase {
); );
// exceeding upper bound // 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( assertEquals(
"Wrong binary type. Should be varchar for length > 254", "Wrong binary type. Should be varchar for length > 254",
"varchar(255) for bit data", "varchar(255) for bit data",

View File

@ -31,9 +31,9 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase {
public void testBasicDDL() { public void testBasicDDL() {
PersistentClass classMapping = metadata().getEntityBinding( Address.class.getName() ); PersistentClass classMapping = metadata().getEntityBinding( Address.class.getName() );
Column stateColumn = (Column) classMapping.getProperty( "state" ).getColumnIterator().next(); 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(); Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next();
assertEquals( zipColumn.getLength(), 5 ); assertEquals( zipColumn.getLength(), (Long) 5L );
assertFalse( zipColumn.isNullable() ); assertFalse( zipColumn.isNullable() );
} }
@ -41,7 +41,7 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase {
public void testApplyOnIdColumn() throws Exception { public void testApplyOnIdColumn() throws Exception {
PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() ); PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() );
Column serialColumn = (Column) classMapping.getIdentifierProperty().getColumnIterator().next(); 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 @Test
@ -49,7 +49,7 @@ public class DDLTest extends BaseNonConfigCoreFunctionalTestCase {
public void testLengthConstraint() throws Exception { public void testLengthConstraint() throws Exception {
PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() ); PersistentClass classMapping = metadata().getEntityBinding( Tv.class.getName() );
Column modelColumn = (Column) classMapping.getProperty( "model" ).getColumnIterator().next(); Column modelColumn = (Column) classMapping.getProperty( "model" ).getColumnIterator().next();
assertEquals( modelColumn.getLength(), 5 ); assertEquals( modelColumn.getLength(), (Long) 5L );
} }
@Test @Test

View File

@ -51,7 +51,7 @@ public class NestedEmbeddableMetadataTest extends BaseUnitTestCase {
Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue(); Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue();
assertEquals( 1, descriptionValue.getColumnSpan() ); assertEquals( 1, descriptionValue.getColumnSpan() );
Column selectable = (Column) descriptionValue.getColumnIterator().next(); Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals( 500, selectable.getLength() ); assertEquals( (Long) 500L, selectable.getLength() );
Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue(); Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue(); SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType(); CustomType currencyType = (CustomType) currencyMetadata.getType();

View File

@ -49,7 +49,7 @@ public class FieldAccessedNestedEmbeddableMetadataTest extends BaseUnitTestCase
Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue(); Value descriptionValue = investmentMetadata.getProperty( "description" ).getValue();
assertEquals( 1, descriptionValue.getColumnSpan() ); assertEquals( 1, descriptionValue.getColumnSpan() );
Column selectable = (Column) descriptionValue.getColumnIterator().next(); Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals( 500, selectable.getLength() ); assertEquals( (Long) 500L, selectable.getLength() );
Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue(); Component amountMetadata = (Component) investmentMetadata.getProperty( "amount" ).getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue(); SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty( "currency" ).getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType(); CustomType currencyType = (CustomType) currencyMetadata.getType();

View File

@ -154,7 +154,7 @@ public final class MetadataTools {
public static Element addColumn( public static Element addColumn(
Element parent, Element parent,
String name, String name,
Integer length, Long length,
Integer scale, Integer scale,
Integer precision, Integer precision,
String sqlType, String sqlType,
@ -166,7 +166,7 @@ public final class MetadataTools {
public static Element addColumn( public static Element addColumn(
Element parent, Element parent,
String name, String name,
Integer length, Long length,
Integer scale, Integer scale,
Integer precision, Integer precision,
String sqlType, String sqlType,