问题描述:
在hibernate+mySQL中使用HQL的cast语句时,由于mySQL的cast函数只支持转成以下8种类型:
The CONVERT()
and CAST()
functions take an expression of any type and produce a result value of a specified type.
The type
for the result can be one of the following values:
因此在hql中使用cast (ta.id as integer ) 都会报错。
解决方法,修改MySQLDialect类,或者新建一个类继承MySQLDialect.修改方法getCastTypeName:
public String getCastTypeName(int code) {
if ( code==Types.INTEGER || code == Types.BIGINT )
else if ( code==Types.VARCHAR )
{ return "char"; }else if ( code==Types.VARBINARY )
{ return "binary"; }else
{ return super.getCastTypeName( code ); }}
附hibernate数据类型与java类型的对应关系:http://jclick.iteye.com/blog/1452637