Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA; save() auto increment primary key error

I have a USER table with UserId as primary key, which is int and auto incremented in MySQL.

@Id
@GeneratedValue
@Column(name="USER_ID")
private Integer userId;

When I run userRepository.save(user); I am getting an error saying: org.springframework.beans.InvalidPropertyException: Invalid property 'userId' of bean class [com.mysite.model.User]: Getter for property 'userId' threw exception; nested exception is java.lang.reflect.InvocationTargetException

If I just hard coded the user id I don't get this error. What am I doing wrong?

// hard code it
user.setUserId(5);
userRepository.save(user);
like image 280
topcan5 Avatar asked Feb 18 '15 00:02

topcan5


2 Answers

You may need to set the ID strategy: @GeneratedValue(strategy=GenerationType.IDENTITY)

What you have should work, but also can be quirky.

like image 52
John Thompson Avatar answered Sep 22 '22 12:09

John Thompson


Found my problem: I was using int in my setter and getter for userId, instead of Integer.

like image 40
topcan5 Avatar answered Sep 19 '22 12:09

topcan5