Jpa Repository save() doesn't update existing data

后端 未结 3 1146
挽巷
挽巷 2021-01-21 11:48

I am trying to update data and as I know save() method saves entity if the id is null or update an existing entity in the database if the given id is found in DB.

Howeve

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-21 12:38

    Your Entity Employer looks to be in detached/Transient state and you are passing id value manually which is not permitted, as it is marked as @GeneratedValue(strategy = GenerationType.IDENTITY).

    What you need to do is when you know the primary key value i.e id value, first you fetch the Entity from the database using findById() method, by which Entity comes into Managed state and then try to update the Entity by calling save() method. This will update your Entity.

    For more info on Entity state you can refer this: https://vladmihalcea.com/a-beginners-guide-to-jpa-hibernate-entity-state-transitions/

提交回复
热议问题