如何在存储过程jpa 2.1

时间:2017-08-06 09:11:23

标签: spring jpa

我正在使用JPA 2.1来调用存储过程。我的存储过程中有8个IN参数。其中一个参数(inRegionId)可能为null。

当存储过程中的region id为null时,我遇到以下异常:

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

以下是命名的Query参数:

 @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]

有人可以帮助我解决问题。

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题。我最终只是使用原生查询

@Query(value = "{call AddUpdateCurrentLocation (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)}", nativeQuery = true)
void AddUpdateCurrentLocation(int inSubscribedUserId, double inLatitude, double inLongitude, String inLocationAccuracy, String inLocationName, boolean inISExactLocation, int inCountryId, Integer inRegionId);