Jpa Repository save() doesn't update existing data

后端 未结 3 1148
挽巷
挽巷 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:53

    This question is already been answered but this is my understanding of topic, as I recently started working on it.

    This can be answered on basis of Transient and Persistent/managed entities.

    Transient entity : A new entity object created which is not associated with any session till now. This object is not related to any stored data in database since it is freshly created.

    When you fetch a record from db it is fetched in managed or persistent state and any changes made to it will be reflected back to the record that it is mapped to.

    Suggestion 1 : You should not be manually adding primary key values since it is already AutoIncremented.

    Suggestion 2 : If you already have recirds id, fetch it from db first and then update it.

    Here is a Stackoverflow discussion on the topic to get you some more insight : What are detached, persistent and transient objects in hibernate?

提交回复
热议问题