|
17 | 17 | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
18 | 18 | import org.springframework.context.index.CandidateComponentsIndex; |
19 | 19 | import org.springframework.context.index.CandidateComponentsIndexLoader; |
| 20 | +import org.springframework.core.GenericTypeResolver; |
20 | 21 | import org.springframework.core.ResolvableType; |
21 | 22 | import org.springframework.core.annotation.AnnotationUtils; |
22 | 23 | import org.springframework.core.io.Resource; |
|
27 | 28 | import org.springframework.core.type.classreading.MetadataReader; |
28 | 29 | import org.springframework.core.type.classreading.MetadataReaderFactory; |
29 | 30 | import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; |
| 31 | +import org.springframework.util.ReflectionUtils; |
30 | 32 |
|
31 | 33 | import javax.persistence.Table; |
32 | 34 | import java.io.IOException; |
@@ -79,6 +81,34 @@ protected Set<String> scanEntities(String[] packageStr) { |
79 | 81 | .collect(Collectors.toSet()); |
80 | 82 | } |
81 | 83 |
|
| 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 | + |
82 | 112 | @Override |
83 | 113 | @SneakyThrows |
84 | 114 | @SuppressWarnings("all") |
@@ -106,18 +136,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B |
106 | 136 |
|
107 | 137 | Reactive reactive = AnnotationUtils.findAnnotation(entityType, Reactive.class); |
108 | 138 |
|
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); |
121 | 140 |
|
122 | 141 | EntityInfo entityInfo = new EntityInfo(entityType, |
123 | 142 | entityType, |
|
0 commit comments