1

SQL Serverに関数があり、休止状態から呼び出したい。私はこれら2つの方法をテストしましたが、誰も成功しませんでした。最初の方法:

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は私の関数であり、p_Vcodeはintです。

エラーは次のとおりです。

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

2番目の方法:

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

エラーは次のとおりです:org.hibernate.MappingException:名前付きクエリが不明です:{dbo.old_remaining(?)}

私を助けてください...

4

2 に答える 2

1

CallableStatementに基づいて1つの回避策を提案できます。関数を呼び出すためのこの標準のjdbcステートメント:

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);
    }
});
于 2012-05-23T06:38:25.793 に答える
0

次のコードを試してください

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) ) ;
    }
});
于 2012-05-23T11:02:28.117 に答える