正在使用spring boot和mongoDB。已经为我的模型编写了存储库,并编写了我自己的逻辑来使用mongo inc增加id。在做的时候
gradle test
测试失败,但有以下异常。
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)
}
我正在递增Application类的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)
春天有一个开放的改进Don't require Converters for Complex classes that are used as Ids。目前Spring决定不支持它。
决定不支持它