diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java b/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java index 4cd98f3acdc..1b99aa3183c 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/dao/GlobalConfigDao.java @@ -98,11 +98,16 @@ public long insert(GlobalConfig globalConfig) { public void insert(GlobalConfig[] globalConfigs) { SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource); simpleJdbcInsert.setTableName("global_config"); + simpleJdbcInsert.usingGeneratedKeyColumns("cid"); Map[] maps = new Map[globalConfigs.length]; for (int i = 0; i < globalConfigs.length; i++) { maps[i] = toMaps(globalConfigs[i]); } - simpleJdbcInsert.executeBatch(maps); + + int[] cids = simpleJdbcInsert.executeBatch(maps); + for (int i = 0; i < globalConfigs.length; i++) { + globalConfigs[i].setCid(cids[i]); + } } public long getCountByName(String name) { diff --git a/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestGlobalConfigDao.java b/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestGlobalConfigDao.java index 535df701740..38edec32d8f 100644 --- a/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestGlobalConfigDao.java +++ b/smart-metastore/src/test/java/org/smartdata/metastore/dao/TestGlobalConfigDao.java @@ -53,18 +53,17 @@ public void testInsertAndGetSingleRecord(){ @Test public void testBatchInsert() { - GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setCid(1); - globalConfig.setProperty_name("test"); - globalConfig.setProperty_value("test1"); - - - GlobalConfig[] globalConfigs = new GlobalConfig[1]; - globalConfigs[0] = globalConfig; + GlobalConfig[] globalConfigs = new GlobalConfig[2]; + globalConfigs[0] = new GlobalConfig(0, "test1", "test1"); + globalConfigs[1] = new GlobalConfig(0, "test2", "test2"); globalConfigDao.insert(globalConfigs); - Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfig)); + globalConfigs[0].setCid(1); + globalConfigs[1].setCid(2); + + Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfigs[0])); + Assert.assertTrue(globalConfigDao.getById(2).equals(globalConfigs[1])); } @Test @@ -80,4 +79,5 @@ public void testUpdate() { Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfig)); } + }