Here is what I found by debugging:
Column 'empno' in table 'Employee' is declared as 'CHAR'. In hibernate if we do not declare the sql-type or attribute type, it defaults to String or VARCHAR(255). As hibernate validation routine compares using this code:
*****************Table.java- validateColumns - method***********
final boolean typesMatch = col.getSqlType(dialect, mapping)
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| columnInfo.getTypeCode() == col.getSqlTypeCode(mapping);
if ( !typesMatch ) {
throw new HibernateException(........
**********************************************
1. between VARCHAR(255), which is default and the METADATA and its type gathered from DB2Dialect, which is 'CHAR'--->col.getSqlType(dialect, mapping)= "varchar(255)" - Hibernate default
columnInfo.getTypeName().toLowerCase()= "char" -> value returned by DB2 driver
This code will return false
2. In this part of the code Hibernate compares between mapped information and the Metadata returned by DB2 driver in numbers or code for Types:
columnInfo.getTypeCode() will return 1 (CHAR from DB2 table)
col.getSqlTypeCode(mapping) will return 12 (String - Hibernate default)
columnInfo.getTypeCode() == col.getSqlTypeCode(mapping)
This code too will return false. As both return false values Hibernate throws an exception with error message:
org.hibernate.HibernateException: Wrong column type: EMPNO, expected: varchar(255)
********************Resolution****************
I hardcoded the sql-type='char' did the trick. Please note the lower case. If we use upper case it will fail. I think case issue should have been addressed by Hibernate code by converting values either to lower or upper case instead of asking users to use always lower case. Here is the mapping part I have changed to make it work:
<class name="com.ibm.hibernate.Employee" table="EMPLOYEE">
<id name="empno" type="string">
<column name="EMPNO" sql-type="char"/>
</id>
<property name="firstNme">
<column name="FIRSTNME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
</class>
It hasa nothing to do with sequence generation as I mentioned earlier this is related to sql-type between Hibernate default value and DB2 dialect in case of validation. Not sure if this is an issue with Hibernate code and to be addressed or not. Thanks for all the help, analysis and feedback
if we do not declare the sql-type or attribute type, it defaults to String or VARCHAR(255).
最新推荐文章于 2024-06-04 09:07:32 发布