Skip to content

Commit 04963ff

Browse files
committed
refactor: 优化实体类id字段类型获取
1 parent df35260 commit 04963ff

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/EasyormRepositoryRegistrar.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
1818
import org.springframework.context.index.CandidateComponentsIndex;
1919
import org.springframework.context.index.CandidateComponentsIndexLoader;
20+
import org.springframework.core.GenericTypeResolver;
2021
import org.springframework.core.ResolvableType;
2122
import org.springframework.core.annotation.AnnotationUtils;
2223
import org.springframework.core.io.Resource;
@@ -27,6 +28,7 @@
2728
import org.springframework.core.type.classreading.MetadataReader;
2829
import org.springframework.core.type.classreading.MetadataReaderFactory;
2930
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
31+
import org.springframework.util.ReflectionUtils;
3032

3133
import javax.persistence.Table;
3234
import java.io.IOException;
@@ -79,6 +81,34 @@ protected Set<String> scanEntities(String[] packageStr) {
7981
.collect(Collectors.toSet());
8082
}
8183

84+
private Class<?> findIdType(Class<?> entityType) {
85+
Class<?> idType;
86+
try {
87+
if (GenericEntity.class.isAssignableFrom(entityType)) {
88+
return GenericTypeResolver.resolveTypeArgument(entityType, GenericEntity.class);
89+
}
90+
91+
Class<?>[] ref = new Class[1];
92+
ReflectionUtils.doWithFields(entityType, field -> {
93+
if (field.isAnnotationPresent(javax.persistence.Id.class)) {
94+
ref[0] = field.getType();
95+
}
96+
});
97+
idType = ref[0];
98+
99+
if (idType == null) {
100+
Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId");
101+
idType = getId.getReturnType();
102+
}
103+
} catch (Throwable e) {
104+
log.warn("unknown id type of entity:{}", entityType);
105+
idType = String.class;
106+
}
107+
108+
return idType;
109+
110+
}
111+
82112
@Override
83113
@SneakyThrows
84114
@SuppressWarnings("all")
@@ -106,18 +136,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
106136

107137
Reactive reactive = AnnotationUtils.findAnnotation(entityType, Reactive.class);
108138

109-
Class idType = null;
110-
try {
111-
if (GenericEntity.class.isAssignableFrom(entityType)) {
112-
idType = ClassUtils.getGenericType(entityType);
113-
}
114-
if (idType == null) {
115-
Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId");
116-
idType = getId.getReturnType();
117-
}
118-
} catch (Exception e) {
119-
idType = String.class;
120-
}
139+
Class idType = findIdType(entityType);
121140

122141
EntityInfo entityInfo = new EntityInfo(entityType,
123142
entityType,

0 commit comments

Comments
 (0)