NutzCN Logo
问答 一个pojo跟另外两个pojo是一对多关系,如何将这两个一对多关系一起插入数据库
发布于 2417天前 作者 wx_7lk4hgerl7vdvt2aav1n 2067 次浏览 复制 上一个帖子 下一个帖子
标签:
blic class SaleActivityBo extends SaleActivity {
    /**
     * 生效区域的集合
     */
    @Many(field = "saleActivityId")
    private List<EffectiveAreaBo> effectiveAreaBoList;


    /**
     * 优惠内容的集合
     */
    @Many(field = "saleActivityId")
    private List<SaleContentBo> saleContentBoList;
   SaleActivityBo returnBo = saleRuleService.insertWith(saleActivityBo, "effectiveAreaBoList");
8 回复

insertWith的第二个参数是正则表达式,可以是null的

那如果是下面的这种多层次的一对多关系映射呢,如何将SaleContentBo跟SaleRuleBo的一对多关系也一起保存到数据库中

public class SaleActivityBo extends SaleActivity {
    /**
     * 生效区域的集合
     */
    @Many(field = "saleActivityId")
    private List<EffectiveAreaBo> effectiveAreaBoList;


    /**
     * 优惠内容的集合
     */
    @Many(field = "saleActivityId")
    private List<SaleContentBo> saleContentBoList;

}

public class SaleContentBo extends SaleContent {

    /**
     * 保存营销规则的集合
     */
    @Many(field = "saleContentId")
    private List<SaleRuleBo> saleRuleBoList;
}

如果是特例,可以先insertWith,然后对第二层的对象执行insertLinks

具体怎么操作呢,下面的这个代码稚嫩插入到第二层次,第三层次的就不行了


/** * 测试插入一对多关系 * 如果要插入一对多的关系的话直接下面这种实现就可以了 */ @Test public void testManyInsert() { SaleActivityBo saleActivityBo = new SaleActivityBo(); saleActivityBo.setCreatorId("11111111111111111111"); List<EffectiveAreaBo> effectiveAreaList = new ArrayList<>(); EffectiveAreaBo effectiveArea = new EffectiveAreaBo(); effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE); effectiveArea.setEffectiveId("2222222222222222"); effectiveAreaList.add(effectiveArea); effectiveArea = new EffectiveAreaBo(); effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE); effectiveArea.setEffectiveId("3333333333333333"); effectiveAreaList.add(effectiveArea); saleActivityBo.setEffectiveAreaBoList(effectiveAreaList); List<SaleContentBo> saleContentBoList = new ArrayList<>(); SaleContentBo saleContentBo = new SaleContentBo(); saleContentBo.setCurrentNumber(222); saleContentBo.setPreferentialValue(222); saleContentBoList.add(saleContentBo); List<SaleRuleBo> saleRuleBoList = new ArrayList<>(); SaleRuleBo saleRuleBo = new SaleRuleBo(); saleRuleBo.setMinValue(111); saleRuleBo.setMaxValue(111); saleRuleBoList.add(saleRuleBo); saleRuleBo = new SaleRuleBo(); saleRuleBo.setMinValue(222); saleRuleBo.setMaxValue(222); saleRuleBoList.add(saleRuleBo); saleContentBo.setSaleRuleBoList(saleRuleBoList); saleContentBo = new SaleContentBo(); saleContentBo.setCurrentNumber(111); saleContentBo.setPreferentialValue(111); saleContentBoList.add(saleContentBo); saleActivityBo.setSaleContentBoList(saleContentBoList); saleRuleBoList = new ArrayList<>(); saleRuleBo = new SaleRuleBo(); saleRuleBo.setMinValue(333); saleRuleBo.setMaxValue(333); saleRuleBoList.add(saleRuleBo); saleRuleBo = new SaleRuleBo(); saleRuleBo.setMinValue(444); saleRuleBo.setMaxValue(444); saleRuleBoList.add(saleRuleBo); saleContentBo.setSaleRuleBoList(saleRuleBoList); saleRuleService.insertWith(saleActivityBo, "saleActivityId|saleActivityId|saleContentId"); }
dao.insertLinks(saleContentBo, null);

貌似不行,像下面这样的代码,只能将EffectiveAreaBo、SaleContentBo插入,SaleActivityBo和SaleRuleBo都插入不进去

 @Test
    public void testManyInsert() {
        SaleActivityBo saleActivityBo = new SaleActivityBo();
        saleActivityBo.setCreatorId("11111111111111111111");

        List<EffectiveAreaBo> effectiveAreaList = new ArrayList<>();
        EffectiveAreaBo effectiveArea = new EffectiveAreaBo();
        effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE);
        effectiveArea.setEffectiveId("2222222222222222");
        effectiveAreaList.add(effectiveArea);
        saleActivityBo.setEffectiveAreaBoList(effectiveAreaList);

        List<SaleContentBo> saleContentBoList = new ArrayList<>();
        SaleContentBo saleContentBo = new SaleContentBo();
        saleContentBo.setCurrentNumber(222);
        saleContentBo.setPreferentialValue(222);

        List<SaleRuleBo> saleRuleBoList = new ArrayList<>();
        SaleRuleBo saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(111);
        saleRuleBo.setMaxValue(111);
        saleRuleBoList.add(saleRuleBo);
        saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(222);
        saleRuleBo.setMaxValue(222);
        saleRuleBoList.add(saleRuleBo);
        saleContentBo.setSaleRuleBoList(saleRuleBoList);

        saleContentBoList.add(saleContentBo);
        saleActivityBo.setSaleContentBoList(saleContentBoList);

        saleRuleService.insertLinks(saleActivityBo, null);
    }

