Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Id annotation in JPA and Hibernate

If I don't want to automatically generate the primary Key, instead, I want to supply the ID with the first column of the table as primary key.

2 A
4 B
7 D
13 E

...

I want the first column 2,4, 7, 13 to be the primary key of the table. Should I just use @Id to do the annotation?

@Entity
public class Code {
   @Id
   @Column(unique=true)
   private int id;

   ...
}

Or, if @Id is used, the primary key will be always automatically generated, instead of using the first column, in this case?

like image 450
user697911 Avatar asked Mar 29 '16 04:03

user697911


1 Answers

@Id will only declare the primary key. it will not insert generated value. if you use @GeneratedValue then it will generate the value of the field.

like image 86
stinepike Avatar answered Oct 06 '22 05:10

stinepike