1 package com.supwisdom.institute.backend.common.framework.entity;
3 import java.lang.reflect.Field;
6 import javax.persistence.Column;
7 import javax.persistence.Id;
9 import com.supwisdom.institute.backend.common.util.ReflectUtils;
13 import lombok.ToString;
16 * 对 entity 的操作 如:复制、合并、转换等
21 public class EntityUtils {
24 * 合并 domain 中带有{@link Column}注解的字段值, 将 newEntity 中值为null的字段,使用 oldEntity 中的值
31 * @return 合并后的newEntity
33 public static <T> T merge(T oldEntity, T newEntity) {
35 for (Class<?> clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
36 for (Field field : clazz.getDeclaredFields()) {
37 Column[] annotations = field.getAnnotationsByType(Column.class);
38 if (annotations == null || annotations.length == 0) {
39 Id[] idAnnotations = field.getAnnotationsByType(Id.class);
40 if (idAnnotations == null || idAnnotations.length == 0) {
45 String fieldName = field.getName();
46 Object newFieldValue = ReflectUtils.getFieldValue(newEntity, fieldName);
48 if (newFieldValue == null) {
49 Object oldFieldValue = ReflectUtils.getFieldValue(oldEntity, fieldName);
50 ReflectUtils.setFieldValue(newEntity, fieldName, oldFieldValue,field.getType());
58 public static <S, T> T copy(S sourceEntity, T targetEntity) {
60 for (Class<?> clazz = targetEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
61 for (Field field : clazz.getDeclaredFields()) {
62 Column[] annotations = field.getAnnotationsByType(Column.class);
63 if (annotations == null || annotations.length == 0) {
64 Id[] idAnnotations = field.getAnnotationsByType(Id.class);
65 if (idAnnotations == null || idAnnotations.length == 0) {
70 String fieldName = field.getName();
71 Object sFieldValue = ReflectUtils.getFieldValue(sourceEntity, fieldName);
73 if (sFieldValue != null) {
74 ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue,field.getType());
82 public static <S, T> T fatherToChild (S father, T child){
83 for (Class<?> clazz = child.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
84 for (Field field : clazz.getDeclaredFields()) {
85 String fieldName = field.getName();
86 if(fieldName.equals("serialVersionUID")){continue;}
87 Object sFieldValue = ReflectUtils.getFieldValue(father, fieldName);
89 if (sFieldValue != null) {
90 ReflectUtils.setFieldValue(child, fieldName, sFieldValue,field.getType());
98 public static void main(String[] args) {
100 Test target0 = new Test();
101 target0.setId("id0");
102 target0.setCode("code");
103 target0.setName("name");
104 target0.setDate(new Date());
105 target0.setEnabled(false);
106 target0.setStatus(1);
108 System.out.println("target0 == " + target0.toString());
109 System.out.println();
111 Test source1 = new Test();
112 // source1.setId("id1");
113 source1.setCode("code1");
114 // source1.setName("name");
115 // source1.setDate(new Date());
116 source1.setEnabled(true);
117 // source1.setStatus(1);
118 System.out.println("source1 == " + source1.toString());
120 Test target1 = EntityUtils.merge(source1, target0);
121 System.out.println("target0 == " + target0.toString());
122 System.out.println("target1 == " + target1.toString());
123 System.out.println();
125 Test source2 = new Test();
126 // source2.setId("id2");
127 source2.setCode("code2");
128 source2.setName("name2");
129 // source2.setDate(new Date());
130 // source2.setEnabled(true);
131 source2.setStatus(2);
132 System.out.println("source2 == " + source2.toString());
134 Test target2 = EntityUtils.merge(source2, target0);
135 System.out.println("target0 == " + target0.toString());
136 System.out.println("target2 == " + target2.toString());
137 System.out.println();
140 Test test = new Test();
142 test.setCode("code");
143 test.setName("name");
144 test.setDate(new Date());
145 test.setEnabled(false);
148 Test2 test2 = new Test2();
149 test2 = EntityUtils.copy(test, test2);
150 System.out.println("test == " + test.toString());
151 System.out.println("test2 == " + test2.toString());
152 System.out.println();
159 public static class Test extends ABaseEntity {
164 private static final long serialVersionUID = -8348781653151879484L;
167 private String code = null;
169 private String name = null;
171 private Date date = null;
173 private Boolean enabled = null;
175 private Integer status = null;
179 public static class Test2 extends ABaseEntity {
184 private static final long serialVersionUID = -5565959639168005384L;
187 private String name = null;
189 private String memo = null;
191 private Date date = null;
193 private Boolean enabled = null;
195 private Integer status = null;