【问题标题】:Error Msg: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session错误消息:org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联
【发布时间】:2020-04-11 23:45:47
【问题描述】:
try {
        final CHEMRFacilityMedicalEquipment medicalequ = m_chemrFacilityMedicalEquipmentManagementBO
                .findCHEMRFacilityMedicalEquipmentById(id);
        final CHEMRFacilityMedicalEquipment updateMed = new CHEMRFacilityMedicalEquipment();
        if (medicalequ.getMetaStatus() == true) {
            updateMed.setId(medicalequ.getId());
            updateMed.setMetaStatus(false);
            m_chemrFacilityMedicalEquipmentManagementBO
                    .updateCHEMRFacilityMedicalEquipment(updateMed);
            return true;
        }
        return false;
    }

以上是我在 DaOImpl 中调用我的方法的方式

try {
        m_sessionFactory.getCurrentSession().update(instance);
    } catch (final DataAccessException _e) {
        M_LOG.debug("Update object failed:" + _e.toString());
        throw new CHEMRDAOException(_e.toString());
    } catch (final Exception _e) {
        M_LOG.debug("Update object failed:" + _e.toString());
        throw new CHEMRDAOException(_e.toString());
    }
}

DAO 实现

我正在尝试更新我的对象并通过更新方法将其发送到更新表中的数据。 当我调用更新方法时,它返回“具有相同标识符值的不同对象已与会话相关联”没有得到究竟是什么问题。

【问题讨论】:

  • 请提供完整的堆栈跟踪。您尝试更新updateMed.setId 看起来也很可疑。你不应该这样做。

标签: spring hibernate rest


【解决方案1】:

可能是您的会话当前正在处理 2 个具有相同标识符(它们的 id)的不同对象。例如。

CHEMRFacilityMedicalEquipment updateMed = new CHEMRFacilityMedicalEquipment();
updateMed.setId(1);
student.setName("Mukesh");

updateMed = new CHEMRFacilityMedicalEquipment();
updateMed.setId(1);
student.setName("Ryan");

那么假设这个语句被调用了

m_sessionFactory.getCurrentSession().save(updateMed );

虽然该实例的会话尚未关闭(提交),但该实例仍处于持久状态,但不知何故,您的会话试图通过另一个具有相同标识符 (id) 的不同实例来管理:

m_sessionFactory.getCurrentSession().save(updateMed );

m_sessionFactory.getCurrentSession().close();

另外,下面这两行对我来说看起来很可疑

 final CHEMRFacilityMedicalEquipment medicalequ = m_chemrFacilityMedicalEquipmentManagementBO
                .findCHEMRFacilityMedicalEquipmentById(id);
 final CHEMRFacilityMedicalEquipment updateMed = new CHEMRFacilityMedicalEquipment();

如果您的会话当前正在处理具有相同标识符(在本例中为 id)的那些实例。我认为您可以自己弄清楚,因为目前无法从您的代码中提取太多信息。

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2010-11-07
    • 2011-04-06
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2013-08-30
    相关资源
    最近更新 更多