1

I have a function in SQL server and I want to call it from hibernate. I have tested these 2 methods but no one was successful. First method:

Session sixSession=HibernateUtil.getSessionFactory().openSession();
Query q2=sixSession.createQuery("from dbo.old_remaining(?)").setParameter("paymentVcode", p_Vcode);
        q2.getNamedParameters();
        List list=sixSession.getNamedQuery("{dbo.old_remaining(?)}").setString(1,"p_Vcode").list();

dbo.old_remaining is my function and p_Vcode is an int

the error is:

unexpected token: ( near line 1, column 23 [from dbo.old_remaining(?)]

Second method:

Float var;
List li=session.getNamedQuery("{dbo.old_remaining(?)}")  
    .setString(1, var).list();

The error is: org.hibernate.MappingException: Named query not known: {dbo.old_remaining(?)}

Please Help me...

2 Answers 2

1

Can propose one work around based on CallableStatement. This standard jdbc statement to call functions:

session.doWork(new Work() {
    public void execute(Connection connection) throws SQLException {
        CallableStatement callable = connection.prepareCall("{? = call dbo.old_remaining(?)}");
        callable.registerOutParameter( 1, Types.FLOAT );
        callable.setString(2, "your string parameter");
        callable.execute();
        float functionResult = callable.getFloat(1);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Error invoking Action using Hibernate Core Session / Transaction injection com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:213)
Yes, change it: "{? = call dbo.old_remaining(?)}". Foget add call.
0

try following code

session.doWork(new Work() {
    public void execute(Connection conn) throws SQLException {
        PreparedStatement pstmt = conn.prepareStatement("SELECT dbo.old_remaining(?)");
        pstmt.setString(1,p_Vcode);
        ResultSet rs = pstmt.executeQuery();
        while( rs.next() )
        System.out.println( rs.getFloat(1) ) ;
    }
});

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.