diff --git a/orm/src/main/java/com/zfoo/orm/convention/AnnotationConvention.java b/orm/src/main/java/com/zfoo/orm/convention/AnnotationConvention.java index 11c2422da..92a4ec03b 100644 --- a/orm/src/main/java/com/zfoo/orm/convention/AnnotationConvention.java +++ b/orm/src/main/java/com/zfoo/orm/convention/AnnotationConvention.java @@ -1,6 +1,8 @@ package com.zfoo.orm.convention; import com.zfoo.orm.anno.Id; +import com.zfoo.protocol.exception.RunException; +import com.zfoo.protocol.util.ReflectionUtils; import org.bson.codecs.pojo.ClassModelBuilder; import org.bson.codecs.pojo.Convention; import org.bson.codecs.pojo.PropertyModelBuilder; @@ -16,37 +18,29 @@ * @since 2024/7/30 */ public class AnnotationConvention implements Convention { + public static final AnnotationConvention INSTANCE = new AnnotationConvention(); @Override public void apply(ClassModelBuilder classModelBuilder) { String idPropertyName = classModelBuilder.getIdPropertyName(); if (idPropertyName != null) { - try { - Field idField = classModelBuilder.getType().getDeclaredField(idPropertyName); - Id annotation = idField.getAnnotation(Id.class); - if (annotation == null) { - throw new RuntimeException("The class " + classModelBuilder.getType().getName() + - " has an id property[name=" + idPropertyName + "] but no @Id annotation"); - } - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); + Field idField = ReflectionUtils.getFieldByNameInPOJOClass(classModelBuilder.getType(), idPropertyName); + Id annotation = idField.getAnnotation(Id.class); + if (annotation == null) { + throw new RunException("The class:[{}] has an id property:[{}] but no @Id annotation", classModelBuilder.getType().getName(), idPropertyName); } return; } for (PropertyModelBuilder propertyModelBuilder : classModelBuilder.getPropertyModelBuilders()) { - processPropertyAnnotations(classModelBuilder, propertyModelBuilder); - } - } - - private void processPropertyAnnotations(final ClassModelBuilder classModelBuilder, - final PropertyModelBuilder propertyModelBuilder) { - for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) { - if (annotation.annotationType().equals(Id.class)) { - String fieldName = propertyModelBuilder.getName(); - classModelBuilder.idPropertyName(fieldName); - break; + for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) { + if (annotation.annotationType().equals(Id.class)) { + String fieldName = propertyModelBuilder.getName(); + classModelBuilder.idPropertyName(fieldName); + break; + } } } } -} + +} \ No newline at end of file diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index f746787eb..31f5ea84b 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -105,9 +105,11 @@ public void initBefore() { allEntityCachesUsableMap.put(entityClass, false); } - var pojoCodecProvider = PojoCodecProvider.builder().automatic(true) + var pojoCodecProvider = PojoCodecProvider.builder() + .automatic(true) .conventions(List.of(Conventions.ANNOTATION_CONVENTION, AnnotationConvention.INSTANCE)) - .register(new MapCodecProvider()).build(); + .register(new MapCodecProvider()) + .build(); var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));