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));
}
上面的代码使用Hibernate 5在Springboot 2.5中工作,但是在Hibernate 6中,我低于错误,
ERROR: abc.my_func(bigint) is not a procedure
Hint: To call a function, use SELECT.
Position: 6
在浏览互联网时,我遇到了以下帖子,用户面临同一问题,但在Spring Boot 3.1.2中,作为解决方法,他将Hibernate方言设置为org.hibernate.dialect.postgresqlesql10dialect,以解决问题。 。
https://stackoverflow.com/questions/76927479/postgresql-hibernate-6-store-function-cannot-call但是就我而言,在SB 3.2 org.hibernate.dialect.postgresql10dialect中已被编写,如果我设置了它,我将获得classNotFoundException。 我什至尝试使用EntityManager.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是实体类别的ID)exception我甚至尝试过 @ sqlresultSet映射,但仍然错误
org.hibernate.query.results.MissingSqlSelectionException: ResultSet mapping specified selected-alias `variableA` which was not part of the ResultSet
有人可以帮助已经实施此功能的人,我完全陷入了无法找到任何参考的问题上。
我使用的是Google Cloud SQL提供的PostgreSQL 12数据库,需要找到一个仅为版本12的Soln,CANT CANT升级SQL版本。
thanks向阿德里安(Adrian)我能够通过调用该功能使用
Query query=em.createNativeQuery("select * from my_func(:resp)", ResultTable.class).setParameter("resp", Long.valueOf(myvariable));
List<ResultTable> res =query.getResultList();
如果您使用的是
JpaRepository
@Query(value = "SELECT * FROM get_actor_by_id(?1)", nativeQuery = true)
Actor getActorByCallingFunction(final Integer id);