if we do not declare the sql-type or attribute type, it defaults to String or VARCHAR(255).
2011-09-10 16:33
543 查看
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
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
相关文章推荐
- Colored SQL--How to force the AWR to capture a specified SQL even if it is not the top one
- Cannot connect to WMI provider.You do not have permission or the server is unreachable.Note that you can only manager SQL Server 2005 and later version with SQL Server Configuration Manager.Invalid namespace [0x8004100e]
- The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
- 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".
- CocoaPods问题: CocoaPods was not able to update the `master` repo.if it is a unexpected issue and
- How to scroll an image, if it does not fit in the display
- This application failed to start because it could not find or load the Qt platform plugin "xcb"
- Could not store transport type data for Receive Location 'Recv.Loc' to config store. Primary SSO Server 'Sql-server' failed. The external credentials in the SSO database are more recent.
- This application failed to start because it could not find or load the Qt platform plugin
- sql --it is not contained in either an aggregate function or the GROUP BY clause.
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- org.apache.hadoop.hbase.DoNotRetryIOException: Class org.apache.phoenix.coprocessor.MetaDataEndpointImpl cannot be loaded Set hbase.table.sanity.checks to false at conf or table descriptor if you want
- Anaconda "failed to start because it could not find or load the Qt platform plugin"
- Oracle Bug: WARNING:Could not lower the asynch I/O limit to 224 for SQL direct I/O. It is set to -1
- qt程序运行在树莓派错误解决This application failed to start because it could not find or load the Qt platform plu
- 执行 maven 命令 报错Unable to add module to the current project as it is not of packaging type 'pom'
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- QT移植无法启动 This application failed to start because it could not find or load the Qt platform plugin
- PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath s
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "