java 如何获取 Hibernate entityManager.createNamedQuery 结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14924006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:52:26  来源:igfitidea点击:

How to get Hibernate entityManager.createNamedQuery results

javahibernatejpajpa-2.0

提问by J?cob

I am using Hibernate 4.0

我正在使用休眠 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.createNamedQueryin my below method?

我的 DAO 类中有如下方法,其中方法返回实体类Employees。我正在调用一个 Oracle 函数,它返回sys_refcursor. 我怎样才能entityManager.createNamedQuery在下面的方法中返回?

Any help is highly appreciated.

任何帮助都受到高度赞赏。

@Override
     public Employees getEmployeeRecords(String employeeNumber) {

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

     }

回答by anshulkatta

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

安舒尔

回答by devBinnooh

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

您可能对“entityManager.createNativeQuery("SQL Syntax query") 感兴趣,其中可以执行 SQL 语法查询。返回数据类型为“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

另一方面,'entityManager.createNamedQuery("defined-query-name")' 应该在代码执行之前(在持久性配置或类注释中)提前定义其查询。示例:在您的班级之上

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

@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

参考资料:http: //www.oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php