2016-09-30 54 views
2

地圖鍵代碼產品包含點,但未配置替換!確保地圖鍵首先不包含點或配置適當的替換!MongoDB-Escape dots'。'在地圖鍵中]

org.springframework.data.mapping.model.MappingException:映射鍵foo.bar.key包含點但未配置替換!確保地圖鍵首先不包含點或配置適當的替換! 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.potentiallyEscapeMapKey(MappingMongoConverter.java:622) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:586) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:517) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:424) 的組織。 springframework.data.mongodb.core.convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter.java:386) at org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter.java:373) at org。 springframework.d ata.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at org.springframework.data.mongodb。 core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter.java:386) at org.springframework.data.mongodb.core .convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter.java:373) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert。 MappingMongoConverter.writeInternal(MappingMongoC onverter.java:373) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter .java:386) at org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 3.doWithPersistentProperty(MappingMongoConverter.java:373) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java: 257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:345) at org.springframe在org.springframework上的org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) 上的work.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:310) 。 data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:859) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:806) at org.springframework.data.mongodb.core。 MongoTemplate.save(MongoTemplate.java:794)

當我們嘗試插入值時,會發生這種情況。我們如何解決這個問題?

這是我的課

@Configuration 
@EnableMongoRepositories("net.ooo.hepsiburada.**.repository") 
@Profile(Constants.SPRING_PROFILE_CLOUD) 
public class CloudMongoDbConfiguration extends AbstractMongoConfiguration { 

    private final Logger log = LoggerFactory.getLogger(CloudDatabaseConfiguration.class); 

    @Inject 
    private MongoDbFactory mongoDbFactory; 

    @Bean 
    public ValidatingMongoEventListener validatingMongoEventListener() { 
     return new ValidatingMongoEventListener(validator()); 
    } 

    @Bean 
    public LocalValidatorFactoryBean validator() { 
     return new LocalValidatorFactoryBean(); 
    } 

    @Bean 
    public CustomConversions customConversions() { 
     List<Converter<?, ?>> converterList = new ArrayList<>();; 
     converterList.add(DateToZonedDateTimeConverter.INSTANCE); 
     converterList.add(ZonedDateTimeToDateConverter.INSTANCE); 
     converterList.add(DateToLocalDateConverter.INSTANCE); 
     converterList.add(LocalDateToDateConverter.INSTANCE); 
     converterList.add(DateToLocalDateTimeConverter.INSTANCE); 
     converterList.add(LocalDateTimeToDateConverter.INSTANCE); 
     return new CustomConversions(converterList); 
    } 

    @Override 
    protected String getDatabaseName() { 
     return mongoDbFactory.getDb().getName(); 
    } 

    @Override 
    public Mongo mongo() throws Exception { 
     return mongoDbFactory().getDb().getMongo(); 
    } 
} 

回答

4

當使用Spring數據的MongoDB你得到的一個實例:org.springframework.data.mongodb.core.convert.MappingMongoConvertermapKeyDotReplacement默認設置爲空 - 這就是爲什麼你得到一個例外。

您需要創建自己的org.springframework.data.mongodb.core.convert.MappingMongoConverter實例,或者只是使用其提供的setter方法修改現有實例:

/** 
* Configure the characters dots potentially contained in a {@link Map} shall be replaced with. By default we don't do 
* any translation but rather reject a {@link Map} with keys containing dots causing the conversion for the entire 
* object to fail. If further customization of the translation is needed, have a look at 
* {@link #potentiallyEscapeMapKey(String)} as well as {@link #potentiallyUnescapeMapKey(String)}. 
* 
* @param mapKeyDotReplacement the mapKeyDotReplacement to set 
*/ 
public void setMapKeyDotReplacement(String mapKeyDotReplacement) { 
    this.mapKeyDotReplacement = mapKeyDotReplacement; 
} 

在MongoDB中,點始終被視爲一個特殊字符,以便避免它很可能會救你將來會有一些其他的頭痛。

編輯: 覆蓋默認MappingMongoConverter添加以下Bean聲明:

@Bean 
    public MappingMongoConverter mongoConverter(MongoDbFactory mongoFactory) throws Exception { 
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory); 
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext); 
    mongoConverter.setMapKeyDotReplacement("."); 

    return mongoConverter; 
    } 
+0

我在哪裏可以進行此修改? – mark

+0

看到這個問答:http://stackoverflow.com/questions/35598595/how-to-customize-mappingmongoconverter-setmapkeydotreplacement-in-spring-boot –

+0

我添加了我的課程。在那個班裏面?但是如何? – mark

0

對於以下將是有益的XML配置。

注意:mongoConverter bean用於此目的。它將取代「。」 in with key with「_」

<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> 

<mongo:auditing mapping-context-ref="mappingContext"/> 

<mongo:db-factory id="mongoDbFactory" mongo-ref="mongoClient" dbname="${mongo.dbname}"/> 

<bean id ="mongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> 
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> 
    <constructor-arg name="mappingContext" ref="mappingContext"/> 
    <property name="mapKeyDotReplacement" value="_"></property> 
</bean> 

<mongo:mongo-client id="mongoClient" credentials="${mongo.credential}" > 
    <mongo:client-options connections-per-host="50" threads-allowed-to-block-for-connection-multiplier="5000" /> 
</mongo:mongo-client> 

<!-- MongoDB Template --> 
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> 
    <constructor-arg name="mongoConverter" ref="mongoConverter"/> 
</bean>