【问题标题】:call an oracle function with array as parameter via hibernate通过休眠调用以数组为参数的oracle函数
【发布时间】:2018-03-29 11:49:34
【问题描述】:

所以我有一个 oracle functiion,例如:function unbind (ids in id_table)。它需要一组 id 来对我的数据库执行一些更新。

问题是如何运行我的函数以执行更新操作? 我已经尝试过的:

1.Query query = getSession().createSQLQuery("call UNBIND(:ids)"); query.setParameter("ids", myIds); query.executeUpdate();

但我得到 ora-06576 不是有效的函数或过程名称

  1. Query query = getSession().createSQLQuery("execute UNBIND(:ids)"); query.setParameter("ids", myIds); query.executeUpdate();

以 ora-00900 无效的 sql 语句结束

  1. Long [] myArray = movedIds.toArray(new Long[movedIds.size()]); Boolean result = getSession().doReturningWork(new ReturningWork<Boolean>() { @Override public Boolean execute(Connection connection) throws SQLException { CallableStatement callableStatement = connection.prepareCall("{ ? = call UNBIND(:ids)"); callableStatement.registerOutParameter(1, Types.INTEGER); callableStatement.setArray(2, connection.createArrayOf("id_table", myArray)); callableStatement.execute(); return !(callableStatement.getInt(1) == 0); } });

以 java.sql.sqlfeaturenotsupportedexception 不支持的功能结束

应用程序通过 jboss 连接到数据库,所以我想这可能是 p 中的问题。 3?

  1. SELECT UNBIND( id_table (6271789) ) FROM DUAL 不起作用,因为我的函数执行更新...

还有其他方法可以运行直接从 java 代码中获取数组作为参数的函数吗?

【问题讨论】:

标签: oracle hibernate


【解决方案1】:

这是一个简单的例子,这有帮助吗?

import java.sql.*;


public class Class1 {

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(  
                    "jdbc:oracle:thin:@//localhost/orcl","scott","tiger");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

        String query =  "{ ? = call test_func(?) }";
        CallableStatement cs = null;
        try {
            cs = conn.prepareCall(query);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int inVal = 0;

        cs.setInt(2, inVal);

        cs.registerOutParameter(1, oracle.jdbc.OracleTypes.NUMBER);

        cs.executeUpdate();

        int res = cs.getInt(1);

        System.out.println("result is " + res);
    }

}

【讨论】:

  • 这不是一个过程,而是一个函数。我认为您不能通过 begin 调用函数,可以吗?
  • 哦,我的错你看过这个stackoverflow.com/questions/13158212/…我会更新我的帖子
猜你喜欢
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多