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