SpringBoot 3.2.0(Hibernate 6)调用PostgreSQL函数失败求助
Spring Boot 3.2.0 迁移后调用PostgreSQL函数的问题
问题描述
将Spring Boot从2.5版本迁移至3.2.0版本后,原有的entityManager.createStoredProcedureQuery()调用PostgreSQL函数的代码无法正常运行。
PostgreSQL函数定义
CREATE OR REPLACE FUNCTION abc.my_func( v_response bigint) RETURNS TABLE(variableA bigint, variableB text, variableC bigint, phone character varying) LANGUAGE 'plpgsql' -- 函数实现省略
原有调用代码
StoredProcedureQuery query = entityManager .createStoredProcedureQuery("abc.my_func", ResultTable.class) .registerStoredProcedureParameter(1, Long.class, ParameterMode.IN) .setParameter(1, Long.valueOf(myvariable)); try{ query.getResultList().forEach(i->results.add((ResultTable)i)); }
这段代码在Spring Boot 2.5(Hibernate 5)中可正常运行,但迁移到Spring Boot 3.2.0(Hibernate 6)后出现以下错误:
ERROR: abc.my_func(bigint) is not a procedure Hint: To call a function, use SELECT. Position: 6
已尝试的解决方案及遇到的问题
- 设置旧版Hibernate方言:网上找到Spring Boot 3.1.2的同类问题解决方案,建议设置方言为
org.hibernate.dialect.PostgreSQL10Dialect,但该类在Spring Boot 3.2中已被废弃,设置后出现ClassNotFoundException。 - 使用
createNativeQuery和@Query:- 使用
@Query时抛出异常:java.lang.ClassCastException: class org.postgresql.util.PGobject cannot be cast to class com.sample.dto.ResultTable (org.postgresql.util.PGobject and com.sample.dto.ResultTable are in unnamed module of loader 'app') - 使用
createNativeQuery时抛出异常(variableA是ResultTable实体类的ID):org.hibernate.exception.SQLGrammarException: Unable to find column position by name: variableA [The column name variableA was not found in this ResultSet.] [n/a]
- 使用
- 使用
@SqlResultSetMapping:仍报错:org.hibernate.query.results.MissingSqlSelectionException: ResultSet mapping specified selected-alias `variableA` which was not part of the ResultSet
环境限制
使用的是Google Cloud SQL提供的PostgreSQL 12数据库,无法升级数据库版本,需针对该版本寻找解决方案。
内容的提问来源于stack exchange,提问作者Sayak-
相关产品推荐
相关产品推荐

