【问题标题】:java.sql.SQLException: Parameter #9 has not been setjava.sql.SQLException: 参数 #9 尚未设置
【发布时间】:2013-06-13 15:10:50
【问题描述】:

您好,我正在尝试从休眠状态执行 mssql 存储过程。程序有 8 个输入参数,没有输出。但是我得到 java.sql.SQLException: Parameter #9 has not be set whuli execution.

<sql-query name="insertMyData" callable="true">
        { ? = call InsertMyData(?,?,?,?,?,?,?,?) }
</sql-query>

Java 调用

Query query = m_entityManager.createNamedQuery("insertMyData");
        query.setParameter(1, transaction.getGuid());
        query.setParameter(2, new Date());

........指定的其他参数

存储过程

CREATE PROC dbo.insertMyData 
    @ID uniqueidentifier, 
    ...... 7 more parameters
AS 
BEGIN
    INSERT INTO dbo.TestData VALUES (
        @ID,
           ........ 7 more parameters

    )
END

【问题讨论】:

  • 也许删除call之前的? =
  • 您的查询中有九个? ...九个参数。
  • 在这种情况下,我在 org.hibernate.loader.custom.CustomLoader$Metadata.(CustomLoader.java:544) 在 org.hibernate.loader.custom 处得到了 NPE java.lang.NullPointerException .CustomLoader.autoDiscoverTypes(CustomLoader.java:517) 在 org.hibernate.loader.Loader.getResultSet(Loader.java:1817) 在 org.hibernate.loader.Loader.doQuery(Loader.java:697) 在 org.hibernate。 loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.doList(Loader.java:2228) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125) .... ........

标签: java sql-server hibernate


【解决方案1】:

我之前的建议很糟糕:

根据https://forum.hibernate.org/viewtopic.php?f=1&t=986612

另一个有同样问题的人,通过删除“?=”解决了这个问题,因为没有为查询定义“返回”。我建议你也试试。

希望这会有所帮助。

【讨论】:

  • 我想第一个'?'是返回值。但是我怎样才能在代码中说明它呢?也许我必须在映射文件中添加 smth?
  • 您在构建后是否在“查询”上调用 executeUpdate?你能在你的问题中加入更多代码吗?
  • 您可以尝试的另一件事是删除“?=”并将所有 ?,... 替换为名称参数。反正这样更好。它看起来像 - 调用 InsertMyData(:uniqueidentifier, :param2, ....) 并在您的代码中使用 .setParameter("uniqueidentifier", transaction.getGuid())。还要确保您正在调用 executeUpdate 而不是列表。
  • 我是通过 query.getResultList() 调用的
  • 我按照你建议的方式做了,但我现在得到了 java.lang.IllegalArgumentException: callable not yet supported for native queries at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java: 171) 在 org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1190) 在 org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:357)
【解决方案2】:

似乎您忽略了第一个 ? 并且在过程调用中只调用了 setParameter 8 次 ?

这是你应该如何设置你的第一个参数:

statement.registerOutParameter(1, Types.VARCHAR); //Assuming statement is your CallableStatement and return type of procedure is Varchar

【讨论】:

  • 我应该为第一个参数设置什么? (我想是@return_value)
  • 更新了您上述问题的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2011-07-15
  • 1970-01-01
  • 2020-05-18
  • 2020-01-10
  • 1970-01-01
  • 2013-04-16
相关资源
最近更新 更多