目录

Cannot-autogenerate-id-of-type-java.lang.Integer-for-entity

Cannot autogenerate id of type java.lang.Integer for entity

在直接对MongoDB中插入实体对象的时候出现了

Cannot autogenerate id of type java.lang.Integer for entity

org.springframework.dao.InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Integer for entity of type com.zsf.spider.model.Article!
	at org.springframework.data.mongodb.core.EntityOperations$MappedEntity.assertUpdateableIdIfNotSet(EntityOperations.java:537)
	at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:1238)
	at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1178)
	at com.zsf.spider.SpiderArticle.start(SpiderArticle.java:118)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

MongoDB无法为实体自动生成java.lang.integer类型的ID

MongoDB的ObjectId的序列化问题

因为在自己的实体中指定了id

https://i-blog.csdnimg.cn/blog_migrate/517444837d37882b25821c60aa19f305.png

MongoDB在不特殊指认的情况下,默认的集合主键是“_id”,类型是ObjectId。ObjectId是一个12字节的BSON类型字符串,包含了UNIX时间戳,机器识别码,进程号,计数值信息。机器码用来防止分布式系统生成id时冲突的问题,保证每台机器生成的识别码不同,进程号保证多线程情况下生成的id不同。

将实体中的id的类型去掉或者修改为ObjectId