第二次碰上了``
a different object with the same identifier value was already associated with the session
在网上看相应的解决方案。对我这里MS不管用。
原代码: 上面是一个循环 一次保存多个storege对像,而每次storage.setId(storageMap.get("id").toString()); set的id相同 所有报此错误
if (storageMap != null) {
Storage storage = new Storage();
storage.setId(storageMap.get("id").toString());
if (storageMap.get("wholesale") != null) {
storage.setWholesale(storageMap.get("wholesale").toString());
}
storage.setSku(sku);
storage.setStore(store);
Double quantity = (Double) storageMap.get("quantity");
Double q = changeQuantity;
storage.setQuantity(quantity + q);
getStorageService().save(storage);
网上的解决方案 getHibernateTemplate().merge(object); 保存的时候把传过来的storage对像合并 MS不管用。
最后解决方法
Storage storage = getStorageService().get(Storage.class,storageMap.get("id").toString());
if (storageMap.get("wholesale") != null) {
storage.setWholesale(storageMap.get("wholesale").toString());
}
Double quantity = (Double) storageMap.get("quantity");
Double q = changeQuantity;
storage.setQuantity(quantity + q);
getStorageService().save(storage);
另有解决方案: