Update or saveorUpdate in CRUDRepository

前端 未结 2 764
别跟我提以往
别跟我提以往 2020-12-28 18:18

I know there\'s already a similar question answered previously, but my problem is implementing save with update while there are 3 methods in the interface. I\'m currently us

相关标签:
2条回答
  • 2020-12-28 18:38

    putting below if check resolve my issue and save method is working as save and update both when i pass id it updates the record in database and when i dont pass id it save new record in database place is incoming object in my case and new place is new object of place in which i am setting the place id

        if(place.getPlaceId()!=0)
            newPlace.setPlaceId(place.getPlaceId());
    
    0 讨论(0)
  • 2020-12-28 18:43

    CrudRepository has only save but it acts as update as well.

    • When you do save on entity with empty id it will do a save.
    • When you do save on entity with existing id it will do an update that means that after you used findById for example and changed something in your object, you can call save on this object and it will actually do an update because after findById you get an object with populated id that exist in your DB.
    • save in CrudRepository can accept a single entity or Iterable of your entity type.
    0 讨论(0)
提交回复
热议问题