首页 > 解决方案 > 通过 StoredProcedureQuery 调用现有存储过程会导致 JdbcSQLSyntaxErrorException 异常

问题描述

在 MySQL 数据库中,我编写了以下存储过程:

CREATE DEFINER = `root` @` localhost` PROCEDURE `ALLITEMS` ()
BEGIN
  select * from task_item;
END

在 JHipster 应用程序中,我尝试通过调用指定的过程来获取结果集。

我是否使用 createStoredProcedureQuery() 方法,

@Override
@Transactional(readOnly = true)
public List<TaskItem> findTaskItem() {
 StoredProcedureQuery spq = em.createStoredProcedureQuery("allitems"); //("ALLITEMS");
 spq.execute();
 return spq.getResultList();
}

...或@Query 注释,

TaskItemRepository:
    @Query(value = "call ALLITEMS();", nativeQuery = true)
    List<TaskItem> findTaskItems();
...
    @Override
    @Transactional(readOnly = true)
    public List<TaskItem> findTaskItem() {
        return taskItemRepository.findTaskItems();
    }

...当我调用上述存储过程时,会发生以下异常:

Function "ALLITEMS" not found;

完整的堆栈跟踪很长。

...
2021-09-23 12:44:25.105 DEBUG 23884 --- [  XNIO-1 task-2] org.hibernate.SQL                        : {call allitems()}
2021-09-23 12:44:25.122  WARN 23884 --- [  XNIO-1 task-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 90022, SQLState: 90022
2021-09-23 12:44:25.122 ERROR 23884 --- [  XNIO-1 task-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Function "ALLITEMS" not found; SQL statement:
 call allitems()  [90022-200]
2021-09-23 12:44:25.139 ERROR 23884 --- [  XNIO-1 task-2] c.c.service.impl.TaskItemServiceImpl     : Exception in findTaskItem() with cause = 'org.hibernate.exception.SQLGrammarException: could not prepare statement' and exception = 'org.hibernate.exception.SQLGrammarException: could not prepare statement'
...
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Function "ALLITEMS" not found; SQL statement:
 call allitems()  [90022-200]
...

有没有人有类似的经历?

标签: mysqlspring-bootstored-proceduresjhipsterresultset

解决方案


推荐阅读