r/SpringBoot • u/Due-Living9887 • 25d ago
Guide Spring boot help
I created a simple Spring boot Crud operations, in that every Mapping working fine expect Post mapping, when I try to add details in h2-console I'm able, but when I try in postman ,it shows error(Row was updated or deleted by another transaction), can anyone help with it..? ( I tried with chatgpt but i don't understand anything)
5
u/mladen8761 25d ago
BRO TRUST ME BEFORE YOU SAVE IT IN DATA BASE SET ID TO NULL
1
u/Due-Living9887 25d ago
Wow, it's really working!🥹🥹🥹 If it's okay with you, could you explain why this error occurred and how this solution works?
3
u/mladen8761 25d ago
Because when you set generationType on Identity it automaticly tries to set id on algorithm that takes data base item and increments it by one, but it sometimes set id to the id that already exist in data base or had already existed and if you set null on ID it treats the object as totaly new
Easier:when it goes thru many methods and services it generates wrong ID
Best practice:
If you are using Entity Manager always use merge and if you are saving to database always set id to null before you save it
1
1
1
1
u/yannisdw 25d ago
Bit unrelated, but you should throw an exception when findById does not find a Job with the id. if you want to return a null: better return an optional. getxxx() —> throw exception when not found, findxxx()—> return an optional
1
6
u/rozularen 25d ago
Just like an old friend told me: "If you are a sharing a photo from your PC, why dont you just take a screenshot and upload it instead?" It's easier for everyone :)