From adc334e7c7587be37839300413d2d867377c10de Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=88=98=E6=B4=AA=E9=9D=92?= Date: Tue, 3 Sep 2019 09:14:03 +0800 Subject: [PATCH] =?utf8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=B7=A5=E5=85=B7?= =?utf8?q?=E7=B1=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../common/framework/entity/EntityUtils.java | 46 ++++++++++++++----- .../backend/common/util/ReflectUtils.java | 7 ++- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java b/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java index 662b24a..902c592 100644 --- a/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java +++ b/common/framework/src/main/java/com/supwisdom/institute/backend/common/framework/entity/EntityUtils.java @@ -1,6 +1,7 @@ package com.supwisdom.institute.backend.common.framework.entity; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.Date; import javax.persistence.Column; @@ -30,7 +31,7 @@ public class EntityUtils { * ,待覆盖的实体 * @return 合并后的newEntity */ - public static T merge(T oldEntity, T newEntity) { + public static T merge(T oldEntity, T newEntity) { for (Class clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { @@ -59,19 +60,32 @@ public class EntityUtils { for (Class clazz = targetEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { - Column[] annotations = field.getAnnotationsByType(Column.class); - if (annotations == null || annotations.length == 0) { - Id[] idAnnotations = field.getAnnotationsByType(Id.class); - if (idAnnotations == null || idAnnotations.length == 0) { - continue; - } + +// Column[] annotations = field.getAnnotationsByType(Column.class); +// if (annotations == null || annotations.length == 0) { +// Id[] idAnnotations = field.getAnnotationsByType(Id.class); +// if (idAnnotations == null || idAnnotations.length == 0) { +// continue; +// } +// } + + if (Modifier.isStatic(field.getModifiers())) { + continue; } String fieldName = field.getName(); + + if(fieldName.equals("serialVersionUID")){ + continue; + } + if (!ReflectUtils.existField(sourceEntity, fieldName)) { + continue; + } + Object sFieldValue = ReflectUtils.getFieldValue(sourceEntity, fieldName); if (sFieldValue != null) { - ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue,field.getType()); + ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue, field.getType()); } } } @@ -79,15 +93,25 @@ public class EntityUtils { return targetEntity; } - public static T fatherToChild (S father, T child){ + public static C fatherToChild (F father, C child){ for (Class clazz = child.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { + if (Modifier.isStatic(field.getModifiers())) { + continue; + } + String fieldName = field.getName(); - if(fieldName.equals("serialVersionUID")){continue;} + if(fieldName.equals("serialVersionUID")){ + continue; + } + if (!ReflectUtils.existField(father, fieldName)) { + continue; + } + Object sFieldValue = ReflectUtils.getFieldValue(father, fieldName); if (sFieldValue != null) { - ReflectUtils.setFieldValue(child, fieldName, sFieldValue,field.getType()); + ReflectUtils.setFieldValue(child, fieldName, sFieldValue, field.getType()); } } } diff --git a/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java b/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java index 1b9313e..cddd545 100644 --- a/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java +++ b/common/utils/src/main/java/com/supwisdom/institute/backend/common/util/ReflectUtils.java @@ -33,7 +33,7 @@ public class ReflectUtils { * 目标属性 * @return 目标字段 */ - private static Field getField(Object obj, String fieldName) { + public static Field getField(Object obj, String fieldName) { Field field = null; for (Class clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { try { @@ -45,6 +45,11 @@ public class ReflectUtils { } return field; } + + public static boolean existField(Object obj, String fieldName) { + Field field = getField(obj, fieldName); + return field != null; + } /** * 调用Getter方法. -- 2.17.1