问题描述
在使用spring-data-mongodb-1.7.0.RELEASE.jar的时候,当我们存储的Map的key中含有“.”dot时,那么程序就会报错:
org.springframework.data.mapping.model.MappingException: Map key foo.bar.key contains dots but no replacement was configured! Make sure map keys don't contain dots in the first place or configure an appropriate replacement! at org.springframework.data.mongodb.core.convert.MappingMongoConverter.potentiallyEscapeMapKey(MappingMongoConverter.java:622) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:586) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:517) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:424) 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.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.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.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:310) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) at org.springframework.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)
这个错误出现的原因是因为 源代码
Spring => "MappingMongoConverter.java"
/**
* Potentially replaces dots in the given map key with the configured map key
* replacement if configured or aborts * conversion if none is configured.
* @see #setMapKeyDotReplacement(String)
* @param source
* @return */
protected String potentiallyEscapeMapKey(String source)
{
if (!source.contains("."))
{
return source;
}
if (mapKeyDotReplacement == null)
{
throw new MappingException(String.format(
"Map key %s contains dots but no replacement was configured! Make " +
" sure map keys don't contain dots in the first place or configure an appropriate replacement!", source))
}
return source.replaceAll("\\.", mapKeyDotReplacement);
}
解决方法
<bean id="mongoMoxydomainConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg index="0" ref="mongoDbFactory" />
<constructor-arg index="1">
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/
</constructor-arg>
<property name="mapKeyDotReplacement" value="\\+"/>
</bean>
网友评论