我是JPA的新手,我正在尝试调用DB2存储过程。但是,据我所知,生成的SQL是错误的,因此失败了。
以下是我如何设置存储过程:
@NamedStoredProcedureQuery(name = "getApplVars", procedureName = "prc_get_all_appl_var", resultClasses = ApplVars.class,
parameters = {@StoredProcedureParameter(mode = ParameterMode.IN, name = "ip_cus_set_c", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "ip_cps_ver_n", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "op_sqlstate", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "op_sqlcode", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "op_msg", type = String.class)})
以下是我调用存储过程的代码:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DB1");
EntityManager em = emf.createEntityManager();
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("getApplVars");
spq.setParameter("ip_cus_set_c","CONS");
spq.setParameter("ip_cps_ver_n",500);
spq.getResultList();
根据我在介绍代码时看到的内容以及日志中的内容,这是我看到的sql语句。
CALL prc_get_all_appl_var(ip_cus_set_c =?,ip_cps_ver_n = ?, op_sqlstate = ?, op_sqlcode = ?, op_msg =?)
但它应该是
CALL prc_get_all_appl_var(" CONS",500,?,?,?)
这是堆栈跟踪。
[10/23/13 13:43:15:703 MDT] 00000036 SystemOut O [EL警告]:2013-10-23 13:43:15.698 - UnitOfWork(1084751114) - 异常[EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC ==; ppl_var(ip_cus_set_c; +,DRIVER = 4.17.28 错误代码:-104 调用:CALL prc_get_all_appl_var(ip_cus_set_c =?,ip_cps_ver_n = ?, op_sqlstate = ?, op_sqlcode =?,op_msg =?) bind => [5个参数绑定] 查询:ResultSetMappingQuery(name =" getApplVars") [10/23/13 13:43:15:707 MDT] 00000036 SystemOut O e:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC ==; ppl_var(ip_cus_set_c; +,DRIVER = 4.17.28 错误代码:-104 调用:CALL prc_get_all_appl_var(ip_cus_set_c =?,ip_cps_ver_n = ?, op_sqlstate = ?, op_sqlcode =?,op_msg =?) bind => [5个参数绑定] 查询:ResultSetMappingQuery(name =" getApplVars") [10/23/13 13:43:15:707 MDT] 00000036 SystemErr R javax.persistence.PersistenceException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence。 exceptions.DatabaseException 内部异常:com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC ==; ppl_var(ip_cus_set_c; +,DRIVER = 4.17.28 错误代码:-104 调用:CALL prc_get_all_appl_var(ip_cus_set_c =?,ip_cps_ver_n = ?, op_sqlstate = ?, op_sqlcode =?,op_msg =?) bind => [5个参数绑定] 查询:ResultSetMappingQuery(name =" getApplVars") [10/23/13 13:43:15:735 MDT] 00000036 SystemErr R at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378) [10/23/13 13:43:15:735 MDT] 00000036 SystemErr R at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260) [10/23/13 13:43:15:735 MDT] 00000036 SystemErr R at or.e.eclipse.persistence.internal.jpa.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:316) [10/23/13 13:43:15:735 MDT] 00000036 SystemErr R at org.eclipse.persistence.internal.jpa.StoredProcedureQueryImpl.getResultList(StoredProcedureQueryImpl.java:548) [10/23/13 13:43:15:736 MDT] 00000036 SystemErr R at cpstools.CPSToolsServlet.doGet(CPSToolsServlet.java:62) [10/23/13 13:43:15:736 MDT] 00000036 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:575) [10/23/13 13:43:15:736 MDT] 00000036 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) [10/23/13 13:43:15:737 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1240) [10/23/13 13:43:15:737 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:760) [10/23/13 13:43:15:737 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443) [10/23/13 13:43:15:738 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1033) [10/23/13 13:43:15:738 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4499) [10/23/13 13:43:15:738 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost $ 2.handleRequest(DynamicVirtualHost.java:282) [10/23/13 13:43:15:738 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:954) [10/23/13 13:43:15:738 MDT] 00000036 SystemErr R at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost $ 2.run(DynamicVirtualHost.java:252) [10/23/13 13:43:15:739 MDT] 00000036 SystemErr R at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink $ TaskWrapper.run(HttpDispatcherLink.java:584) [10/23/13 13:43:15:739 MDT] 00000036 SystemErr R at com.ibm.ws.threading.internal.Worker.executeWork(Worker.java:439) [10/23/13 13:43:15:739 MDT] 00000036 SystemErr R at com.ibm.ws.threading.internal.Worker.run(Worker.java:421) [10/23/13 13:43:15:739 MDT] 00000036 SystemErr R at java.lang.Thread.run(Thread.java:744) [10/23/13 13:43:15:740 MDT] 00000036 SystemErr R引起:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.DatabaseException