【发布时间】:2017-08-06 09:11:22
【问题描述】:
我正在使用 JPA 2.1 来调用存储过程。我的存储过程中有 8 个 IN 参数。参数之一(inRegionId)可能为空。
当存储过程中的区域 id 变为空时,我得到以下异常:
Caused by: java.sql.SQLException: No value specified for parameter 8
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2176)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2100)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:931)
at com.mysql.jdbc.CallableStatement.execute(CallableStatement.java:859)
at org.hibernate.result.internal.OutputsImpl.<init>(OutputsImpl.java:69)
... 98 more
下面是命名的查询参数:
@NamedStoredProcedureQuery(name = "AddUpdateCurrentLocation", procedureName =
"AddUpdateCurrentLocation", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class, name
= "inSubscribedUserId"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = Double.class, name
= "inLatitude"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = Double.class, name
= "inLongitude"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = String.class, name
= "inLocationAccuracy"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = String.class, name
= "inLocationName"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = Boolean.class, name
= "inISExactLocation"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class, name
= "inCountryId"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class, name
= "inRegionId")
}
)
下面是调用存储过程的代码:
StoredProcedureQuery query = em.createStoredProcedureQuery("AddUpdateCurrentLocation");
query.registerStoredProcedureParameter("inSubscribedUserId", Integer.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inLatitude", Double.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inLongitude", Double.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inLocationAccuracy", String.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inLocationName", String.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inISExactLocation", Boolean.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inCountryId", Integer.class, ParameterMode.IN);
query.registerStoredProcedureParameter("inRegionId", Integer.class, ParameterMode.IN);
query.setParameter("inSubscribedUserId", userCurrentLocation.getSubscribedUser().getId());
query.setParameter("inLatitude", userCurrentLocation.getLatitude());
query.setParameter("inLongitude", userCurrentLocation.getLongitude());
query.setParameter("inLocationAccuracy", userCurrentLocation.getLocationAccuracy());
query.setParameter("inLocationName", userCurrentLocation.getLocationName());
query.setParameter("inISExactLocation", userCurrentLocation.getIsExactLocation());
query.setParameter("inCountryId", userCurrentLocation.getCountry().getId());
if(userCurrentLocation.getRegion()!=null){
query.setParameter("inRegionId", userCurrentLocation.getRegion().getId());
}else{
query.setParameter("inRegionId", null);
}
我尝试将下面的提示属性一一设置,但报错:
query.setHint("spring.jpa.properties.hibernate.proc.param_null_passing", true);
query.setHint("hibernate.proc.param_null_passing", true);
HHH000121: Ignoring unrecognized query hint [spring.jpa.properties.hibernate.proc.param_null_passing]
谁能帮我解决这个问题。
【问题讨论】:
-
如果你调用
setParameter传入 null 那么你的 JPA 提供者似乎没有正确地将它传递给CallableStatement。如果是这样,请在他们身上提出一个错误 -
谁能帮我找出在存储过程中传递空值的替代方法。