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;
* ,待覆盖的实体
* @return 合并后的newEntity
*/
- public static <T> T merge(T oldEntity, T newEntity) {
+ public static <T extends ABaseEntity> T merge(T oldEntity, T newEntity) {
for (Class<?> clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
for (Field field : clazz.getDeclaredFields()) {
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());
}
}
}
return targetEntity;
}
- public static <S, T> T fatherToChild (S father, T child){
+ public static <F, C> 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());
}
}
}