Skip to content
Advertisement

Hibernate @GeneratedValue null error for primary key

I am using Hibernate to save an object in the database. I am generating my primary key with @GeneratedValue annotation.

Here is my code Vendor class

I am using MS SQL Server 2012 where I have a vendor table with following columns

Unchecked means its NOTNULL = true, it doesnt allow null values.

Here is the code where I am trying to save the data into database.

When I run this, I get following error

Advertisement

Answer

If you use the strategy javax.persistence.GenerationType.IDENTITY for @GeneratedValue your table must have an identity generator. This can be done including an AUTO_INCREMENT to your primary key.

Example: CREATE TABLE Vendor ( ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID) )

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement