title: What is the simplest way to assign an ID to a result from a JPA native query?
body: <p>I have a complex SQL query that I'm implementing as a native query in JPA. The entity manager is automatically converting each row of my result set into a <code>Person</code> object, resulting in <code>List&lt;Person&gt;</code></p>
  
  <pre><code>String sql = "select name, address, date_of_birth from ...";
  Query q = entityManager.createNativeQuery(sql, Student.class);
  List&lt;Student&gt; results = q.getResultList();
  return results;
  </code></pre>
  
  <p>The entityManager demands that the <code>Student</code> bean is annotated with <code>@Entity</code>, and this requires an <code>@Id</code>. The API also requires that all fields are matched.  Unfortunately, my select clause pulls from various sources, so there isn't really an Id. In fact, I'd like to later have an id generated when I call <code>StudentRepostiory.save(student)</code>.</p>
  
  <p>I'm in a catch 22 situation. I need an id for the object to be generated, but I don't have an id as yet.</p>
  
  <p>What should I do?</p>
  
  <ul>
  <li>I've tried making the ID null. It makes all results null.</li>
  <li>I've tried making the ID zero. It makes all results the same.</li>
  <li>I've tried adding a Generation Strategy annotation. Didn't do anything.</li>
  </ul>
  
  <p><em>For your reference</em></p>
  
  <p><strong>Student.java</strong></p>
  
  <pre><code>@Entity // This forces me to have an Id.
  class Student {
  
      @Id Long id  // I'm forced to include this, but my query doesn't have one
  
      String name;
      String dateOfBirth;
      String address;
  
      // Getters and Setters
  
  }
  </code></pre>