简体   繁体   English

如何在休眠映射文件中定义存储过程的调用,然后从Java(MSSQL服务器)调用它

[英]How to define call of stored procedure in hibernate mapping file and then call it from java (mssql server)

I need to call stored procedure in java code ( entityManager is available only). 我需要用Java代码调用存储过程(只有entityManager可用)。 This stored procedure should be defined in some hbm.xml file. 此存储过程应在某些hbm.xml文件中定义。

I didn't find any solution how to do it. 我没有找到解决方案。

In hibernate documentation I found that it should be declared in this way. 在休眠文档中,我发现应该以这种方式声明它。

<sql-query name="InsertTestCaseData" callable="true">
    { ? = call dbo.InsertTestCaseData(:ID) }
</sql-query>

This procedure returns only result code ( mssql server ). 此过程仅返回结果代码( mssql server )。 how should I describe it in mapping file? 我应该如何在映射文件中描述它? And how to handle if this procedure returns some value of some column, should I define <return-property> in hbm file? 以及如何处理此过程返回某些列的某些值,我应该在hbm文件中定义<return-property>吗?

I have only entityManager object. 我只有entityManager对象。 there is no method like getNamedQuery() . 没有像getNamedQuery()这样的方法。

What method could be used instead? 可以使用哪种方法代替?

And where mapping file should be located? 映射文件应位于何处? Could it be separate file with queries only? 可以是仅包含查询的单独文件吗?

There are few examples for you here but here is how you would call a stored procedure using entity 这里有几个示例供您参考但这是您如何使用实体调用存储过程的示例

// This stored procedure returns a result set and has one input parameter.
@NamedStoredProcedureQuery(
    name = "ReadAddressById",
    resultClasses = Address.class,
    procedureName = "READ_ADDRESS",
    parameters = {
        @StoredProcedureParameter(mode=IN, name="P_ADDRESS_ID", type=Long.class)
    }
)
@Entity
public class Address {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM