简体   繁体   English

Hibernate存储过程调用按顺序而不是通过参数名称映射值

[英]Hibernate Stored procedure call maps values sequentially, not by parameter names

We are calling stored procedure from hibernate using following code: 我们使用以下代码从休眠中调用存储过程:

ProcedureCall procedureCall = session.createStoredProcedureCall("StoredProcedureName", CustomEntity.class);

procedureCall.registerParameter("ParameterName1",String.class,ParameterMode.IN);
        procedureCall.getParameterRegistration("ParameterName1")
                .bindValue("Value1");
procedureCall.registerParameter("ParameterName2",String.class,ParameterMode.IN);
        procedureCall.getParameterRegistration("ParameterName2")
                .bindValue("Value2");

Here, it is expected that it should map parameters with values by name. 在这里,期望它应该按名称映射带有值的参数。 But it looks like it is mapping the values sequentially. 但看起来它正在按顺序映射值。 Because if we put a condition that if "value" is empty then skip the register parameter for that value. 因为如果我们设定一个条件,即如果“ value”为空,则跳过该值的寄存器参数。 So, if we skip any parameter in between, then it maps values of it's next registered parameter with that skipped parameter. 因此,如果我们跳过其间的任何参数,则它将下一个已注册参数的值与该跳过的参数映射。

Ex: 例如:

if (!value1.equals(""))
{
procedureCall.registerParameter("ParameterName1",String.class,ParameterMode.IN);
        procedureCall.getParameterRegistration("ParameterName1")
                .bindValue("Value1");
}

(that means if we get "value1" empty, then we will skip registering that parameter, but in this case, it will map "value2" to "parametername1") (这意味着如果“ value1”为空,则将跳过该参数的注册,但是在这种情况下,它将把“ value2”映射到“ parametername1”)

How it can be avoided? 如何避免呢?

Thanks in advance! 提前致谢!

I recommend you to call your stored procedure using the annotation @NamedNativeQueries in your CustomEntity.class like this: 我建议你使用注释来调用存储过程@NamedNativeQueriesCustomEntity.class这样的:

@NamedNativeQueries(
    {
        @NamedNativeQuery(
              name = "storedProcedureName",
              query = "EXEC StoredProcedureName :ParameterName1, :ParameterName2",
              resultClass = CustomEntity.class
        )
    }
)

Then in your stored procedures caller class you can create a method that receives a HashMap <String, Object> where you will put your parameters by <name, value> tuples. 然后,在存储过程调用程序类中,您可以创建一个接收HashMap <String, Object>的方法,在该方法中,您将按<name, value>元组放置参数。 Remember that you must put all parameters, if you dont need an specific parameter in some situation, send an empty string or a null value. 请记住,必须放置所有参数,如果在某些情况下不需要特定参数,请发送空字符串或空值。 For example the following method: 例如以下方法:

public <T> List<T> execPAmultipleResults(String paNamedQuery, HashMap<String, Object> parameters) {
        NativeQuery nativeQuery = currentSession.getNamedNativeQuery(paNamedQuery);
        if (parameters != null) {
            Set<String> params = parameters.keySet();
            params.stream().forEach((param) -> {
                Object obj = parameters.get(param);
                nativeQuery.setParameter(param, obj);
            });
        }
        return nativeQuery.list();
    }

You can send a null value for the HashMap argument when you execute a stored procedure without input parameters. 当执行不带输入参数的存储过程时,可以为HashMap参数发送一个空值。

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

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