Found shared references to a collection异常
//保存到库存(delStorageInventory)
Member member = this.memberService.findBySn(sn);
List<DelStorageInventory> delStorageInventoryMembers = delStorageInventoryService.findByMember(member);
List<DelStorageInventory> delStorageInventorys = delStorageInventoryService.findByUPC(delStorageInventory.getUpc());
//当upc查找到的库存为0,或者member查找到的库存为0时,执行添加操作
if (delStorageInventoryMembers.size() == 0 || delStorageInventorys.size() == 0) {
delStorageInventory.setMember(this.memberService.findBySn(sn));
delStorageInventory.setOperator(this.adminService.getCurrent());
delStorageInventory.setDelStorage(this.delStorageService.find(delStorageId));
delStorageInventory.setSn(this.snService.generate(Sn.Type.delStorageInventory));
/** 数据验证 */
if(!isValid(delStorageInventory, new Class[0])) {
return ERROR_VIEW;
}
this.delStorageInventoryService.save(delStorageInventory);
}
for (DelStorageInventory delStorageInventoryMember : delStorageInventoryMembers) {
//根据会员得到的upc和传进来的upc不等时,执行添加操作
if (delStorageInventoryMember.getUpc() != delStorageInventory.getUpc()) {
delStorageInventory.setMember(this.memberService.findBySn(sn));
delStorageInventory.setOperator(this.adminService.getCurrent());
delStorageInventory.setDelStorage(this.delStorageService.find(delStorageId));
delStorageInventory.setSn(this.snService.generate(Sn.Type.delStorageInventory));
/** 数据验证 */
if(!isValid(delStorageInventory, new Class[0])) {
return ERROR_VIEW;
}
this.delStorageInventoryService.save(delStorageInventory);
}
//根据会员得到的upc和传进来的upc相等时,执行更新操作
if (delStorageInventoryMember.getUpc() == delStorageInventory.getUpc()) {
BeanUtils.copyProperties(delStorageInventory, delStorageInventoryMember, new String[] {"id","sn","upc","member","quantity"});
if (!isValid(delStorageInventoryMember, new Class[0])) {
return ERROR_VIEW;
}
delStorageInventoryMember.setOperator(this.adminService.getCurrent());
delStorageInventoryMember.setDelStorage(this.delStorageService.find(delStorageId));
delStorageInventoryMember.setQuantity(delStorageInventoryMember.getQuantity() + delStorageInventory.getQuantity());
delStorageInventoryService.update(delStorageInventoryMember);
}
}
for (DelStorageInventory delStorageInventory2 : delStorageInventorys) {
//且根据upc得到的会员和sn查找到的会员不等时,执行添加操作
if (delStorageInventory2.getMember() != member) {
delStorageInventory.setMember(this.memberService.findBySn(sn));
delStorageInventory.setOperator(this.adminService.getCurrent());
delStorageInventory.setDelStorage(this.delStorageService.find(delStorageId));
delStorageInventory.setSn(this.snService.generate(Sn.Type.delStorageInventory));
/** 数据验证 */
if(!isValid(delStorageInventory, new Class[0])) {
return ERROR_VIEW;
}
this.delStorageInventoryService.save(delStorageInventory);
}
//根据upc得到的会员和sn查找到的会员相等时,执行更新操作
if (delStorageInventory2.getMember() == member) {
BeanUtils.copyProperties(delStorageInventory, delStorageInventory2, new String[] {"id","sn","upc","member","quantity"});
if (!isValid(delStorageInventory2, new Class[0])) {
return ERROR_VIEW;
}
delStorageInventory2.setOperator(this.adminService.getCurrent());
delStorageInventory2.setDelStorage(this.delStorageService.find(delStorageId));
delStorageInventory2.setQuantity(delStorageInventory2.getQuantity() + delStorageInventory.getQuantity());
delStorageInventoryService.update(delStorageInventory2);
}
}
org.hibernate.HibernateException: Found shared references to a collection: com.delawaresaving.entity.DelStorageInventory.delStorageInventoryIns
网上有人这样说:
原因:
BeanUtils.copyPropertis是浅拷贝,导致这两个对象引用的Set是同一个Set,这在hibernate中是不允许的,参见Hibernate reference第6章的"Two entities may not share a reference to the same collection instance"。
这种问题常见于复制对象时。
如何解决:newP.setSonSet(null);
但这个newP.setSonSet(null);是什么意思啊,,搞不懂该怎么写,求指导 javaWeb
补充:Java , Web 开发