KeyHolder { public static void setGeneratedKeyBack(DaoTask<?> task, Hints hints, List<?> rawPojos) throws SQLException { if (!(task instanceof KeyHolderAwaredTask)) { return; } KeyHolder keyHolder = hints.getKeyHolder(); if (keyHolder == null || rawPojos == null || rawPojos.isEmpty()) { return; } if (!(hints.is(HintEnum.setIdentityBack) && hints.isIdentityInsertDisabled())) { return; } Object obj = rawPojos.get(0); if(obj.getClass() == Entity.class) { Entity entity = (Entity) obj; com.ppdai.das.service.EntityMeta meta = entity.getEntityMeta(); List<Map<String, Object>> maps = ConvertUtils.entity2POJOs((List<Entity>)rawPojos, meta, Map.class); String key = meta.getPrimaryKeyNames().get(0); String type = meta.getMetaMap().get(key).getType(); for (int i = 0; i < rawPojos.size(); i++){ if (!isEmptyKey(keyHolder, i)) { setPrimaryKey(type, key, maps.get(i), keyHolder.getKey(i)); Entity newEntity = ConvertUtils.pojo2Entity(maps.get(i), meta); ((Entity)rawPojos.get(i)).setValue(newEntity.getValue()); } } return; } EntityMeta em = EntityMetaManager.extract(rawPojos.get(0).getClass()); if (em.getPrimaryKeyNames().length == 0) { throw new IllegalArgumentException( "insertIdentityBack only support JPA POJO. Please use code gen to regenerate your POJO"); } Field pkFlield = em.getFieldMap().get(em.getPrimaryKeyNames()[0]); if (pkFlield == null) { throw new IllegalArgumentException( "insertIdentityBack only support JPA POJO. Please use code gen to regenerate your POJO"); } for (int i = 0; i < rawPojos.size(); i++) { if (!isEmptyKey(keyHolder, i)) { setPrimaryKey(pkFlield, rawPojos.get(i), keyHolder.getKey(i)); } } } KeyHolder(); void initialize(int size); int size(); void requireMerge(); boolean isRequireMerge(); boolean isMerged(); void waitForMerge(); void waitForMerge(int timeout); Number getKey(); Number getKey(int index); @Deprecated Map<String, Object> getKeys(); Map<String, Object> getUniqueKey(); List<Map<String, Object>> getKeyList(); List<Number> getIdList(); void addKeys(List<Map<String, Object>> keys); void addKey(Map<String, Object> key); static Hints prepareLocalHints(DaoTask<?> task, Hints hints); static void mergePartial(DaoTask<?> task, KeyHolder originalHolder, Integer[] indexList,
                                    KeyHolder localHolder, Throwable error); static void mergePartial(DaoTask<?> task, KeyHolder originalHolder, KeyHolder localHolder, Throwable error); void addEmptyKeys(int count); void addPatial(Integer[] indexList, KeyHolder tmpHolder); static void setGeneratedKeyBack(DaoTask<?> task, Hints hints, List<?> rawPojos); static final String NOT_SET; }
@Test public void testSetGeneratedKeyBack () throws SQLException { MyTask myTask = new MyTask(); Entity entity = new Entity(); entity.setValue(new Gson().toJson(ImmutableMap.of("keyC", 1))); EntityMeta meta = new EntityMeta(); meta.setPrimaryKeyNames(Lists.newArrayList("keyC")); meta.setColumnNames(Lists.newArrayList("keyC")); meta.setColumnTypes(Lists.newArrayList("INTEGER")); ColumnMeta columnMeta = new ColumnMeta(); columnMeta.setType(JDBCType.INTEGER.getName()); meta.setMetaMap(ImmutableMap.of("keyC", columnMeta)); entity.setEntityMeta(meta); Hints hints = Hints.hints().setIdBack().setSize(Lists.newArrayList(entity)); hints.getKeyHolder().addKey(ImmutableMap.of("keyC", 2)); KeyHolder.setGeneratedKeyBack(myTask, hints, Lists.newArrayList(entity)); Assert.assertTrue(Double.parseDouble(new Gson().fromJson(entity.getValue(), Map.class).get("keyC").toString()) > 1); }