mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-11 03:13:51 +00:00
style[orm]: code style
This commit is contained in:
1 parent
d86c7e6ead
commit
a455317925
2 files changed
+19
-23
No files matched your search
@@ -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 @@ import java.lang.reflect.Field;
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -105,9 +105,11 @@ public class OrmManager implements IOrmManager {
|
||||
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));
|
||||
|
||||
|
||||
Reference in new issue
Block a user