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.
i want:
- 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).
problem:
- 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)?
my code:
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
Annotation Detail based on java doc:
@Id : Specifies the primary key of an entity
@GeneratedValue(strategy = ... ) : Provides for the specification of generation strategies for the values of primary keys
GenerationType.IDENTITY : Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
GenerationType.SEQUENCE : Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
GenerationType.TABLE : Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
GenerationType.AUTO : If you use AUTO, Hibernate will choose one of the strategies to generate your id