首页 > 解决方案 > 字符串索引超出范围:当存储过程在 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 应用程序中可能的修复方法。

标签: springspring-bootjpaspring-data-jpa

解决方案


推荐阅读