spring - 字符串索引超出范围:当存储过程在 Spring Boot 中返回空值时为 0
问题描述
我创建了一个 Spring Boot 应用程序,它调用存储过程以使用 CrudRepository 获取数据。下面是 Repo 接口的实现:
public interface MyRepository extends CrudRepository<EmpEntity, Long> {
@Query(value = "call myStoredProc(:empID, :empProv);", nativeQuery = true)
Map<String, String> findWithNumber(@Param("empID ") String empID, @Param("empProv ") String empProv);
}
存储过程返回 10 个变量,其中 1 个值返回空白值。由于这种行为,我收到以下错误:
2020-06-10 08:02:46.118 ERROR 23724 --- [nio-8050-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: 0] with root cause
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658) ~[na:1.8.0_144]
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:61) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:16) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:62) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
我可以通过将 NULLIF(a.addressType,"") addressType 放入存储过程来解决此问题,但我想了解问题背后的原因以及 Spring Boot 应用程序中可能的修复方法。
解决方案
推荐阅读
- django - 提交表单时显示MultipleObjectsReturned
- flutter - 如何在 Flutter 应用中使用 png 文件作为图标?
- vba - Word VBA 网页抓取-我如何抓取某些课程并跳过其他课程
- ios - Viewcontroller 的导航栏添加在标签栏控制器内导航控制器的导航栏下方
- java - 上传和检索图像 android studio Parse 服务器
- javascript - 单击按钮时,“未定义”显示在控制台中
- python - 发回响应后如何使 Django 视图进一步运行?
- javascript - 为什么要麻烦调用 toString?
- node.js - 是否可以使用 Google 的 Drive API v3 查询多个文件夹(父母)?
- javascript - 在 Mongoose 中保存时刻格式