春季数据mongodb _id映射首选项 [英] spring data mongodb _id mapping preference
问题描述
我在Java应用程序中使用spring data mongodb框架将应用程序数据持久存储在mongodb中.在我的Java模型类中,我有两个字段.
I am using spring data mongodb framework in my java application to persist my application data in mongodb. In my java model class I have two fields.
1)带有@Id注释的objId字段. 2)字段ID
1) A field objId with @Id annotation on it. 2) A field id
,哪个将获得首选项,或者我将为多次映射而出错.我正在使用spring data mongo 1.6.1.我知道我可以对此进行测试,但是我没有本地环境设置.
with respect to mapping to _id key in the mongodb document saved, which one will get preference or will I get an error for multiple mapping. I am using spring data mongo 1.6.1. I know I could have tested this out but I do not have local environment setup.
推荐答案
答案在文档 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.
这篇关于春季数据mongodb _id映射首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!