How to prevent Spring Data MongoDB from mapping id field as object id?

▼魔方 西西 提交于 2020-03-01 03:34:47

问题


I have set up my own mechanism for assigning identities to my domain objects, and so when persisting them, there really isn't much value for me to keep track of what MongoDB assigns to them. However, I name the identity fields for my domain classes id because, well, it's concise and understandable. The problem is that, according to the documentation, Spring will automatically map this field to MongoDB's assigned ObjectID. How do I prevent this from happening without having to rename my id field, or defining a custom identity field annotated with @Id just for the sake of working around this?


回答1:


Well, you can't do that with Spring data I am afraid. Mongodb (and in turn, Spring data) needs a field to uniquely identify each document. If you have an id field already, and if it's unique for each and every object then yes, you can annotate it with @Id and mongo will take care of the rest.

If not, you will have to create a new field and map it to _id.



来源:https://stackoverflow.com/questions/44704977/how-to-prevent-spring-data-mongodb-from-mapping-id-field-as-object-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!