私は「」restrictivesための変数値を設定することができるよ:HQL:変数列
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where test.col = :variableValue ");
criteria.setInteger("variableValue", 10);
をしかし、このように変数の列を設定することが可能ですか?
String variableColumn = "test.col1";
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where :variableColumn = :variableValue ");
criteria.setInteger("variableValue", 10);
criteria.setString("variableColumn", variableColumn);
これが結果です:
Exception in thread "main" Hibernate: select .... where ?=? ...
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
...
at _test.TestCriteria.main(TestCriteria.java:44)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value 'test.col1' to data type int.
...
はUPDATE(使用液):
Query criteria = session.createQuery(
"select test.col1, test.col2, test.col3
"from Test test " +
"where (:value1 is null OR test.col1 = :value1) AND
(:value2 is null OR test.col2 = :value2) "
ありがとうございますが、 'メソッドの置き換え(String、String)はQuery型'のために定義されていません。私は誤って別の方法でドキュメントを見つけました。私は数分で私の質問を更新します。 – gaffcz
私の提案したコードで述べたように、 'query'はHQLを保持する文字列です。 – nobeh
Omg、それは動作し、それは私のソリューションよりもクリーンであるようです、もう一度ありがとう! – gaffcz