[英]How to call ms sql store procedure using hibernate 4.0
i am working with store procedure ie 我正在使用存储过程,即
CREATE PROCEDURE test
(
@INPUTPARAM INT,
@OUTPUTPARAM VARCHAR(20)
)
AS
SELECT @OUTPUTPARAM=S.NAME+','+D.NAME
FROM STUDENT S,DEPARTMENT D
WHERE S.DEPTID=D.DEPARTID AND D.DEPARTID=@INPUTPARAM
BEGIN
END
how to get out parameter from java class using hibernate please share code example 如何使用休眠从Java类中获取参数,请共享代码示例
CREATE PROCEDURE test
(
@INPUTPARAM INT,
@OUTPUTPARAM VARCHAR(20) OUTPUT --<-- You need to use key word "OUTPUT" here
)
AS
BEGIN
SELECT @OUTPUTPARAM = S.NAME + ',' + D.NAME
FROM STUDENT S INNER JOIN DEPARTMENT D
ON S.DEPTID = D.DEPARTID --<-- Use New Syntax of join with On Clause
WHERE D.DEPARTID = @INPUTPARAM
END
EXECUTE Procedure 执行程序
DECLARE @Var VARCHAR(20);
EXECUTE dbo.test
@INPUTPARAM = 1
@OUTPUTPARAM = @Var OUTPUT --<-- use OUTPUT key word here as well
SELECT @Var
The only way to do it is using em.createNativeQuery
and talk directly with you DB Server in SQL. 唯一的方法是使用em.createNativeQuery
并直接在SQL中与您的DB Server对话。
Update: 更新:
Here is, how it could be done: 这是如何完成的:
//get connection from em
Session session = (Session)em.getDelegate();
Connection conn = session.connection();
//Native SQL
final CallableStatement callStmt = conn.prepareCall("{call your.function(?)}");
callStmt.setLong(1, documentId);
callStmt.execute();
if (callStmt.getMoreResults()) {
ResultSet resSet = cStmt.getResultSet();
//Do something good with you result
resSet.close();
}
callStmt.close();
//Don't know if calling conn.close(); is a good idea. Since the session owns it.
Hope that helps a little. 希望能有所帮助。
Notes: 笔记:
If you are using JPA 2.0, you can get the session using 如果您使用的是JPA 2.0,则可以使用
Connection conn = em.unwrap(Session.class).connection();
If you are using JPA 2.1, you can call stored procedures directly 如果使用的是JPA 2.1,则可以直接调用存储过程
StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ReadAddressById");
query.setParameter("P_ADDRESS_ID", 12345);
List<Address> result = query.getResultList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.