MongoDB不能将复杂对象用作Java的键值

时间:2016-12-05 04:21:27

标签: java spring mongodb graph spring-data-mongodb

我正在实现一个对象,该对象表示要存储在MongoDb中的图形。这是类定义:

@Document(collection = "mygraph")
public class MyGraph {

    @Id
    @JsonSerialize(using = ToStringSerializer.class)
    public String id;
    public Map<Instance, Set<Instance>> graph;
    public String timestamp;

    /**
     * Add the default constructor
     */
    public MyGraph() {
        this.id = new ObjectId().toHexString();
        this.graph = new LinkedHashMap<>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        this.timestamp = dateFormat.format(new Date());
    }

        /**
     * 
     * @param instances
     */
    public MyGraph(Set<Instance> instances) {
        this.id = new ObjectId().toHexString();
        this.graph = new LinkedHashMap<>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        this.timestamp = dateFormat.format(new Date());

        /* Initialize the graph */
        for (Instance instance : instances) {
            this.graph.put(instance, new HashSet<>());
        }
    }
    // get and set method are below
} 

但是我收到以下错误:

Time elapsed: 0.03 sec  <<< FAILURE!
org.springframework.data.mapping.model.MappingException: Cannot use a complex object as a key value.
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:669)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:585)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:471)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:430)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:418)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:312)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:418)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:392)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:356)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:79)
    at org.springframework.data.mongodb.core.MongoTemplate.toDbObject(MongoTemplate.java:853)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1014)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:963)
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy197.save(Unknown Source)
    at models.CloudGraphTest.testSaveCloudGraph(CloudGraphTest.java:79)

如何更改我的MappingMongoConverter以适应它?

1 个答案:

答案 0 :(得分:1)

春天有一个开放的改进:Don't require Converters for Complex classes that are used as Ids

不幸的是,目前Spring团队决定不支持它。

  

决定不支持它

修改

有一个与开放相关的问题Allow using complex types as Map keys,可能包含稍后修复