From 04963ffabd511302f7dd334a4e712c1d5fb750d5 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 1 Aug 2023 16:19:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E7=B1=BBid=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EasyormRepositoryRegistrar.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/EasyormRepositoryRegistrar.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/EasyormRepositoryRegistrar.java index bd6575bb7..b3cc8ba02 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/EasyormRepositoryRegistrar.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/configuration/EasyormRepositoryRegistrar.java @@ -17,6 +17,7 @@ import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.index.CandidateComponentsIndex; import org.springframework.context.index.CandidateComponentsIndexLoader; +import org.springframework.core.GenericTypeResolver; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.io.Resource; @@ -27,6 +28,7 @@ import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; +import org.springframework.util.ReflectionUtils; import javax.persistence.Table; import java.io.IOException; @@ -79,6 +81,34 @@ protected Set scanEntities(String[] packageStr) { .collect(Collectors.toSet()); } + private Class findIdType(Class entityType) { + Class idType; + try { + if (GenericEntity.class.isAssignableFrom(entityType)) { + return GenericTypeResolver.resolveTypeArgument(entityType, GenericEntity.class); + } + + Class[] ref = new Class[1]; + ReflectionUtils.doWithFields(entityType, field -> { + if (field.isAnnotationPresent(javax.persistence.Id.class)) { + ref[0] = field.getType(); + } + }); + idType = ref[0]; + + if (idType == null) { + Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId"); + idType = getId.getReturnType(); + } + } catch (Throwable e) { + log.warn("unknown id type of entity:{}", entityType); + idType = String.class; + } + + return idType; + + } + @Override @SneakyThrows @SuppressWarnings("all") @@ -106,18 +136,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B Reactive reactive = AnnotationUtils.findAnnotation(entityType, Reactive.class); - Class idType = null; - try { - if (GenericEntity.class.isAssignableFrom(entityType)) { - idType = ClassUtils.getGenericType(entityType); - } - if (idType == null) { - Method getId = org.springframework.util.ClassUtils.getMethod(entityType, "getId"); - idType = getId.getReturnType(); - } - } catch (Exception e) { - idType = String.class; - } + Class idType = findIdType(entityType); EntityInfo entityInfo = new EntityInfo(entityType, entityType,