title: How to autogenerate ID during sql INSERT?
body: <pre><code>INSERT INTO table_having_pk SELECT * FROM table_without_pk;
  </code></pre>
  
  <p>Schema of both tables are the same, only the primary key constraints are not set for the <code>table_without_pk</code>.</p>
  
  <p>Problem: during copy the entries of the 2nd table have <code>null</code> in ID, thus inserts into first table fails.</p>
  
  <pre><code>org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 0, SQLState: 23502
  org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ERROR: NULL-Value in Column ?id? violates Not-Null-Constraint
  </code></pre>
  
  <p>How can I let the 1st table autogenerate the IDs (just count them up) during insert?</p>
  
  <p>Postgres 9.x</p>
  
  <p>Inside my <code>@Entity</code> class the ID is generated as follows for the <code>table_having_pk</code>:</p>
  
  <pre><code>  @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
  </code></pre>