Erwin
Erwin

Reputation: 11

How to solve the value of not null in database when JPA is updating?

CREATE TABLE `user_info` (
`user_info_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(40) NOT NULL,
`email` varchar(100) 
)

this my database table,database have A record id=1, nickname=test, email=null

UserInfo info = new UserInfo();
info.setUserInfoId(1);
info.setEmail("[email protected]");

When I update with the above code, an error Column 'nickname' cannot be null occurs!

I know this is because JPA has to perform a lookup first, but I don't want to give JPA all the values that can't be empty when updating the operation.

Additional explanation:

Assuming that my front end only sends me ID and email, how can I update it? Use SQL is feasible, but JPA must require nickname not null

How to solve this problem? thx

Upvotes: 0

Views: 2036

Answers (1)

David Conrad
David Conrad

Reputation: 16379

The way to update an existing record is to load the record, update any columns that need updating, and then save it, not by creating a new object.

UserInfo info = userInfoRepository.findById(1);
info.setEmail("[email protected]");
userInfoRepository.save(info);

Upvotes: 1

Related Questions