数据库
首页 > 数据库> > Cannot autogenerate id of type java.lang.Integer for entity of type com.mongodb.pojo.User!

Cannot autogenerate id of type java.lang.Integer for entity of type com.mongodb.pojo.User!

作者:互联网

第一次尝试使用MongoDB对实体进行插入操作,结果出现 Cannot autogenerate id of type java.lang.Integer for entity of type com.mongodb.pojo.User!
大概意思是不能将ID转换为Integer
在这里插入图片描述

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

所以,将主键ID的类型改为ObjectId(org.bson.types.ObjectId)即可。

标签:lang,java,ObjectId,主键,Integer,entity,type,id
来源: https://blog.csdn.net/weixin_44504392/article/details/120173064