0

I am trying to call a stored function in hibernate but I am getting invalid sql exception.

Java code ---

public Integer callSqlBlock(){
        Integer outputValue =1;
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.doWork(new Work() {
                                        @Override
                                        public void execute(Connection conn)
                                                throws SQLException {
                                              CallableStatement stmt = conn.prepareCall("? = call test(?)");
                                              stmt.registerOutParameter(1, Types.INTEGER);
                                              stmt.setString(2, "callIndex");
                                              stmt.execute();
                                              Integer output = stmt.getInt(1);
                                              test(output);

                                           }
                        });
        Connection oracleConnection = getJavaSqlConnectionFromHibernateSession(session);
       // CallableStatement statement = oracleConnection.prepareCall(sql)

        return outputValue;
}

Test stored function in oracle ---

create or replace 
function test( str in varchar2) return number
as 
begin
if str = 'test' then
 return 1;
end if;
return 0;
end;

Please let me know where I am going wrong

0

1 Answer 1

1

You can either change the prepareCallline to use the JDBC escape syntax by enclosing the call in braces, as in

CallableStatement stmt = conn.prepareCall("{? = call test(?)}");

or you can use a PL/SQL block in the call, such as

CallableStatement stmt = conn.prepareCall("begin ? := test(?); end;");

The JDBC syntax is more portable, the PL/SQL block is probably some immeasurably small amount faster.

Best of luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.