问题描述:
在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:
-
BINARY[(
N
)]
-
CHAR[(
N
)]
-
DATE
-
DATETIME
-
DECIMAL[(
M
[,
D
])]
-
SIGNED [INTEGER]
-
TIME
-
UNSIGNED [INTEGER]
因此在hql中使用cast (ta.id as integer ) 都会报错。
解决方法,修改MySQLDialect类,或者新建一个类继承MySQLDialect.修改方法getCastTypeName:
public String getCastTypeName(int code) {
if ( code==Types.INTEGER || code == Types.BIGINT )
{ return "signed"; }
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