title: How to fetch only specific columns in a @NamedQuery?
body: <p>I have a large database row, and want to only fetch some specific colums. Ideally they should be automatically mapped to my <code>@Entity</code>.</p>
  
  <p>I tried the following, which did not work, as somehow the id attribute is required. Why?</p>
  
  <pre><code>@Entity
  @NamedNativeQuery(
      name = "testQuery",
      query = "SELECT city FROM mytable",
      resultClass = MyEntity.class
  )
  
  public class MyEntity {
      @Id
      private int id;
  
      private String country;
      private String city;
  
      public MyEntity() {
  
      }
  
      //getter+setter for all attributes
  }
  </code></pre>
  
  <p>Execution:</p>
  
  <pre><code>getEntityManager().createNamedQuery("testQuery", MyEntity.class);
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 0, SQLState: 42703
  ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper: The column id was not found in the ResultSet.
  ERROR : PSQLException: The column id was not found in the ResultSet.
  </code></pre>
  
  <p>Even if I write <code>SELECT id, city FROM mytable</code> I would get: The column country was not found in the ResultSet.` So somehow every column that is present in the database, but not selected by the query, causes this failure. Why?</p>
  
  <p><strong>Regarding the duplicate mark:</strong></p>
  
  <p>Quote from that anser:</p>
  
  <pre><code>@NamedNativeQuery(
      name="jedisQry", 
      query = "SELECT name,age FROM jedis_table", 
      resultClass = Jedi.class
  )
  </code></pre>
  
  <p>Then, we can simply do:</p>
  
  <pre><code>TypedQuery&lt;Jedi&gt; query = em.createNamedQuery("jedisQry", Jedi.class);
  List&lt;Jedi&gt; items = query.getResultList();
  </code></pre>
  
  <p>Well, if that's the solution, it is exactly the same as my question code. Any for me it does not work??</p>
  
  <p><strong>Solution in my case:</strong></p>
  
  <pre><code>@NamedNativeQuery(
      name = "testQuery",
      query = "SELECT city FROM mytable",
      resultSetMapping = "testResultSet"
  )
  
  @SqlResultSetMapping(
      name = "testResultSet",
      classes = {
          @ConstructorResult(
              targetClass = MyEntity.class,
              columns = {
                  @ColumnResult(name = "city")
              }
          )
      }
  )
  </code></pre>