数据库
首页 > 数据库> > hibernate createSQLQuery StringIndexOutOfBoundsException: String index out of range: 0

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

 

 

 



标签:index,createSQLQuery,hibernate,name,NewName,union,tb1,id,select
来源: https://www.cnblogs.com/q149072205/p/11934945.html