1

我在 SQL Server 中有一个函数,我想从休眠中调用它。我已经测试了这两种方法,但没有一种方法成功。第一种方法:

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(?)]

第二种方法:

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

错误是:org.hibernate.MappingException:命名查询未知:{dbo.old_remaining(?)}

请帮我...

4

2 回答 2

1

可以基于 CallableStatement 提出一种解决方法。这个标准的 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 回答