title: How to best map results from an SQL query to a non-entity Java object using Hibernate?
body: <p>I have a Hibernate managed Java entity called X and a native SQL function (myfunc) that I call from a Hibernate SQL query along these lines:</p>
  
  <pre><code>SQLQuery q = hibernateSession.createSQLQuery(
                       "SELECT *, myfunc(:param) as result from X_table_name"
               );
  </code></pre>
  
  <p>What I want to do is to map the everything returned from this query to a class (not necessarily managed by Hibernate) called Y. Y should contain all properties/fields from X plus the <code>result</code> returned by <code>myfunc</code>, e.g. Y could extend class X and add a "result" field.</p>
  
  <p>What I've tried:</p>
  
  <ol>
  <li>I've tried using <code>q.addEntity(Y.class)</code> but this fails with:
  <code>org.hibernate.MappingException: Unknown entity com.mycompany.Y</code></li>
  <li><code>q.setResultTransformer(Transformers.aliasToBean(Y.class));</code> but this fails with: <code>org.hibernate.PropertyNotFoundException: Could not find setter for some_property</code>. X has a field called <code>someProperty</code> with the appropriate getter and setter but in this case it doesn't seem like Hibernate maps the column name (some_property) to the correct field name.</li>
  <li><code>q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);</code> returns a Map but the values are not always of the type expected by the corresponding field in X. For example fields in X of type enum and Date cannot be mapped directly from the Map returned by the SQL query (where they are Strings).</li>
  </ol>
  
  <p>What's the appropriate way to deal with this situation?</p>