jpa保存数据报错 org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find cn.scitc_rx.rjxh.Module.AcctMgr with id xxxx; nested exception is javax.persistence.EntityNotFoundException: Unable to find cn.scitc_rx.rjxh.Module.AcctMgr with id xxx
问题描述
在用repository保存对象的时候报了如题的错误。
代码如下:
String ip = GetIP.getIpAddress(request);
try {
acctMgr = acctMgrRepository.getOne(ip);
if (acctMgr.getIp()==null) throw e1;
int tmp = acctMgr.getTimes();
}catch (Exception e) {
acctMgr = new AcctMgr();
acctMgr.setIp(ip);
acctMgr.setTimes(RJXHINFO.getAcctMaxLimit());
}
acctMgr.setTimes(acctMgr.getTimes()-1);
acctMgrRepository.save(acctMgr);
一个简单的记录ip访问的代码,每个函数都能正常运行,但就是到保存的时候就报错了。
一开始我怀疑是ip带有特殊字符导致的保存失败,于是直接把setIP里的ip改为了
acctMgr.setIp("0p0p0p0p0p0p1");
重启application后能正常写入,于是我用replaceAll函数替换了所有特殊字符为p,但是依然出现了题目中的错误!
经过多次试验后发现,这个setIp(String ip)里的值不能直接用我定义的ip传过去!
解决方法
既然我定义的你不要,那我让别人包装一下给你
aactMgr.setIp(String.valueOf(ip));
将信将疑的运行了一下,没想到还真就解决了!具体原理现在还未知,只能初步判断我定义的ip不是真正String类型???具体还要等我忙完收手的项目后再仔细研究。