且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

春季数据mongodb _id映射首选项

更新时间:2023-12-03 21:43:52

答案在文档 http://docs.spring.io/spring-data/data-mongo/docs/1.7.0.M1/reference/html/ >

The answer is in documentation http://docs.spring.io/spring-data/data-mongo/docs/1.7.0.M1/reference/html/

MongoDB要求所有文档都有一个"_id"字段.如果 您不提供驱动程序,驱动程序将为ObjectId分配一个 产生的价值.使用MongoMappingConverter时 某些规则,这些规则控制Java类的属性如何映射 到此"_id"字段.

MongoDB requires that you have an '_id' field for all documents. If you don’t provide one the driver will assign a ObjectId with a generated value. When using the MongoMappingConverter there are certain rules that govern how properties from the Java class is mapped to this '_id' field.

以下概述了将什么属性映射到'_id' 文档字段:

The following outlines what property will be mapped to the '_id' document field:

用@Id注释的属性或字段 (org.springframework.data.annotation.Id)将映射到'_id' 字段.

A property or field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.

没有注释但命名为id的属性或字段将被映射 到"_id"字段.

A property or field without an annotation but named id will be mapped to the '_id' field.

如您所见,@ Id(objId)将优先.

As you can see @Id (objId) will take precedence.