title: Hibernate - Cannot set named parameter on SQLQuery
body: <p>I am unable to set parameters on a native SQL query using Hibernate; here is the code:</p>
  
  <pre><code>Session sess = HibernateUtil.getCurrentSession();
  SQLQuery query = sess.createSQLQuery("SELECT * FROM :table WHERE :field = ':value'");       
  query
      .addEntity(klass)
      .setString("table", table)
      .setString("field", field)
      .setString("value", value)
      .uniqueResult();
  </code></pre>
  
  <p>I am receiving an <code>IllegalArgumentException</code>; here is the top of the trace:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Parameter value does not exist as a named parameter in [SELECT * FROM :table WHERE :field = ':value']
      at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:409)
      at org.hibernate.internal.AbstractQueryImpl.setString(AbstractQueryImpl.java:696)
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Trying to use positional parameters, I still get an error:</p>
  
  <pre><code>    SQLQuery query = sess.createSQLQuery("SELECT * FROM ? WHERE ? = '?'");
  
      query
          .addEntity(klass)
          .setParameter(1, table)
          .setParameter(2, field)
          .setParameter(3, value)
          .uniqueResult();
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 3
  at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:80)
  at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:86)
  </code></pre>