9

休止状態を使用してネイティブ/生のmysqlクエリを実行したいのですが、これがあります:

 sessionFactory.getCurrentSession().createSQLQuery(
       "update table1 set someCounter = someCounter + 1 where id = ?")
     .setParameter(1, someId)
     .executeUpdate();

エラーが発生します:

threw exception [Request processing failed; nested exception is
      org.hibernate.QueryParameterException: Position beyond number of declared ordinal
      parameters. Remember that ordinal parameters are 1-based! Position: 2] 
      with root cause
      org.hibernate.QueryParameterException: Position beyond number of declared ordinal
      parameters. Remember that ordinal parameters are 1-based! Position: 2

ここで何が問題なのですか?

4

4 に答える 4

18

0パラメータ index が から始まるため、 index as を使用します0

sessionFactory.getCurrentSession()
  .createSQLQuery("update table1 set someCounter = someCounter + 1 where id = ?")
  .setParameter(0, someId)
  .executeUpdate();

Hibernate を使用しているため、名前付きパラメーターも使用できます。

sessionFactory.getCurrentSession()
  .createSQLQuery("update table1 set someCounter = someCounter + 1 where id = :id")
  .setParameter("id", someId)
  .executeUpdate();
于 2012-10-26T01:44:35.887 に答える
4

パラメータはゼロから始まるインデックスを使用します。試す:

 sessionFactory.getCurrentSession().createSQLQuery("update table1 set someCounter = someCounter + 1 where id = ?")
                .setParameter(0, someId)
                .executeUpdate();

現在の Hibernate JavaDocs では、setPosition が位置パラメーターのゼロベースのインデックス付けに依存することも指定しています。 http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/Query.html#setParameter%28int,%20java.lang.Object%29

setParameter

Query setParameter(int position,
                   Object val)
                   throws HibernateException

    Bind a value to a JDBC-style query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the given object.

    Parameters:
        position - the position of the parameter in the query string, numbered from 0.
        val - the non-null parameter value 
    Throws:
        HibernateException - if no type could be determined

このドキュメントのパラメーター セクションを確認してください: https://access.redhat.com/knowledge/docs/en-US/JBoss_Enterprise_Web_Server/1.0/html/Hibernate_Core_Reference_Guide/querysql.html#id3043464

setParameter() メソッドが 0 ベースか 1 ベースかについて、いくつかの議論がありました。この混乱は、パラメータが 1 ベースであることに注意して投稿者が受け取った例外によるものですが、JavaDoc ではパラメータがゼロ ベースであると述べられています。私は Hibernate のソース コードを分析し、実際にはゼロ ベースであると考えています。正しいクラスをチェックしたと仮定すると、基になるコードはリストを使用してパラメータ バインド値を格納します。これは、setParameter メソッドが実際にはゼロベースであることを意味します。ソースコードをチェックアウトしてください: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/internal/AbstractQueryImpl.java

于 2012-10-26T01:44:54.403 に答える
0

上記の解決策に基づいて解決できなかった人には、休止状態 (バージョンによっては 3 を使用しています) が実際に間違ったエラーを表示することがあるようです。 hibernate q 言語の構文エラー:

find("from Student s where s.id = :id")

これを次のように変更します。

find("from Student s where s.id = ?")

実際にその問題を解決しました。私の主なポイントは、休止状態がこの例外に誤ってラベルを付けることができるように見える問題の構文も確認する必要があるということだと思います。

于 2019-08-16T15:37:26.327 に答える
0

Positionalパラメータは0notから始まります1

ネイティブ SQL クエリは、位置パラメーターと名前付きパラメーターをサポートします。

1 の代わりに 0 を渡してクエリを更新しますsetParameter(1, someId)

sessionFactory.getCurrentSession().createSQLQuery("update table1 set someCounter = someCounter + 1 where id = ?")
                .setParameter(0, someId)
                .executeUpdate();

リソースパラメータ

于 2012-10-26T04:13:15.933 に答える