title: How to run a Hibernate NativeQuery in a type-safe manner instead of returning an Object[]
body: <p>I'm migrating an application from Hibernate 4.x to Hibernate 5.3.6. The application has queries like this:</p>
  
  <pre><code>SQLQuery query = getSession().createSQLQuery("SELECT a.a, a.b, a.c FROM aTable");
  </code></pre>
  
  <p>As the method <a href="https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/query/QueryProducer.html#createSQLQuery-java.lang.String-" rel="noreferrer">createSQLQuery</a> has been deprecated, I first replaced the method call with the alternative suggested in the Hibernate Javadoc, namely using createNativeQuery:</p>
  
  <pre><code>NativeQuery query = getSession().createNativeQuery("SELECT a.a, a.b, a.c FROM aTable");
  </code></pre>
  
  <p>The problem with this is that it produces a compiler warning "NativeQuery is a raw type. References to generic type NativeQuery should be parameterized". Furthermore, of course I'd like to benefit from typed queries, now that they are available. So I changed the query to </p>
  
  <pre><code>NativeQuery&lt;Object[]&gt; query = getSession().createNativeQuery("SELECT a.a, a.b, a.c FROM aTable", Object[].class);
  </code></pre>
  
  <p>Now the problem is that executing the query with</p>
  
  <pre><code>List&lt;Object[]&gt; retList = query.list();
  </code></pre>
  
  <p>produces the error</p>
  
  <blockquote>
    <p>javax.persistence.PersistenceException:
    org.hibernate.MappingException: Unknown entity: [Ljava.lang.Object;</p>
  </blockquote>
  
  <p>Researching the problem <a href="https://stackoverflow.com/a/19051238/76777">seems to indicate</a> that it is not possible to use non-mapped entities when using typed native queries (which seems like a serious and unnecessary restriction, but I digress here). </p>
  
  <p><strong>The question:</strong> is there any way <em>to</em> execute a native SQL query returning an array of Objects using Hibernate <em>without</em> producing compiler warnings <em>while</em> achieving type safety? If not, is there any sensible alternative?</p>