场景
当以查询的实体或者是查询的实体作为参数传送时,修改内容,就会报identifier of an instance of 的错误。持久层工具本人使用的是JPA,
private Result func(){
Aentity a = aresporitory.findById("123");
String id = UUID.randomUUID().toString();
a.setId(id);
......
aresporitory.save(a);
}
问题
问题描述:就是会出现提醒你A实体已经改成了B实体
identifier of an instance of **.**.Aentity alter **.**.Bentity
解释
是因为内存地址没有发生改变,此时,需要新建一个实体对象来承接。
解决方案
新建对象并赋值即可解决该问题。
private Result func(){
Aentity goal_a = aresporitory.findById("123");
//预保存实体
Aentity pre_save_a = new Aentity();
//将 goal_a 的值赋值给 pre_save_a
BeanUtils.copyProperties(goal_a,pre_save_a);
String id = UUID.randomUUID().toString();
pre_save_a.setId(id);
......
aresporitory.save(pre_save_a);
}