【问题标题】:JPA 2.1 - @NamedStoredProcedureQuery - ParameterMode.IN - null value gives errorJPA 2.1 - @NamedStoredProcedureQuery - ParameterMode.IN - 空值给出错误
【发布时间】:2015-10-22 16:22:46
【问题描述】:

我正在尝试使用 JPA 2.1 @NamedStoredProcedureQuery 调用存储过程。 我的实体类中有以下注释。

问题是如果我将模式声明为 ParameterMode.IN,我将无法传递空值。只有当我将它声明为 ParameterMode.INOUT 时才能传递空值。

实体

@NamedStoredProcedureQuery(name = "employee", procedureName = "myProcedure", resultClasses = Employee.class,  parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "EMPLOYEE_PHONE", type = String.class),
        @StoredProcedureParameter(mode = ParameterMode.INOUT, name = "CITY", type = String.class)

存储库

        public Long createOrUpdateEmployee(Employee employee) throws EbaDataException {

            StoredProcedureQuery query = entityManager.createNamedStoredProcedureQuery("employee");

            query.setParameter("EMPLOYEE_PHONE", employee.getName());
            query.setParameter("CITY", employee.getCity());         
            query.execute();

        }

例如,如果我使用 ParameterMode.IN 为索引 2 (CITY) 传递 null,则会收到此异常。

Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 2

【问题讨论】:

    标签: java hibernate jpa stored-procedures jpa-2.1


    【解决方案1】:

    根据topic,您不能将空值作为参数传递。

    我将其解释为 JPA(特别是 Hibernate)根本不支持设置空参数。看起来他们正在努力支持默认参数值,而不是在适当的时候替换空值。他们选择支持前者。看起来那些需要支持后者(可为空值)的人必须使用 java.sql.CallableStatement

    您也可以随时尝试使用空字符串而不是 null,我认为它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2013-07-02
      • 2015-07-06
      • 2021-07-06
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多