2

I am using Hibernate 4.0

I have method in my DAO class like the following where method return Entity class Employees. I am calling an Oracle function which returns sys_refcursor. How can I return entityManager.createNamedQuery in my below method?

Any help is highly appreciated.

@Override
     public Employees getEmployeeRecords(String employeeNumber) {

         <??> = entityManager.createNamedQuery("myfunction");
     return <??>;

     }
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • It seems you are calling a stored procedure from Hibernate, can see the answer [http://stackoverflow.com/questions/14335939/how-to-call-oracle-function-or-procedure-using-hibernate-4-entitymanager](http://stackoverflow.com/questions/14335939/how-to-call-oracle-function-or-procedure-using-hibernate-4-entitymanager) – Genzer Feb 17 '13 at 17:58
  • @Genzer My question is if I am using `Query` as `Query query = entityManager.createNamedQuery("getFunc"); query.getResultList();` How can I cast this to `Employees`? Because my method return type is of `Employees` – Jacob Feb 17 '13 at 18:02
  • 1
    When you declare a `NamedNativeQuery` on your `Employee` entity, specify value of attribute `resultClass = Employee.class`, then when you call `query.getResultList()`, it will return a `List` just like `NamedQuery`. Though you have to explicitly cast it. – Genzer Feb 17 '13 at 18:09
  • @Genzer I have already defined `resultClass = Employees.class`. So now can I cast as `Employees e = (Employees) query.getSingleResult();` ? – Jacob Feb 17 '13 at 18:19
  • Yes. Notice that you also need to implement the stored procedure to return value(s) which mapped to `Employee` entity. – Genzer Feb 17 '13 at 18:24
  • @Genzer Thanks it worked. Can you post as an answer, I will be glad to accept it. – Jacob Feb 17 '13 at 18:50
  • Actually I just gave you the link to the real solution. So please go to the link and upvote the answer instead. – Genzer Feb 18 '13 at 06:43
  • @Genzer Myself added that answer in the link you have provided. :-) – Jacob Feb 18 '13 at 06:45
  • Wow, I didn't notice that. LOL! – Genzer Feb 18 '13 at 06:49

2 Answers2

5

see i do like this

Query query = em.createQuery("SELECT e FROM Employee e WHERE e.name = :name ");
query.setParameter("name", name);

List<Employee> results = query.getResultList();
return results;

here i am returning list of Employee type , if you want to send one object then do like this

return results.get(0);

Regards

Anshul

anshulkatta
  • 2,044
  • 22
  • 30
2

You might be interested in "entityManager.createNativeQuery("SQL Syntax query") where is can execute SQL syntax query. The return datatype would be "ResultSet".

ResultSet result = entityManager.createNativeQuery("Your_SQL_function").getSingleResult();

On the other hand, 'entityManager.createNamedQuery("defined-query-name")' should have its query defined ahead earlier than your code execution (either in persistance config, or class annotation). example: on top your class

@NamedQuery(id="findFirstEmployee" query="from Employees e where id = 1")

your code should look like:

public Employees getEmployeeRecords(String employeeNumber) {

    Emloyee result = entityManager.createNamedQuery("findFirstEmployee").getSingleResult();
 return result;

References: http://www.oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php

devBinnooh
  • 611
  • 3
  • 12