title: JPA Transformer for JPQL Query Result
body: <p>In JPA 2.1 I need to transform the result of a JPQL Query to a custom POJO object.</p>
  
  <p>I'm aware of the following solution but I <strong>can't</strong> use it because I don't have a native SQL query.</p>
  
  <pre><code>@Entity
  @Table("USER_T")
  @SqlResultSetMapping(name="SearchResult", classes = {
          @ConstructorResult(targetClass = InternalUser.class, 
          columns = {@ColumnResult(name="userId"), @ColumnResult(name="firstName"),
                  @ColumnResult(name="lastName"), @ColumnResult(name="role")
          })
      })
  </code></pre>
  
  <p>Then in my DAO class,</p>
  
  <pre><code>javax.persistence.Query query = entityManager.createNativeQuery(jpql, "SearchResult");
  </code></pre>
  
  <p>If I were to try this approach I would get SQL Exceptions because the <code>jpql</code> variable isn't native SQL. It holds a JPQL expression, which allows some "extras" like specifying a custom field in a Where clause or an Order-By.</p>
  
  <p>So instead of <code>createNativeQuery(strQuery, strMapping)</code> I need <code>createQuery(strQuery, strMapping)</code> -- but that one doesn't exist. Any suggestions? I still need to transform the result to a custom POJO.</p>