web-dev-qa-db-fra.com

Veuillez utiliser 'MongoMappingContext # setAutoIndexCreation (boolean)' ou remplacer 'MongoConfigurationSupport # autoIndexCreation ()' pour être explicite

Je suis passé par https://docs.spring.io/spring-data/mongodb/docs/2.2.0.RC1/reference/html/#mapping-usage et d'autres sources sur le Web, mais la solution n'a pas fonctionné pour moi.

J'utilise Spring Boot 2.2.2.RELEASE et Spring Data Mongo. Dans cet exemple, au niveau du champ Model/Pojo, nous utilisons

@Indexed(name = AppConstants.FIRSTNAME_INDEX, direction = IndexDirection.ASCENDING)
private String firstName;

Erreur:

Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.

> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
>
>     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
>     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
>     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------

Comme suggéré dans le journal, j'ai implémenté, mais je ne vois pas la méthode setAutoIndexCreation.

public class MongoConfig extends AbstractMongoClientConfiguration {

    @Override
    public MongoClient mongoClient() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String getDatabaseName() {
        // TODO Auto-generated method stub
        return null;
    }
}
7
Pra_A

essayé avec la configuration suivante, j'ai même créé un propre bean mappingContext

@Configuration
public class MongoConfiguration extends AbstractMongoClientConfiguration {

    private final AbstractCloudConfig abstractCloudConfig;

    @Autowired
    public MongoConfiguration(AbstractCloudConfig abstractCloudConfig) {
        this.abstractCloudConfig = abstractCloudConfig;
    }

    @Override
    public MongoClient mongoClient() {
        return MongoClients.create(MongoClientSettings.builder().build());
    }

    @Bean
    @Override
    public MongoDbFactory mongoDbFactory() {
        return abstractCloudConfig.connectionFactory().mongoDbFactory();
    }

    @Bean
    public MappingMongoConverter mongoConverter(MappingMongoConverter mongoConverter) {
        /*
         * For some obscure reason, Spring Mongo does not like hash maps with keys that contain dots '.'.
         * We configure to replace dots '.' in maps with some obscure character that does not occur in in keys.
         */
        mongoConverter.setMapKeyDotReplacement("~");
        return mongoConverter;
    }

    @Bean
    public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
        MongoMappingContext mappingContext = new MongoMappingContext();
        mappingContext.setInitialEntitySet(this.getInitialEntitySet());
        mappingContext.setSimpleTypeHolder(this.customConversions().getSimpleTypeHolder());
        mappingContext.setFieldNamingStrategy(this.fieldNamingStrategy());
        mappingContext.setAutoIndexCreation(false);
        return mappingContext;
    }

    @Override
    protected String getDatabaseName() {
        throw new RuntimeException("This method should not be called");
    }

    @EventListener(ApplicationReadyEvent.class)
    public void initIndicesAfterStartup() throws ClassNotFoundException {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        this.mongoMappingContext().setAutoIndexCreation(false);
        createIndexForEntity(DataSetDefinitionV2.class, mongoTemplate);
        createIndexForEntity(ModelConfiguration.class, mongoTemplate);
        createIndexForEntity(ScheduleEntity.class, mongoTemplate);
    }

    private void createIndexForEntity(Class<?> entityClass, MongoTemplate mongoTemplate) throws ClassNotFoundException {
        IndexOperations indexOps = mongoTemplate.indexOps(entityClass);
        IndexResolver resolver = new MongoPersistentEntityIndexResolver(this.mongoMappingContext());
        resolver.resolveIndexFor(entityClass).forEach(indexOps::ensureIndex);
    }
}

toujours l'avertissement vient.J'ai essayé spring.data.mongodb.auto-index-creation = false dans application.properties, pas de chance non plus

0
Tibor

Mais la solution ne fonctionne pas bien, du moins dans le contexte réactif. La mongoMappingContext.getPersistentEntities() est vide à ce moment. Seuls auto-index-creation: true (Voire mongoMappingContext.setAutoIndexCreation(true)) assurent la création d'index.

0
Gena Batalski