然后试了下,像这样子的实现就可以了,就能将三层的关系全部插入进去掉

 public void testManyInsert() {
        SaleActivityBo saleActivityBo = new SaleActivityBo();
        saleActivityBo.setCreatorId("11111111111111111111");

        List<EffectiveAreaBo> effectiveAreaList = new ArrayList<>();
        EffectiveAreaBo effectiveArea = new EffectiveAreaBo();
        effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE);
        effectiveArea.setEffectiveId("2222222222222222");
        effectiveAreaList.add(effectiveArea);
        saleActivityBo.setEffectiveAreaBoList(effectiveAreaList);

        List<SaleContentBo> saleContentBoList = new ArrayList<>();
        SaleContentBo saleContentBo = new SaleContentBo();
        saleContentBo.setCurrentNumber(222);
        saleContentBo.setPreferentialValue(222);

        List<SaleRuleBo> saleRuleBoList = new ArrayList<>();
        SaleRuleBo saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(111);
        saleRuleBo.setMaxValue(111);
        saleRuleBoList.add(saleRuleBo);
        saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(222);
        saleRuleBo.setMaxValue(222);
        saleRuleBoList.add(saleRuleBo);
        saleContentBo.setSaleRuleBoList(saleRuleBoList);

        saleContentBoList.add(saleContentBo);
        saleActivityBo.setSaleContentBoList(saleContentBoList);

        saleRuleService.insertWith(saleActivityBo, null);
        saleRuleService.insertLinks(saleContentBo, null);
    }

上面的saleRuleService.insertLinks(saleContentBo, null);会存在一个问题就是只会将一个saleContentBo对应的saleRuleBoList关系保存到数据库,如果要将全部的saleContentBo跟对应的saleRuleBoList关系保存下来的会就需要像下面这样迭代saleContentBoList集合然后使用saleRuleService.insertLinks(saleContentBo, null);这样就能将全部的saleContentBo对应的saleRuleBoList关系保存下来

@Test
    public void testManyInsert() {
        SaleActivityBo saleActivityBo = new SaleActivityBo();
        saleActivityBo.setCreatorId("活动创建者id");

        List<EffectiveAreaBo> effectiveAreaList = new ArrayList<>();
        //第一个生效区域
        EffectiveAreaBo effectiveArea = new EffectiveAreaBo();
        effectiveArea.setEffectiveType(EnumEffectiveType.HOUSE_SPACE);
        effectiveArea.setEffectiveId("第一个生效区域");
        effectiveAreaList.add(effectiveArea);
        //第二个生效区域
        effectiveArea = new EffectiveAreaBo();
        effectiveArea.setEffectiveType(EnumEffectiveType.ZONE);
        effectiveArea.setEffectiveId("第二个生效区域");
        effectiveAreaList.add(effectiveArea);
        saleActivityBo.setEffectiveAreaBoList(effectiveAreaList);

        //第一个优惠内容
        List<SaleContentBo> saleContentBoList = new ArrayList<>();
        SaleContentBo saleContentBo = new SaleContentBo();
        saleContentBo.setCurrentNumber(111);
        saleContentBo.setPreferentialValue(111);
        //第一个优惠内容对应的规则
        List<SaleRuleBo> saleRuleBoList = new ArrayList<>();
        SaleRuleBo saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(111111);
        saleRuleBo.setMaxValue(111111);
        saleRuleBoList.add(saleRuleBo);
        saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(111222);
        saleRuleBo.setMaxValue(111222);
        saleRuleBoList.add(saleRuleBo);
        saleContentBo.setSaleRuleBoList(saleRuleBoList);
        //将第一个优惠内容添加到集合中
        saleContentBoList.add(saleContentBo);


        //第二个优惠内容
        saleContentBo = new SaleContentBo();
        saleContentBo.setCurrentNumber(222);
        saleContentBo.setPreferentialValue(222);
        //第二个优惠内容对应的规则
        saleRuleBoList = new ArrayList<>();
        saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(222111);
        saleRuleBo.setMaxValue(222111);
        saleRuleBoList.add(saleRuleBo);
        saleRuleBo = new SaleRuleBo();
        saleRuleBo.setMinValue(222222);
        saleRuleBo.setMaxValue(222222);
        saleRuleBoList.add(saleRuleBo);
        saleContentBo.setSaleRuleBoList(saleRuleBoList);
        //将第二个优惠内容添加到集合中
        saleContentBoList.add(saleContentBo);

        //保存优惠内容到优惠活动中
        saleActivityBo.setSaleContentBoList(saleContentBoList);

        //将所有的关系映射保存起来
        saleRuleService.insertWith(saleActivityBo, null);
        for (SaleContentBo contentBo : saleContentBoList) {
            saleRuleService.insertLinks(contentBo, null);
        }

    }
添加回复
请先登陆
回到顶部