简体   繁体   English

JPA Hibernate调用存储过程

[英]JPA Hibernate calling stored procedure

I have a stored procedure in Postgres. 我在Postgres中有一个存储过程。 That procedure has 4 OUT parameters. 该过程有4个OUT参数。 Using JPA normally I can't get the results. 通常,使用JPA无法获得结果。 What I'm trying to do is use a SELECT query with that procedure. 我想做的是对该过程使用SELECT查询。

For example If I try in pgAdmin the query: 例如,如果我在pgAdmin中尝试查询:

SELECT * FROM get_results (arg0, arg1 etc); SELECT * FROM get_results(arg0,arg1等);

I get one result row containing 4 columns with the results from the 4 OUT parameters. 我得到一个包含4列的结果行,其中包含4个OUT参数的结果。

But When I try to use it in JPA it fails. 但是,当我尝试在JPA中使用它时,它将失败。 I'm trying something like: 我正在尝试类似:

Query q = em.createNativeQuery("SELECT * FROM get_results (arg0, arg1 etc)");
q.getSingleResult();

But it throws an java.lang.IllegalStateException [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] No such transaction! 但是它抛出java.lang.IllegalStateException [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] No such transaction!

Any suggestions? 有什么建议么?

JPA 2.1 now support Stored Procedure, read the Java doc here . JPA 2.1现在支持存储过程,请在此处阅读Java文档。

Example: 例:

StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("sales_tax");
// set parameters
storedProcedure.registerStoredProcedureParameter("subtotal", Double.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("tax", Double.class, ParameterMode.OUT);
storedProcedure.setParameter("subtotal", 1f);
// execute SP
storedProcedure.execute();
// get result
Double tax = (Double)storedProcedure.getOutputParameterValue("tax");

See detailed example here . 请参阅此处的详细示例。

use below code co call procedure using hibernate. 使用下面的代码使用hibernate共同调用过程。

Query query = session.getNamedQuery("ProcedureName")
    .setParameter(parameterName,value);
    .setParameter(parameterName,value);
    .setParameter(parameterName,value);
    .setParameter(parameterName,value);

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

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