How to prevent Spring Data MongoDB from displaying the id field as an object id?

I set up my own mechanism for assigning IDs to my domain objects, and so while storing them, it doesn't really matter 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 straightforward. The problem is, according to the documentation, Spring will automatically map this field to the MongoDB assigned ObjectID. How can I prevent this from happening without renaming my id field or defining a custom id field tagged @Id

just to get around this?

+5


source to share


1 answer


Well, you cannot do it with Spring Data, which I am afraid of. Mongodb (and in turn Spring data) needs a field to uniquely identify each one document

. If you already have a field id

, and if it is unique for each object, then yes, you can annotate it with @Id

and mongo will take care of the rest.



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

.

+1


source







All Articles