1 package com.supwisdom.institute.backend.common.framework.entity;
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Modifier;
7 import javax.persistence.Column;
8 import javax.persistence.Id;
10 import com.supwisdom.institute.backend.common.util.ReflectUtils;
14 import lombok.ToString;
17 * 对 entity 的操作 如:复制、合并、转换等
22 public class EntityUtils {
25 * 合并 domain 中带有{@link Column}注解的字段值, 将 newEntity 中值为null的字段,使用 oldEntity 中的值
32 * @return 合并后的newEntity
34 public static <T extends ABaseEntity> T merge(T oldEntity, T newEntity) {
36 for (Class<?> clazz = oldEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
37 for (Field field : clazz.getDeclaredFields()) {
38 Column[] annotations = field.getAnnotationsByType(Column.class);
39 if (annotations == null || annotations.length == 0) {
40 Id[] idAnnotations = field.getAnnotationsByType(Id.class);
41 if (idAnnotations == null || idAnnotations.length == 0) {
46 String fieldName = field.getName();
47 Object newFieldValue = ReflectUtils.getFieldValue(newEntity, fieldName);
49 if (newFieldValue == null) {
50 Object oldFieldValue = ReflectUtils.getFieldValue(oldEntity, fieldName);
51 ReflectUtils.setFieldValue(newEntity, fieldName, oldFieldValue,field.getType());
59 public static <S, T> T copy(S sourceEntity, T targetEntity) {
61 for (Class<?> clazz = targetEntity.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
62 for (Field field : clazz.getDeclaredFields()) {
64 // Column[] annotations = field.getAnnotationsByType(Column.class);
65 // if (annotations == null || annotations.length == 0) {
66 // Id[] idAnnotations = field.getAnnotationsByType(Id.class);
67 // if (idAnnotations == null || idAnnotations.length == 0) {
72 if (Modifier.isStatic(field.getModifiers())) {
76 String fieldName = field.getName();
78 if(fieldName.equals("serialVersionUID")){
81 if (!ReflectUtils.existField(sourceEntity, fieldName)) {
85 Object sFieldValue = ReflectUtils.getFieldValue(sourceEntity, fieldName);
87 if (sFieldValue != null) {
88 ReflectUtils.setFieldValue(targetEntity, fieldName, sFieldValue, field.getType());
96 public static <F, C> C fatherToChild (F father, C child){
97 for (Class<?> clazz = child.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
98 for (Field field : clazz.getDeclaredFields()) {
99 if (Modifier.isStatic(field.getModifiers())) {
103 String fieldName = field.getName();
104 if(fieldName.equals("serialVersionUID")){
107 if (!ReflectUtils.existField(father, fieldName)) {
111 Object sFieldValue = ReflectUtils.getFieldValue(father, fieldName);
113 if (sFieldValue != null) {
114 ReflectUtils.setFieldValue(child, fieldName, sFieldValue, field.getType());
122 public static void main(String[] args) {
124 Test target0 = new Test();
125 target0.setId("id0");
126 target0.setCode("code");
127 target0.setName("name");
128 target0.setDate(new Date());
129 target0.setEnabled(false);
130 target0.setStatus(1);
132 System.out.println("target0 == " + target0.toString());
133 System.out.println();
135 Test source1 = new Test();
136 // source1.setId("id1");
137 source1.setCode("code1");
138 // source1.setName("name");
139 // source1.setDate(new Date());
140 source1.setEnabled(true);
141 // source1.setStatus(1);
142 System.out.println("source1 == " + source1.toString());
144 Test target1 = EntityUtils.merge(source1, target0);
145 System.out.println("target0 == " + target0.toString());
146 System.out.println("target1 == " + target1.toString());
147 System.out.println();
149 Test source2 = new Test();
150 // source2.setId("id2");
151 source2.setCode("code2");
152 source2.setName("name2");
153 // source2.setDate(new Date());
154 // source2.setEnabled(true);
155 source2.setStatus(2);
156 System.out.println("source2 == " + source2.toString());
158 Test target2 = EntityUtils.merge(source2, target0);
159 System.out.println("target0 == " + target0.toString());
160 System.out.println("target2 == " + target2.toString());
161 System.out.println();
164 Test test = new Test();
166 test.setCode("code");
167 test.setName("name");
168 test.setDate(new Date());
169 test.setEnabled(false);
172 Test2 test2 = new Test2();
173 test2 = EntityUtils.copy(test, test2);
174 System.out.println("test == " + test.toString());
175 System.out.println("test2 == " + test2.toString());
176 System.out.println();
183 public static class Test extends ABaseEntity {
188 private static final long serialVersionUID = -8348781653151879484L;
191 private String code = null;
193 private String name = null;
195 private Date date = null;
197 private Boolean enabled = null;
199 private Integer status = null;
203 public static class Test2 extends ABaseEntity {
208 private static final long serialVersionUID = -5565959639168005384L;
211 private String name = null;
213 private String memo = null;
215 private Date date = null;
217 private Boolean enabled = null;
219 private Integer status = null;