title: Java Spring Hibernate : Model class primary key auto increment field : set automatic and manually on a field
body: <p>in java spring hibernate maven project, i use (@Id and @GeneratedValue(strategy = GenerationType.AUTO)) together on a field to make this field P.K and Auto-increment on mysql DB.</p>
  
  <h2>i want:</h2>
  
  <ul>
  <li>this field to be auto_increment if i did not set its id manually , and if i set it manually by setter , it works fine(but currently i get error if i set manually).</li>
  </ul>
  
  <h2>problem:</h2>
  
  <ul>
  <li>when i left the id null , auto_increment works , but when i set it manually i get error;
  how i annotate and config the model class that i can set manually by setter and automatic pk Id (anyone i like to do)?</li>
  </ul>
  
  <h2>my code:</h2>
  
  <pre><code>public class Company implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Basic(optional = false)
  @Column(name = "id")
  private Integer id; 
  </code></pre>
  
  <h2>Annotation Detail based on java doc:</h2>
  
  <p>@Id :
  Specifies the primary key of an entity</p>
  
  <p>@GeneratedValue(strategy = ... ) :
  Provides for the specification of generation strategies for the values of primary keys</p>
  
  <ul>
  <li><p>GenerationType.IDENTITY : 
  Indicates that the persistence provider must assign primary keys for the entity using a database identity column.</p></li>
  <li><p>GenerationType.SEQUENCE : 
  Indicates that the persistence provider must assign primary keys for the entity using a database sequence.</p></li>
  <li><p>GenerationType.TABLE : 
  Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.</p></li>
  <li><p>GenerationType.AUTO : 
  If you use AUTO, Hibernate will choose one of the strategies to generate your id</p></li>
  </ul>