1

我有一个包含 HashMap 的用户类,用于在两个自定义对象之间进行映射。

目前,每当我尝试在 mongodb 中保存一个用户时,我都会得到:

org.springframework.data.mapping.model.MappingException: Map key net.bigpoint.globalchat.models.keys.ProjectId@0 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:548) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:512) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:385) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:346) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:335) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:173) ~[spring-data-commons-core-1.2.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:335) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:307) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:73) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:717) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:703) ~[spring-data-mongodb-1.0.1.RELEASE.jar:na]
at net.bigpoint.globalchat.hazelcast.MongoMapStore.store(MongoMapStore.java:81) [classes/:na]
at net.bigpoint.globalchat.hazelcast.MongoMapStore.store(MongoMapStore.java:1) [classes/:na]
at com.hazelcast.impl.concurrentmap.MapStoreWrapper.store(MapStoreWrapper.java:110) [hazelcast-2.0.2.jar:2.0.2]
at com.hazelcast.impl.ConcurrentMapManager$PutOperationHandler$PutStorer.doMapStoreOperation(ConcurrentMapManager.java:2736) [hazelcast-2.0.2.jar:2.0.2]
at com.hazelcast.impl.ConcurrentMapManager$AbstractMapStoreOperation.run(ConcurrentMapManager.java:3440) [hazelcast-2.0.2.jar:2.0.2]
at com.hazelcast.impl.executor.ParallelExecutorService$ParallelExecutorImpl$ExecutionSegment.run(ParallelExecutorService.java:212) [hazelcast-2.0.2.jar:2.0.2]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_04]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_04]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_04]

在我看来,spring data 试图通过调用 toString 来序列化键,这是不可能反序列化的。

我已经有两个类的转换器,可以将实例转换为字符串,反之亦然,那么我的设置有什么问题?

编辑

由于某些讨厌的原因,MappingMongoConverter 类不使用转换器将键转换为可管理的东西,它只是使用 toString 将键转换为字符串并将字符串写入 DBObject。

在反序列化期间,字符串(这里作为对象处理)被转换为正确的键类型......

4

1 回答 1

1

我不确定该行为是否是有意的,但正如我在编辑中所描述的那样,映射键通过调用 toString(适用于 Float、Double 等)进行“序列化”,并通过使用配置的转换器进行反序列化。

所以我的解决方法是在类 toString 方法中调用我的 complexClassToString 转换器。这只是可能的,因为我已经有了这个转换器,因为有问题的类在我的模型中用作 ID。

于 2012-05-11T14:23:28.233 回答