Rick-Lu 2014-06-03 08:06 采纳率: 50%
浏览 884

SSH组合开发中,Hibernate中,主表与从表数据更新

在Hibernate中,我需要更新一条主表的数据,但同时更新多条从表的数据!我最初的构思是,用主表PO类中的Set从表集合,与从jsp中获取的更新从表数据相对比,相同的删除,不相同的添加,同时,把Set集合中的数据相同的留下,不相同的删除。
这是代码:
/*
* 先查询一条商品属性表的信息
* 将Vo转换为Po
* 根据vo类中的ProductCategoryid字段查询需要查询一个商品类型表(CyProductCategory)PO对象
*/
//查询一条商品属性表的信息
CyAttribute attribute = attributeDao.findByIdCyAttribute(att);
//修改商品属性值表的数据时,先将数组与CyAttribute中的 Set cyAttributeValues 值对比
Iterator it = attribute.getCyAttributeValues().iterator();
//双层循环判断数组与Set cyAttributeValues 集合中的值,相等的值赋值为空
for(int i = 0; i < att.getAttVals().length; i++){
System.out.println("------------------>>>" + att.getAttVals()[i]);
while(it.hasNext()){
CyAttributeValue attval = it.next();
System.out.println("--------------->>>" + attval.getAttributeValue());
if(att.getAttVals()[i].trim().equals(attval.getAttributeValue())){
att.getAttVals()[i] = null;
System.out.println("---------------->>>att.getAttVals()[" + i + "]" + att.getAttVals()[i]);
it.remove();
break;
}
System.out.println("---------------->>while");
}
System.out.println("----------------------->>>for");
}

//将Set cyAttributeValues 集合中的值与数组中的值不相等的全部删除
while(it.hasNext()){
    CyAttributeValue attval = it.next();
    if(null != attval && null != attval.getAttributeValue()
            && !"".equals(attval.getAttributeValue())){
        attributeValueDao.delByIdCyAttributeValue(attval);
    }
}

//判断商品属性值表的数据与数组中的值不匹配时,增加不匹配数据
for(int i = 0; i < att.getAttVals().length; i++){
    if(null != att.getAttVals()[i] 
               && !"".equals(att.getAttVals()[i])){
        attributeValueDao.saveCyAttributeValue(
        new CyAttributeValue(attribute, att.getAttVals()[i]));
    }
}
    //将VO类中的数据添加到PO类中
    attribute.setModifydate(StringTimestamp.stringToTimestamp());
    attribute.setName(att.getName());
    attribute.setCyProductCategory(categoryDao.findByIdCyProductCategory(
            new CyProductCategoryVo(att.getProductCategory().getId())));

    attribute.setOrders(att.getOrders());
    //修改
    attributeDao.updateCyAttribute(attribute);
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
    • ¥50 NT4.0系统 STOP:0X0000007B
    • ¥15 想问一下stata17中这段代码哪里有问题呀