java 通过反射原理进行复制操作

// 这些实例的对象字段[sCode]
		String[] copyEntityArr = new String[] { "xxx", "xxx" };
		List<Object> insertBatchList = new ArrayList<Object>();
		for (String entity : copyEntityArr) {
			// 可能有多个配置信息
			List objList = this.queryHiber("from " + entity + " where sCode=?", new Object[] { scenCode });
			if (objList != null) {
				for (Object obj : objList) {
					Object newObj = UtilBean.cloneBean(obj);
					Class clazz = newObj.getClass();
					try {
						Method setId = clazz.getDeclaredMethod("setId", String.class);
						Method setScenarioCode = clazz.getDeclaredMethod("setScenarioCode", String.class);
						// 重新生成主键{ID}
						setId.invoke(newObj, UUIDGenerator.getUUID());
						setScenarioCode.invoke(newObj, newScenarioCode);
						insertBatchList.add(newObj);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}

猜你喜欢

转载自forlan.iteye.com/blog/2383863