JPA Hibernate调用存储过程 [英] JPA Hibernate calling stored procedure
问题描述
我在Postgres中有一个存储过程.该过程有4个OUT参数.通常,使用JPA无法获得结果.我想做的就是在该过程中使用SELECT查询.
I have a stored procedure in Postgres. That procedure has 4 OUT parameters. Using JPA normally I can't get the results. What I'm trying to do is use a SELECT query with that procedure.
例如,如果我在pgAdmin中尝试查询:
For example If I try in pgAdmin the query:
SELECT * FROM get_results(arg0,arg1等);
SELECT * FROM get_results (arg0, arg1 etc);
我得到一个包含4列的结果行,其中包含来自4个OUT参数的结果.
I get one result row containing 4 columns with the results from the 4 OUT parameters.
但是当我尝试在JPA中使用它时,它会失败.我正在尝试类似的东西:
But When I try to use it in JPA it fails. I'm trying something like:
Query q = em.createNativeQuery("SELECT * FROM get_results (arg0, arg1 etc)");
q.getSingleResult();
但是它抛出一个java.lang.IllegalStateException [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] No such transaction!
有什么建议吗?
推荐答案
JPA 2.1 now support Stored Procedure, read the Java doc here.
示例:
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");
请参见此处.
这篇关于JPA Hibernate调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!