diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java index 5222556dfb..6fae4d0773 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java @@ -51,7 +51,8 @@ public class DB2Dialect extends Dialect { registerColumnType( Types.SMALLINT, "smallint" ); registerColumnType( Types.TINYINT, "smallint" ); registerColumnType( Types.INTEGER, "integer" ); - registerColumnType( Types.CHAR, "char(1)" ); + registerColumnType( Types.CHAR, "varchar($l)" ); + registerColumnType( Types.CHAR, 254, "char($l)" ); registerColumnType( Types.VARCHAR, "varchar($l)" ); registerColumnType( Types.FLOAT, "float" ); registerColumnType( Types.DOUBLE, "double" ); @@ -64,7 +65,8 @@ public class DB2Dialect extends Dialect { registerColumnType( Types.CLOB, "clob($l)" ); registerColumnType( Types.LONGVARCHAR, "long varchar" ); registerColumnType( Types.LONGVARBINARY, "long varchar for bit data" ); - registerColumnType( Types.BINARY, "char($l) for bit data" ); + registerColumnType( Types.BINARY, "varchar($l) for bit data" ); + registerColumnType( Types.BINARY, 254, "char($l) for bit data" ); registerColumnType( Types.BOOLEAN, "smallint" ); registerFunction( "avg", new AvgWithArgumentCastFunction( "double" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java b/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java new file mode 100644 index 0000000000..6255444269 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/dialect/DB2DialectTestCase.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc.. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.dialect; + +import java.sql.Types; + +import org.junit.Test; + +import org.hibernate.mapping.Column; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static org.junit.Assert.assertEquals; + +/** + * DB2 dialect related test cases + * + * @author Hardy Ferentschik + */ + +public class DB2DialectTestCase extends BaseUnitTestCase { + private final DB2Dialect dialect = new DB2Dialect(); + + @Test + @TestForIssue(jiraKey = "HHH-6866") + public void testGetDefaultBinaryTypeName() { + String actual = dialect.getTypeName( Types.BINARY ); + assertEquals( + "The default column length is 255, but char length on DB2 is limited to 254", + "varchar($l) for bit data", + actual + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-6866") + public void testGetExplicitBinaryTypeName() { + // lower bound + String actual = dialect.getTypeName( Types.BINARY, 1, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong binary type", + "char(1) for bit data", + actual + ); + + // upper bound + actual = dialect.getTypeName( Types.BINARY, 254, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong binary type. 254 is the max length in DB2", + "char(254) for bit data", + actual + ); + + // exceeding upper bound + actual = dialect.getTypeName( Types.BINARY, 255, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong binary type. Should be varchar for length > 254", + "varchar(255) for bit data", + actual + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-6866") + public void testGetDefaultCharTypeName() { + String actual = dialect.getTypeName( Types.CHAR ); + assertEquals( + "The default column length is 255, but char length on DB2 is limited to 254", + "varchar($l)", + actual + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-6866") + public void testGetExplicitCharTypeName() { + // lower bound + String actual = dialect.getTypeName( Types.CHAR, 1, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong char type", + "char(1)", + actual + ); + + // upper bound + actual = dialect.getTypeName( Types.CHAR, 254, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong char type. 254 is the max length in DB2", + "char(254)", + actual + ); + + // exceeding upper bound + actual = dialect.getTypeName( Types.CHAR, 255, Column.DEFAULT_PRECISION, Column.DEFAULT_SCALE ); + assertEquals( + "Wrong char type. Should be varchar for length > 254", + "varchar(255)", + actual + ); + } +}