简体   繁体   中英

How to custmize FieldNamingStrategy in spring-data-mongo

In spring-data-mongo ,is this interface's function still can be extended?

What I want is saving CamelCaseToUnderline in mongodb and querying UnderlineToCamelCase.

I see the part source code, it has a final FieldNamingStrategy.if I wanna costomized it ,it must before instantiation, how?

public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty<MongoPersistentProperty> implements MongoPersistentProperty {
    ....
    private final FieldNamingStrategy fieldNamingStrategy;

    public BasicMongoPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, MongoPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder, FieldNamingStrategy fieldNamingStrategy) {

      super(field, propertyDescriptor, owner, simpleTypeHolder);
      this.fieldNamingStrategy = fieldNamingStrategy == null ? PropertyNameFieldNamingStrategy.INSTANCE : fieldNamingStrategy;

      if (isIdProperty() && getFieldName() != ID_FIELD_NAME) {
        LOG.warn("Customizing field name for id property not allowed! Custom name will not be considered!");
      }
    }
   ...
}

Hi this is from the spring boot appendix

# MONGODB (MongoProperties)
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use

You've got the follow strategies out of the box

CamelCaseSplittingFieldNamingStrategy
CamelCaseAbbreviatingFieldNamingStrategy
SnakeCaseFieldNamingStrategy

If these dont suit your needs, feel free to extend them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM