KeyHolder { static void setPrimaryKey(Field pkFlield, Object entity, Number val) throws SQLException { try { if (val == null) { pkFlield.set(entity, null); return; } Class<?> clazz = pkFlield.getType(); if (clazz.equals(Long.class) || clazz.equals(long.class)) { pkFlield.set(entity, val.longValue()); return; } if (clazz.equals(Integer.class) || clazz.equals(int.class)) { pkFlield.set(entity, val.intValue()); return; } if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { pkFlield.set(entity, val.byteValue()); return; } if (clazz.equals(Short.class) || clazz.equals(short.class)) { pkFlield.set(entity, val.shortValue()); return; } if (clazz.equals(BigInteger.class)) { BigInteger bigIntegerValue = BigInteger.valueOf(val.longValue()); pkFlield.set(entity, bigIntegerValue); return; } } catch (Throwable e) { throw new DasException(ErrorCode.SetPrimaryKeyFailed, entity.getClass().getName(), pkFlield.getName()); } } 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 testSetPrimaryKey() throws Exception { MyEntity entity = new MyEntity(); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("bif"), entity, null); Assert.assertNull(entity.bif); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("lf"), entity, 1); Assert.assertEquals(1, entity.lf); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("intf"), entity, 1); Assert.assertEquals(1, entity.intf); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("bytef"), entity, 1); Assert.assertEquals(1, entity.bytef); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("shf"), entity, 1); Assert.assertEquals(1, entity.shf); KeyHolder.setPrimaryKey(MyEntity.class.getDeclaredField("bif"), entity, 1); Assert.assertEquals(BigInteger.valueOf(1), entity.bif); }
@Test public void testSetPrimaryKeyMap() throws Exception { Map map = new HashMap(); KeyHolder.setPrimaryKey(null, "nullf", map, null); Assert.assertNull(map.get("nullf")); KeyHolder.setPrimaryKey(JDBCType.BIGINT.getName(), "bif", map, 1); Assert.assertEquals(1L, map.get("bif")); KeyHolder.setPrimaryKey(JDBCType.INTEGER.getName(), "intf", map, 1); Assert.assertEquals(1, map.get("intf")); KeyHolder.setPrimaryKey(JDBCType.BINARY.getName(), "binf", map, 1); Assert.assertEquals(Byte.valueOf("1"), map.get("binf")); KeyHolder.setPrimaryKey(JDBCType.SMALLINT.getName(), "sf", map, 1); Assert.assertEquals(Short.valueOf("1"), map.get("sf")); }