問題描述:
在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