JPA Hibernate merge performs insert instead of update

后端 未结 2 1889
执笔经年
执笔经年 2021-02-07 11:17

I have a simple test, where I am trying to update the object, but merge seems to be performing an insert instead of update.

@RunWith(SpringJUnit4ClassRunner.cla         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 11:38

    Merge will update the row ONLY IF it founds the id in the database, so if you create a new object and forgot to set its ID to the same ID of the desired row, it will try to insert a new one. In case your database table doesn't have any unique field, probably this insert will be successful as there will be no sql error.

    I was able to update the desired row once I set the new object with the ID of that row.

提交回复
热议问题