web-dev-qa-db-ja.com

「MongoMappingContext#setAutoIndexCreation(boolean)」を使用するか、「MongoConfigurationSupport#autoIndexCreation()」を明示的にオーバーライドしてください

私は https://docs.spring.io/spring-data/mongodb/docs/2.2.0.RC1/reference/html/#mapping-usage とWeb上の他のソースを調べましたが、解決策は私にとってうまくいきませんでした。

使ってます Spring Boot 2.2.2.RELEASEおよびSpring Data Mongo。この例では、Model/Pojoフィールドレベルで

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

エラー:

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);
> }
> -----------------------------------------------------------------------------------------

ログで示唆されているように、実装しましたが、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

次の設定で試してみましたが、独自のmappingContext Beanを作成しました

@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);
    }
}

それでも警告が表示されます。eventがapplication.propertiesでspring.data.mongodb.auto-index-creation = falseを試行しましたが、運もありませんでした

0
Tibor

しかし、このソリューションは、少なくとも事後的な状況ではうまく機能しません。現時点では、mongoMappingContext.getPersistentEntities()は空です。インデックスの作成を保証するのは_auto-index-creation: true_(またはmongoMappingContext.setAutoIndexCreation(true))だけです。

0
Gena Batalski