简体   繁体   English

如何从plsql中的存储过程获取java代码中的结果集

[英]How to get a resultset in java code, from a stored procedure in plsql

I am using MVC pattern I have two tables : Employee and Address 我正在使用MVC模式我有两个表:Employee和Address

say, Employee is like 说,员工就像

-------------------
Id  | Name   | DeptId
-------------------
101 | Jake   | 501  
102 | Donald | 502

and I have one Department table like 我有一个像这样的部门表

-----------------------------
DeptId | Name | Description
-----------------------------
501    | IT   | software assistance  
502    | HR   | Human resources

Now since I am using MVC these tables are mapped to classes like 现在,因为我使用MVC,这些表被映射到类

@Table(name="Employee")
Class Employee{
   @Id
   @Column(name="Id")
   private Long id;

   @Column(name="Name")
   private String name;

   @Column(name="DeptId")
   private Long deptId;

   @ManyToOne
   @JoinColumn(name="DeptId", referencedColumnName="id", insertable=false,updatable=false)
   private Department dept;

   //getters and setters go here
}

and the other class Department (mapped to Department table) 和其他类部门(映射到部门表)

@Table(name="Department")
    Class Department{
       @Id
       @Column(name="Id")
       private Long id;

       @Column(name="Name")
       private String name;

       @Column(name="Description")
       private String description;

       //getters and setters go here
    }

notice that Employee class has reference to an object of Department class. 请注意,Employee类引用了Department类的对象。 This @ManyToOne and @JoinColumn annotations helps us in automatically retrieving corresponding department object along with an employee object 这个@ManyToOne和@JoinColumn注释帮助我们自动检索相应的部门对象以及员工对象

Its easy with queries directly in code but how can this be done if I am to use only procedures or functions in my code I have tried different methods , but it doesn't seem to help 它很容易直接在代码中查询,但如果我只在我的代码中使用过程或函数我怎么能这样做我尝试了不同的方法,但它似乎没有帮助

Sometimes I get error something like Cannot return resultset from a stored procedure in oracle 10g 有时我得到的错误类似于Cannot return resultset from a stored procedure in oracle 10g

Can anyone please clarify. 任何人都可以澄清一下。 Also I have to use JNDI 我也必须使用JNDI

Can I get my result from the procedure/function in a way that it returns me List<Employee> (not a raw resultset which I myself have to sort out into objects) . 我可以从过程/函数中获取我的结果,它返回给我List<Employee> (不是我自己必须整理到对象中的原始结果集)。 It should be possible using hibernate no ? 应该可以使用hibernate no吗?

thanks 谢谢

Your PLSQL needs to return a ref cursor . 您的PLSQL需要返回引用游标

JPA 2.1 has support for mapping CallableStatement out cursors to Entities. JPA 2.1支持将CallableStatement将游标映射到实体。 See this answer . 看到这个答案

I am writing pseudocode ( Not the working example.) But it should solve your problem. 我正在写伪代码(不是工作示例。)但它应该解决你的问题。

Oracle 神谕

CREATE OR REPLACE PROCEDURE getEmployees(deptName varchar(20))
BEGIN
   SELECT * FROM Employee where DeptId in ( select DeptId from Department where Name =deptName );

END;
/

Hibernate 过冬

Query query = session.createSQLQuery(
    "CALL GetStocks(:getEmployees)")
    .addEntity(Employee.class)
    .setParameter("deptName", "Production");

List result = query.list();
for(int i=0; i<result.size(); i++){
    Employee emp = (Employee)result.get(i);
    System.out.println(emp.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM