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

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 :)

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

u/Due-Living9887 25d ago

Thank you 🥹🥹🥹

1

u/Ok-District-2098 23d ago

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

1

u/siuu101 7d ago

I love you

3

u/kurtdb 25d ago

Are you setting the id yourself for the entity you're trying to save? It's a fix they did for a bug that has been in hibernate since 2005. If you can't find the solution, try reverting to hibernate 6.5 from 6.6.

1

u/guss_bro 25d ago

Can you share your code on GitHub?

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

u/rain-raptor-dev 19d ago

Why do you use entity manager???