【问题标题】:JPA - Invoking stored procedure with BLOB parameterJPA - 使用 BLOB 参数调用存储过程
【发布时间】:2014-10-20 15:47:03
【问题描述】:

我正在使用 JPA 2.0。我想调用一个 Oracle 存储过程。其中一个参数是 BLOB。

PROCEDURE test(file IN BLOB);

我正在尝试使用下一个代码调用它:

byte[] bytes = ...;
Query query = em.createNativeQuery("{ CALL test(?) }");
query.setParameter(1, bytes);
query.executeUpdate();

我收到下一个错误:

PLS-00306:调用“test”时参数的数量或类型错误

问题是如何传递 blob 参数,因为我可以在没有 blob 参数的情况下调用其他存储过程。

谢谢

【问题讨论】:

    标签: jpa stored-procedures blob


    【解决方案1】:

    试试这个:

    @Override
    public void insertData(DataSource ds, byte[] data) throws SQLException {
        Connection conn = null;
        CallableStatement cstmt = null;
    
        try {
            conn = ds.getConnection();
    
            cstmt = conn.prepareCall("{call test (?)}");
            Blob blob = conn.createBlob();
            blob.setBytes(1, data);
    
            cstmt.setBlob(1, blob);
    
            cstmt.execute();
        } finally {
            if (cstmt != null) {
                cstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }
    

    【讨论】:

    • 感谢您的 JDBC 解决方案,但我想使用 JPA 框架。
    • 我也遇到过同样的问题。但是我使用注解@NamedStoredProcedureQuery。而且我也想用JPA框架解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多