解决hibernate nonuniqueobjectexception

时间:2014-02-28 08:45:02

标签: hibernate

我的代码是

@Transactional(propagation = Propagation.REQUIRED)
public void saveIndent(StockRequisitionCumIssue stockRequisitionCumIssue,
    List<StockRequisitionCumIssueDetail> srciDetails,
    List<StockRequisitionCumIssueDetail> removedObjects) {

    sessionFactory.getCurrentSession().clear();
    for (StockRequisitionCumIssueDetail srciDetail : removedObjects) {
        objectLoader.delete(StockRequisitionCumIssueDetail.class, srciDetail);
    }

    objectLoader.save(StockRequisitionCumIssue.class, stockRequisitionCumIssue);

    for (StockRequisitionCumIssueDetail srciDetail : srciDetails) {

        srciDetail.setStockRequisitionCumIssue(stockRequisitionCumIssue);
        objectLoader.save(StockRequisitionCumIssueDetail.class, srciDetail);

        item.setOpeningQuantity(item.getOpeningQuantity().subtract(srciDetail.getQuantityIssued()));
        item.setTotalValuation(item.getOpeningQuantity().multiply(item.getStandardCost()));
        sessionFactory.getCurrentSession().update(item);
        objectLoader.save(Item.class, item);
    }
}

我正在尝试更新项目Object.But它不允许我两次更新相同的项目。它正在抛出一个非常规的非独特的例子。如果你有解决方案,请帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

不确定你到底得到了什么异常,但是如果你试图保存(我假设这是ObjectLoader.save()做的..)多次相同的对象,Hibernate会抛出异常作为带有标识符的对象( PK)已经存在。 使用saveOrUpdate代替或者只删除此行,因为更新应该已经完成​​了工作。