Hibernate中text字段诡异出错
描述:最初的sql语句如下:
select a.game_id,ifnull(cid,0) as cid,ifnull(fullname,'') as title,format_name,file_size*1024 as size,
ifnull(c.name,'') as mobileBrand,ifnull(d.series,'') as series,ifnull(description,'') as description,ifnull
(image_url,'') as image,
concat(download_url,'.',format_name) as url,ifnull(keywords,'') as keywords
from t_game_serise a left JOIN t_games b on a.game_id=b.game_id
left join t_brand c on a.brand_id=c.brand_id
left join t_series d on a.brand_id=d.id order by b.rank
由于series,url等字段的是varchar(2000),因此在查询后由于使用了ifnull()以及concat()函数之后变成了text类型,估计是有些
字段值比较长的缘故,因为在长度比较小的情况下使用ifnull()和concat函数时并不会改变varchar类型。
问题:在使用jdbc的时候肯定是没有问题的,但在使用hibernate的时候就出错,出错信息如下:No Dialect mapping for JDBC
type: -1;原因就是由于sessionFactory.getCurrentSession().createSQLQuery(mostPopularFilmsSQL).list()造成的,具体原因
好像是和hibernate的方言有关系。
解决办法:使用cast()函数把text类型转换成char类型,改变之后的sql语句如下:
select game_id,'5012',cid,title,format_name,size,mobileBrand,cast(series AS CHAR(2000)),
cast(description as CHAR(2000)),cast(image as char(2000)),cast(url as char(2000)),cast(keywords as char(2000))
from
(
select a.game_id,ifnull(cid,0) as cid,ifnull(fullname,'') as title,format_name,file_size*1024 as size,
ifnull(c.name,'') as mobileBrand,ifnull(d.series,'') as series,ifnull(description,'') as description,ifnull
(image_url,'') as image,
concat(download_url,'.',format_name) as url,ifnull(keywords,'') as keywords
from t_game_serise a left JOIN t_games b on a.game_id=b.game_id
left join t_brand c on a.brand_id=c.brand_id
left join t_series d on a.brand_id=d.id order by b.rank
) e
上面的sql语句通过Hibernate执行顺利通过~~~
总结:hibernate的通过hql语句查询带有text字段时不会有错,当通过sql语句查询带有text字段时则会出现No Dialect mapping for JDBCtype: -1 的错误信息。