title: HQL: variable column
body: <p>I'm able to set variable values for "where" restrictives: </p>
  
  <pre><code>Query criteria = session.createQuery(
    "select test.col1, test.col2, test.col3
    "from Test test " +
    "where test.col = :variableValue ");
  criteria.setInteger("variableValue", 10);
  </code></pre>
  
  <p>But is it possible to set variable column like this?</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>This is the result:</p>
  
  <pre><code>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.
      ...
  </code></pre>
  
  <p><strong>UPDATE</strong> (working solution):</p>
  
  <pre><code>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) "
  </code></pre>