title: Manually specify the value of a primary key in JPA @GeneratedValue column
body: <p>I'm having an Entity which has a primary key / id field like the following:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  </code></pre>
  
  <p>This works well. I'm using EclipseLink to create the DDL-Schema, and the column is correctly created like so:</p>
  
  <pre><code>`id` bigint(20) NOT NULL AUTO_INCREMENT
  </code></pre>
  
  <p>However, I've got several entities for which I do want to specify the PK myself (it's a little application that transfers data from an old database to the new one we're building). If I specify the ID for the POJO (using <code>setId(Long id)</code>) and persist it, EclipseLink <em>does not</em> save it (i.e. the record is saved, but the id is auto generated by eclipseLink).</p>
  
  <p><strong>Is there a way to manually specify the value of a column which has a @GeneratedValue ?</strong></p>
  
  <p>Here some thoughts on the issue:</p>
  
  <p>I tried to work around the problem by not using @GeneratedValue at all, but simply manually define the column to be AUTO_INCREMENTed. However this forces me to manually provide an IDs <em>always</em>, since EclipseLink validates the primary key (so it may not be null, zero, or a negative number). The exception message reads that I should specify eclipselink.id_validation, however this does not seem to make any difference (I annotated <code>@PrimaryKey(validation = IdValidation.NONE)</code> but still got the same message).</p>
  
  <p><strong>To clarify:</strong> I'm using EclipseLink (2.4.0) as persistence provider and I can't switch away from it (large portions of the project depend on eclipselink specific query hints, annotations, and classes).</p>
  
  <hr>
  
  <p>EDIT (In Response to the answers):</p>
  
  <p><strong>Custom Sequencing:</strong> I tried to implement my own sequencing. I tried subclassing DefaultSequence, but EclipseLink will tell me <code>Internal Exception: org.eclipse.persistence.platform.database.MySQLPlatform could not be found</code>. But I've checked: The class is on the classpath.</p>
  
  <p>So I subclassed another class, NativeSequence:</p>
  
  <pre><code>public class MyNativeSequence extends NativeSequence {
  
      public MyNativeSequence() {
          super();
      }
  
      public MyNativeSequence(final String name) {
          super(name);
      }
  
  
      @Override
      public boolean shouldAlwaysOverrideExistingValue() {
          return false;
      }
  
      @Override
      public boolean shouldAlwaysOverrideExistingValue(final String seqName) {
          return false;
      }
  
  }
  </code></pre>
  
  <p>However, what I get is the following:</p>
  
  <pre><code>javax.persistence.RollbackException: Exception [EclipseLink-7197] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: Null or zero primary key encountered in unit of work clone [de.dfv.datenbank.domain.Mitarbeiter[ id=null ]], primary key [null]. Set descriptors IdValidation or the "eclipselink.id-validation" property.
      at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
      ...
  Caused by: Exception [EclipseLink-7197] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: Null or zero primary key encountered in unit of work clone [de.dfv.datenbank.domain.Mitarbeiter[ id=null ]], primary key [null]. Set descriptors IdValidation or the "eclipselink.id-validation" property.
      at org.eclipse.persistence.exceptions.ValidationException.nullPrimaryKeyInUnitOfWorkClone(ValidationException.java:1451)
      ...
  </code></pre>
  
  <p>(stack trace shortened for clarity). This is the same message which I got before. Shouldn't I subclass NativeSequence? If so, I don't know what to implement for the abstract methods in Sequence or StandardSequence.</p>
  
  <p>It may also be worth noting, that simply subclassing (without overriding any methods) the class works as expected. However, returing false in <code>shouldAlwaysOverrideExistingValue(...)</code> will not generate a single value at all (I stepped through the program and <code>getGeneratedValue()</code> is not called once).</p>
  
  <p>Also, when I insert like 8 entities of a certain kind within a transaction it resulted in 11 records in the database (what the hell?!).</p>
  
  <p><strong>EDIT (2012-09-01):</strong> I still do not have a Solution for the problem, Implementing my own sequence did not solve it. What I need is a way to be able to not set an Id explicitly (so it will be auto generated) and to be able to set an Id explicitly (so it will be used for the creation of the record in the database).</p>
  
  <p>I tried to define the column as auto_increment myself and ommit @GeneratedValue, however Validation will kick in and not allow me to save such an entity. If I specify a value != 0 and != zero, mysql will complain for a duplicate primary key.</p>
  
  <p>I'm running out of ideas and options to try. Any? (starting a bounty)</p>