if we do not declare the sql-type or attribute type, it defaults to String or VARCHAR(255).

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值