Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

@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. This @ManyToOne and @JoinColumn annotations helps us in automatically retrieving corresponding department object along with an employee object

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

Can anyone please clarify. Also I have to use 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) . It should be possible using hibernate no ?

thanks

like image 326
Sarabjeet Avatar asked Feb 21 '16 02:02

Sarabjeet


1 Answers

Your PLSQL needs to return a ref cursor.

JPA 2.1 has support for mapping CallableStatement out cursors to Entities. See this answer.

like image 60
MarkOfHall Avatar answered Nov 15 '22 05:11

MarkOfHall