Reputation: 111
I am using hibernate annotation to generate my database table:
public class Item {
@Id @GeneratedValue
@Column(name="itemid", unique = true, nullable = false)
private long id;
}
But I don't understand how it generated the id. I am assuming that it will generated id which is different from what already in the table. But the fact is, it doesn't generate the id continuously. The following image is from my database:
And sometimes when I rebuild my project and want to insert a new item into the database, it generated an itemid which is same with before(for example; 12, which already in the table), and the due to this, the insertion will fail because it not allowed the duplicate key.
Anyone know why this happens?
Upvotes: 0
Views: 46
Reputation: 1287
I was having the same issue. It was resolved by adding the below to my component, in your case to class item.
@GeneratedValue(strategy=GenerationType.IDENTITY)
Upvotes: 1