public static boolean setKeyValueIfneed(Object entity, PrimaryKey key, Object keyObj, long rowID) throws IllegalArgumentException, IllegalAccessException { if (key != null && key.isAssignedBySystem() && (keyObj == null || ((Number) keyObj).longValue() < 1)) { FieldUtil.set(key.field, entity, rowID); return true; } return false; }
private <E, T> boolean keepMapping(Collection<E> col1, Collection<T> col2) throws IllegalAccessException, InstantiationException { Class claxx1 = col1.iterator().next().getClass(); Class claxx2 = col2.iterator().next().getClass(); EntityTable table1 = TableManager.getTable(claxx1); EntityTable table2 = TableManager.getTable(claxx2); if (table1.mappingList != null) { for (MapProperty mp : table1.mappingList) { Class itemClass; Class fieldClass = mp.field.getType(); if (mp.isToMany()) { // N对多关系 if (ClassUtil.isCollection(fieldClass)) { itemClass = FieldUtil.getGenericType(mp.field); } else { throw new RuntimeException( "OneToMany and ManyToMany Relation, You must use collection object"); } } else { itemClass = fieldClass; } if (itemClass == claxx2) { ArrayList<String> key1List = new ArrayList<String>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); // 构建第1个对象的key集合以及value映射 for (Object o1 : col1) { if (o1 != null) { Object key1 = FieldUtil.get(table1.key.field, o1); if (key1 != null) { key1List.add(key1.toString()); map1.put(key1.toString(), o1); } } } ArrayList<Relation> mapList = queryRelation(claxx1, claxx2, key1List, null); if (!Checker.isEmpty(mapList)) { HashMap<String, Object> map2 = new HashMap<String, Object>(); // 构建第2个对象的value映射 for (Object o2 : col2) { if (o2 != null) { Object key2 = FieldUtil.get(table2.key.field, o2); if (key2 != null) { map2.put(key2.toString(), o2); } } } for (Relation m : mapList) { Object obj1 = map1.get(m.key1); Object obj2 = map2.get(m.key2); if (obj1 != null && obj2 != null) { if (mp.isToMany()) { // N对多关系 if (ClassUtil.isCollection(fieldClass)) { Collection col = (Collection) FieldUtil.get(mp.field, obj1); if (col == null) { col = (Collection) fieldClass.newInstance(); FieldUtil.set(mp.field, obj1, col); } col.add(obj2); } else { throw new RuntimeException( "OneToMany and ManyToMany Relation, You must use collection object"); } } else { FieldUtil.set(mp.field, obj1, obj2); } } } return true; } } } } return false; }