identifier of an instance of was altered from to n
生活随笔
收集整理的這篇文章主要介紹了
identifier of an instance of was altered from to n
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
錯(cuò)誤使用場(chǎng)景
public?int?receiveDogInfo(Dog?dog)?{dog.setIsMarked("1");dog.setSource("0");int?result?=?0;dog.setOldId(dog.getId());dog.setId(null);dog.setUpdateTime(new?Date());dog.setCreateTime(new?Date());result?=?DogDao.save(dog);return?result;}會(huì)報(bào)錯(cuò)如下
identifier of an instance of was altered from to null
1:bug出現(xiàn)原因,程序修改處于Persistent狀態(tài)的實(shí)體的id,hibernate是不允許這樣操作的,所以保存失敗。
2:在這樣的情況下,要修改id話,就需要new一個(gè)對(duì)象,將原來(lái)處于persistent狀態(tài)的對(duì)象屬性全部復(fù)制到新的對(duì)象上面,然后再保存。
正確用法應(yīng)該如下
public?int?receiveDogInfo(Dog?dog)?{Dog?dto=?new?Dog();dto?=?dog;dto.setIsMarked("1");dto.setSource("0");int?result?=?0;dto.setOldId(dto.getId());dto.setId(null);dto.setUpdateTime(new?Date());dto.setCreateTime(new?Date());result?=?DogDao.save(dto);return?result;}轉(zhuǎn)載于:https://my.oschina.net/liangzhenghui/blog/194939
總結(jié)
以上是生活随笔為你收集整理的identifier of an instance of was altered from to n的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jfinal连接oracle_JFina
- 下一篇: 2020腾讯二次元营销通案