title: Hibernate positional parameters zero based
body: <p>I'm migrating a project from Hibernate 4.2.6 to 5.2.0.</p>
  
  <p>I noticed that for Hibernate 5.2.0, the native queries now require zero based parameter positionning.</p>
  
  <p>According to the <a href="http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf" rel="nofollow">JPA 2.1 Specification</a></p>
  
  <blockquote>
    <h3>3.10.13 Positional Parameters</h3>
    
    <p>Only positional parameter binding and positional access to result items may be portably used for native
    queries, except for stored procedure queries for which named parameters have been defined. <strong>When
    binding the values of positional parameters, the numbering starts as “1”.</strong> It is assumed that for native
    queries the parameters themselves use the SQL syntax (i.e., “?”, rather than “?1”).</p>
  </blockquote>
  
  <p>My understanding of the specification is that even for native queries, the numbering should start with 1.</p>
  
  <p>Now according to Hibernate documentation of <a href="http://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Query.html#setParameter-int-java.lang.Object-" rel="nofollow">Query.setParameter(int, Object)</a>. 
  The position is numbered from 0. In the documentation for Hibernate 4.2 as well as for 5.2.</p>
  
  <p>I made a micro test</p>
  
  <h3>First with Hibernate 4.2.6</h3>
  
  <pre><code>@PersistenceContext 
  private EntityManager entityManager;
  
  
  Query query = entityManager.createNativeQuery("select * from Game g where title = ?");
  query.setParameter(1, GAME_TITLES[0]);
  List list = query.getResultList();
  </code></pre>
  
  <p>This works with hibernate 4.2.6.</p>
  
  <p>The persistence.xml file looks like this</p>
  
  <pre><code>&lt;persistence-unit name="test" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/arquillian&lt;/jta-data-source&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <h3>The same with Hibernate 5.2</h3>
  
  <pre><code>Query query = entityManager.createNativeQuery("select * from Game g where title = ?");
  query.setParameter(0, GAME_TITLES[0]);
  List list = query.getResultList();
  </code></pre>
  
  <p>The only difference is the 0 index in the setParameter.</p>
  
  <p>The persistence.xml is also very similar</p>
  
  <pre><code>&lt;persistence-unit name="test"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/arquillian&lt;/jta-data-source&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>I traced the code in both version. I could find in 4.2.6 where the 1 based index is handeled. I could not find a similar code in version 5.2.</p>
  
  <p>I found a post in the hibernate <a href="https://forums.hibernate.org/viewtopic.php?f=1&amp;t=1025339&amp;p=2467449&amp;hilit=positional+parameters&amp;sid=dd4339194614f24dda7f6cff7ba1e212#p2467449" rel="nofollow">forums</a> dating back to 2009 that:</p>
  
  <blockquote>
    <p>Well, only if you use the JPA-Query-Api the first parameter must have index = 1.
    You are using the Hibernate-Query-Api where the first parameter must have index = 0.</p>
  </blockquote>
  
  <p>Obvioulsy I'm using JPA. 
  So the question is:</p>
  
  <p>Is there a way to configure Hibernate 5.2 to get back the 1 based positional parameter? I would hate to change the code in order not to conform to the specification.</p>