2017-09-05 54 views
0

我正在使用spring引導和mongoDB。已經編寫了我的模型庫,並編寫了我自己的邏輯,通過使用mongo inc來增加id。而這樣做org.springframework.data.mapping.model.MappingException:不能使用複雜的對象作爲鍵值

gradle這個測試

測試與下面的異常失敗。

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:322) 
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.$Proxy94.save(Unknown Source) 
at org.springframework.data.repository.CrudRepository$save.call(Unknown Source) 
at com.auth.mongo.impl.ApplicationRepositoryImpl.save(ApplicationRepositoryImpl.groovy:26) 
at com.auth.repository.ApplicationRepository$save$0.call(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) 

任何一個可以建議我什麼我做錯

編輯: 例如:應用程序類是域名

class Application implements Serializable { 

String id 
APIKey apiKey 
Map actions = [:] 

Application() { 
} 

Application(APIKey apiKey) { 
    this.apiKey = apiKey 
    List customerActionsList = [] // added some actions 
    actions.put(Role.User, customerActionsList) 
} 

APIKey:

String apiKey 
String application 
DateTime whenCreated 
boolean active 

APIKey() { 
} 

APIKey(String application, DateTime whenCreated, boolean active) { 
    this.apiKey = generateAPIKey(application) 
    this.application = application 
    this.whenCreated = whenCreated 
    this.active = active 
} 

庫分類如下:

@Component 
class ApplicationRepositoryImpl implements ApplicationRepository { 

private final Logger logger = LoggerFactory.getLogger(ApplicationRepositoryImpl.class) 

@Autowired 
ApplicationRepositoryMongo applicationRepositoryMongo 

@Autowired 
SequenceRepository sequenceRepository 

@Override 
Serializable save(Application application) { 
    application.id = application.id?:sequenceRepository.getNextSequenceId(Application.simpleName).sequence 
    return applicationRepositoryMongo.save(application).id 
} 

@Override 
Application find(String id) { 
    return applicationRepositoryMongo.findOne(id) 
} 

我增加應用類的ID如下

public SequenceId getNextSequenceId(String type) { 
    //get sequence type 
    Query query = new Query(Criteria.where("id").is(type)) 
    //increase sequence id by 
    Update update = new Update() 
    update.inc('sequence', 1) 
    //return new increased i 
    FindAndModifyOptions options = new FindAndModifyOptions() 
    options.returnNew(true) 
    //this is the magic happened 
    SequenceId seqId = mongoOperation.findAndModify(query, update, options, SequenceId.class) 
    return seqId 
} 

同時節省了域對象獲取上面的異常。我已經完成了所有的mongo配置。該配置適用於其他域。

+0

異常似乎很清楚(你 - 沒有人可以幫助你不用代碼) –

+0

嘿@Jacek鋯石我已經更新了我的代碼的問題。請現在看看和幫助 – SatishY

回答

相關問題