复制出现的问题

@Override
    public void copyPriceRuleEntitys(String[] ids) throws IllegalAccessException, InvocationTargetException {
        /**
         * 复制的时候,BeanUtils.copyPropertis是浅拷贝,导致两个对象引用的set是同一个,
         * 那就导致出现多个引用异常,set属性里面没有任何东西是空集合而不是null
         * 解决:设置set属性为null,
         * 在拷贝后,新建一个集合,将原来的集合元素添加进去,并赋值给新拷贝的实体  
         */
        try {
            MailingWayPriceItemEntity sourcePriceEntity = null;
            MailingWayPriceItemEntity copyPriceEntity = null;
            
            for(String id : ids){
                sourcePriceEntity = mailingWayPriceItemDAO.find(id);
                copyPriceEntity = new MailingWayPriceItemEntity();
                BeanUtils.copyProperties(copyPriceEntity, sourcePriceEntity);
                copyPriceEntity.setId(null);//主键id必须设置为null,否则不能执行insert语句


                Set<MailingWayIncreaseEntity> increase = sourcePriceEntity.getIncrease();
                copyPriceEntity.setIncrease(null);
                if(null!=increase && !increase.isEmpty()){  
                    Set<MailingWayIncreaseEntity> newIncrease = new HashSet<MailingWayIncreaseEntity>(increase.size());//新建一个集合对象,防止共享
                    for(MailingWayIncreaseEntity inc : increase){    
                        newIncrease.add(inc);
                    }  
                    copyPriceEntity.setIncrease(newIncrease);
                }  
                
                Set<MailingWayLinkEntity> mailingWayLink = sourcePriceEntity.getMailingWayLink();
                copyPriceEntity.setMailingWayLink(null);
                if(null!=mailingWayLink&&!mailingWayLink.isEmpty()){
                    Set<MailingWayLinkEntity> newMailingWayLink = new HashSet<MailingWayLinkEntity>(mailingWayLink.size());
                    for(MailingWayLinkEntity mlk:mailingWayLink){
                        newMailingWayLink.add(mlk);
                    }
                    copyPriceEntity.setMailingWayLink(newMailingWayLink);
                }
                
                copyPriceEntity.setRemark(sourcePriceEntity.getRemark()+"_副本");
                mailingWayPriceItemDAO.create(copyPriceEntity);//这里调用了强制插入对象的方法    
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    

修改后

@Override
	public void copyPriceRuleEntitys(String[] ids) throws IllegalAccessException, InvocationTargetException {
		/**
		 * 复制的时候,BeanUtils.copyPropertis是浅拷贝,导致两个对象引用的set是同一个,
		 * 那就导致出现多个引用异常,set属性里面没有任何东西是空集合而不是null
		 * 解决:设置set属性为null,
		 * 将实体的属性一个一个set进去,新建一个集合,将原来的集合元素添加进去,并赋值给新拷贝的实体  
		 */
		MailingWayPriceItemEntity sourcePriceEntity = null;
		MailingWayPriceItemEntity copyPriceEntity = null;
		
		for(String id : ids){
			sourcePriceEntity = mailingWayPriceItemDAO.find(id);
			if(null!=sourcePriceEntity){
				copyPriceEntity = new MailingWayPriceItemEntity();
				copyPriceEntity.setCircumference(sourcePriceEntity.getCircumference());
				copyPriceEntity.setCostBasic(sourcePriceEntity.getCostBasic());
				copyPriceEntity.setCostNotResgister(sourcePriceEntity.getCostNotResgister());
				copyPriceEntity.setCostPerKg(sourcePriceEntity.getCostPerKg());
				copyPriceEntity.setCostResgister(sourcePriceEntity.getCostResgister());
				copyPriceEntity.setExample(sourcePriceEntity.getExample());
				copyPriceEntity.setFlgIncrease(sourcePriceEntity.getFlgIncrease());
				copyPriceEntity.setHeightRestrictions(sourcePriceEntity.getHeightRestrictions());
				copyPriceEntity.setId(sourcePriceEntity.getId());
				
				Set<MailingWayIncreaseEntity> increase = sourcePriceEntity.getIncrease();
				if(null!=increase && !increase.isEmpty()){  
					Set<MailingWayIncreaseEntity> newIncrease = new HashSet<MailingWayIncreaseEntity>(increase.size());//新建一个集合对象,防止共享
					for(MailingWayIncreaseEntity inc : increase){    
						newIncrease.add(inc);
					}  
					copyPriceEntity.setIncrease(newIncrease);
				}  
				
				Set<MailingWayLinkEntity> mailingWayLink = sourcePriceEntity.getMailingWayLink();
				if(null!=mailingWayLink&&!mailingWayLink.isEmpty()){
					Set<MailingWayLinkEntity> newMailingWayLink = new HashSet<MailingWayLinkEntity>(mailingWayLink.size());
					for(MailingWayLinkEntity mlk:mailingWayLink){
						mlk.setMailingWayPriceItem(copyPriceEntity);//建立多对一关系
						newMailingWayLink.add(mlk);
					}
					copyPriceEntity.setMailingWayLink(newMailingWayLink);
				}
				copyPriceEntity.setLengthRestrictions(sourcePriceEntity.getLengthRestrictions());
				copyPriceEntity.setLengthType(sourcePriceEntity.getLengthType());
				copyPriceEntity.setLowWeightLimit(sourcePriceEntity.getLowWeightLimit());
				copyPriceEntity.setMinWeightRestrictions(sourcePriceEntity.getMinWeightRestrictions());
				copyPriceEntity.setPriceId(sourcePriceEntity.getPriceId());
				copyPriceEntity.setPriority(sourcePriceEntity.getPriority());
				copyPriceEntity.setRemark(sourcePriceEntity.getRemark()+"_副本");
				copyPriceEntity.setUpWeightLimit(sourcePriceEntity.getUpWeightLimit());
				copyPriceEntity.setWeightRestrictions(sourcePriceEntity.getWeightRestrictions());
				copyPriceEntity.setWeightType(sourcePriceEntity.getWeightType());
				copyPriceEntity.setWidthRestrictions(sourcePriceEntity.getWidthRestrictions());
			}
			copyPriceEntity.setId(null);//主键id必须设置为null,否则不能执行insert语句
			mailingWayPriceItemDAO.create(copyPriceEntity);//这里调用了创建对象的方法	
		}
	
  }

这个首先出现在,hibernate的浅拷贝,不能出现两个对象引用一个集合,这个时候就要考虑将其一个一个set进去。然后,在处理集合的问题上,要考虑到它的一对多关系,在新建的实体也要保存它的关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值