我对JPA有足够的了解,试图使其运行存储过程。
我最终使用带准备好的语句的JDBC。在花了几个小时的徒劳尝试将方钉插入圆孔后,我在15分钟内做到了。我将持久性单元用来获取连接,创建准备好的语句并在完成后将其关闭的同一个jndi数据源称为。
因此,如果您需要从(现在主要是)JPA应用程序运行存储过程(或Postgres函数),这对我有用:
@Stateless@LocalBeanpublic class UserQueryBean implements Serializable { @Resource(mappedName="jdbc/DatabasePool") private javax.sql.DataSource ds; ... public void runRequestCleanup() { String queryString = "SELECT cleanup_function_that_hibernateJPA_choked_on()"; Connection conn = null; PreparedStatement statement = null; try { conn = ds.getConnection(); statement = conn.prepareCall(queryString); statement.executeQuery(); } catch (SQLException ex) { Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex); }finally{ try { statement.close(); conn.close(); } catch (SQLException ex) { Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex); } } // bit of logging pre here } ...}忽略JPA在服务器上运行函数或存储过程的简单功能似乎令人恐惧。特别是除了void或受影响的行数以外不返回任何内容的代码。如果是故意的……则无可奉告。
编辑:添加了关闭连接。



