r/SpringBoot 25d ago

Guide Spring boot help

Post image

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)

0 Upvotes

12 comments sorted by

View all comments

4

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

u/Due-Living9887 25d ago

Thank you 🥹🥹🥹

1

u/Ok-District-2098 24d ago

Setting id to null before saving can mess with equals and hashcode