spring-boot - 使用spring数据jpa在存储过程中读取ref游标作为输出参数返回null
问题描述
我正在尝试使用 spring data jpa 和 spring boot 读取 oracle 存储过程中的 refcursor,存储过程成功运行,但对返回 List 的引用始终为空。
我尝试使用 Hibernate 作为 JPA 提供程序和 Eclipse Link 没有成功,下面是 oracle ddl 和 java 代码
Oracle 表(我在表中填写了一些示例数据)
CREATE TABLE role
(id NUMBER(10,0),
name VARCHAR2(255 CHAR))
Oracle 存储过程
PROCEDURE collect_roles (role_list_o OUT SYS_REFCURSOR) IS
ex EXCEPTION;
BEGIN
OPEN role_list_o FOR SELECT id, name FROM role;
END;
pom.xml(oracle jdbc 驱动程序依赖项从本地 ojdbc7.jar 安装在 maven 中)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gridapp</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Oracle JDBC driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序属性
spring.main.banner-mode=off
# Oracle settings
spring.datasource.url=jdbc:ora_string_conection
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
# HikariCP settings
# spring.datasource.hikari.*
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5
角色.java
package hello;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedStoredProcedureQueries;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;
import javax.persistence.Table;
import org.springframework.data.domain.Sort.Direction;
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "findRolesViaProcedure",
procedureName = "wfactura.collect_roles",
resultClasses = Role.class,
parameters = {
@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, name = "role_list_o", type = Void.class)
})})
@Entity
public class Role {
@Id @GeneratedValue//
@Column(name="ID")
private Long id;
@Column(name="NAME")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
RoleRepository.java
package hello;
import java.io.Serializable;
import java.util.List;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.CrudRepository;
interface RoleRepository extends CrudRepository<Role, Serializable> {
@Procedure(name = "findRolesViaProcedure")
List<Role> findRolesViaProcedure();
}
我用下一行调用存储过程
List<Role> roles = repo.findRolesViaProcedure();
我修改了在另一个表中插入值的存储过程,我意识到执行成功了,但是即使表中有数据,列表角色也是空的。
有人可以帮我吗?
解决方案
change type=Void.class to type=ResultSet.Class
推荐阅读
- reactjs - Reactjs TypeError:无法读取未定义的属性“城市”
- jquery - 如何在不全局延迟的情况下将变量传递给 jQuery 中的另一个事件处理程序?
- sql-server - 将 SQL Server 迁移到新的 AWS Aurora DB
- vue.js - 如何让 Vue CLI 使用带有热重载的外部 HTML 文件?
- node.js - 无法在 node.js 中获取/
- azure - VM classic 不断关闭并自行释放
- php - PHP 数据重载
- firebase - 在 Firestore 更新时更新 Algolia 索引记录
- r - 多列之间的R多热编码
- html - 您如何使用 HTML5 视频标签流式传输大型 Google 驱动器视频