title: Configure JPA to let PostgreSQL generate the primary key value
body: <p>So our project use PostgreSQL database and we use JPA for operating the database.
  We have created the entities from the database with automatic creator in Netbeans 7.1.2.</p>
  
  <p>After small changes our primary key values are described as:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Basic(optional = false)
  @NotNull
  @Column(name = "idwebuser", nullable = false)
  private Integer idwebuser;
  </code></pre>
  
  <p>The problem is that now the application is not flexible, because when we modify the database directly (using SQL or another tool) instead of going thru the Java app - the Generated Value is lower than actual database ID value - and so we get error during the creation of new entities.</p>
  
  <p>Is there a possibility that the JPA could just let the database generate the ID automatically and then obtain it after the creation process?
  Or what could be a better solution? 
  Thanks.</p>
  
  <p><strong>EDIT</strong>
  More specifically:
  We have a table of users and my problem is that using any type of strategy generationtype, the JPA is inserting a new entity with a specified by it's generator id. Which is wrong for me, because if I make changes to the table on my own, by adding new entries, the GeneratedValue for application is lower than the current ID - what leads us to exception with duplicated ID.
  Can we fix it ;)?</p>
  
  <p><em>a short note on the answer</em>
  There was a little lie from my side because we've used a PG Admin -> View first 100 Rows and edited rows from there instead of using select. ANYWAY, it turns out that this editor somehow skips the process of updating the ID and so even in DB when we write a proper INSERT it is EXECUTED with improper ID! So it was basically more a problem of the editor we used than the database and application...</p>
  
  <p>now it even works using <code>@GeneratedValue(strategy=GenerationType.IDENTITY)</code></p>