hibernate createSQLQuery StringIndexOutOfBoundsException: String index out of range: 0

有一个sql用union拼接的如下:

select id,(**还有很多字段**),'' as NewName from tb1
union
select id,(**还有很多字段**),name as NewName from tb2
union
select id,(**还有很多字段**),name as NewName from tb3

tb1表中不存在这个字段所以用'' as NewName
tb2,tb3中有这了个字段是varchar的
查询以一直报错:StringIndexOutOfBoundsException: String index out of range: 0
1.但是每个select单独查询完全没问题 ,开始以为是union的问题,

2.去掉NewName 这个字段后也没问题,.以为是sql太长了,

3.解决:最后网上查是因为:处理方法  数据表字段为char导致,修改为VARCHAR. 建表时不要使用char类型,为null就会报以上错误

原因是tb1中:'' as NewName以为是char而下面有数据的是varchar所以union的时候类型不一样就会报错。

最后把:  '' as NewName  改成  null as NewName 

改完成SQL如下,搞定:

select id,(**还有很多字段**),null as NewName from tb1
union
select id,(**还有很多字段**),name as NewName from tb2
union
select id,(**还有很多字段**),name as NewName from tb3

 

 

 和上面相反的问题 

hibernate  createSQLQuery报错: MappingException: No Dialect mapping for JDBC type: 0

有SQL,只有一个select:

select id,(**还有很多字段**),null as isVisit from Table

报错MappingException: No Dialect mapping for JDBC type: 0

(isVist数据库中不存在这个字段)把:  null as isVisit  改成  null as isVisit 搞定:
select id,(**还有很多字段**),'' as isVisit from Table
 


posted @ 2019-11-26 13:51  为乐而来  阅读(1335)  评论(0编辑  收藏  举报