比如说有个People类你已经在数据库中查询出来了一个People对象 A, 但是后来(当这次session还未关闭,一般都是在同一个方法里)你又创建了一个People对象B他和A的主键值一模一样, 当你要保存B的时候hibernate是不允许两个主键相同的对象在同一个session里面的, 所以最根本的解决办法就是,当你需要修改这个对象的时候只要修改A的相应的值然后save保存就可以了,没必要再创建B再去保存B. 看到网上解决办法 session.clean()session.refresh(object)session.merge(object)类似文章一大推,小编采用最后一种方法,但是由于hibernate映射又报各种异常,偶尔看到网上有人说的一段话,
“比如说有个People类你已经在数据库中查询出来了一个People对象 A, 但是后来(当这次session还未关闭,一般都是在同一个方法里)你又创建了一个People对象B他和A的主键值一模一样, 当你要保存B的时候hibernate是不允许两个主键相同的对象在同一个session里面的, 所以最根本的解决办法就是,当你需要修改这个对象的时候只要修改A的相应的值然后save保存就可以了,没必要再创建B再去保存B.
”
这段话让我彻底明白了,非常简单的解决这个异常,
出现该异常无非就是保存或修改时,
那么我们将要传进去的对象利用ID从数据库查询一遍,然后将原来要修改的对象的值放到新查询出的对象,然后更改这个对象,就OK了。
下面为我的方法代码 仅供参考:public void updateDmdomain(DmDomain dmdomain) { this.moveToDmDomainHis(dmdomain); DmDomain d = super.get(dmdomain.getDmDomainId()); d.setDmTableHead(dmdomain.getDmTableHead()); d.setDoMainType(dmdomain.getDoMainType()); d.setDoMainCode(dmdomain.getDoMainCode()); d.setDoMainEname(dmdomain.getDoMainEname()); d.setDoMainCname(dmdomain.getDoMainCname()); d.setValidDateDt(dmdomain.getValidDateDt()); d.setInvalidDateDt(dmdomain.getInvalidDateDt()); d.setRemark(dmdomain.getRemark()); d.setFilePath(dmdomain.getFilePath()); d.setStandardTpCd(dmdomain.getStandardTpCd()); d.setIsAutoCoding(dmdomain.getIsAutoCoding()); d.setAutoCodingTpCd(dmdomain.getAutoCodingTpCd()); d.setColum1(dmdomain.getColum1()); d.setColum2(dmdomain.getColum2()); d.setColum3(dmdomain.getColum3()); d.setColum4(dmdomain.getColum4()); d.setColum5(dmdomain.getColum5()); d.setLastUpdateDt(dmdomain.getLastUpdateDt()); d.setLastUpdateTxId(dmdomain.getLastUpdateTxId()); d.setLastUpdateUser(dmdomain.getLastUpdateUser()); d.setApproveState(dmdomain.getApproveState()); d.setBillstate(dmdomain.getBillstate()); d.setSubmitDt(dmdomain.getSubmitDt()); d.setSubmitUser(dmdomain.getSubmitUser()); d.setComCodeType(dmdomain.getComCodeType()); d.setManageDepartTpCd(dmdomain.getManageDepartTpCd()); d.setDoMainTable(dmdomain.getDoMainTable()); d.setValid(dmdomain.getValid()); d.setDmSignDepartTps(dmdomain.getDmSignDepartTps()); d.setDmDataAlls(dmdomain.getDmDataAlls()); super.update(d); logger.debug("修改主数据域"); }
a different object with the same identifier value was already associated with the session
最新推荐文章于 2023-07-26 10:38:43 发布