Original Description:
Motivation: clean POJO class do not use ObjectId in field. Pojo class do not know about mongodb logic.
Add opportunity to serialize/deserilize document field `ObjectId _id` to `String _id` in POJO field.
public static class TestModel { public String _id; // ObjectId.toHexString() public int id; public String name; }
Now there is an exception: Failed to decode 'TestModel'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
Reproducer: http://klikr.org/869dbe58dd5df1a38b9a968c8271.txt
Exception http://klikr.org/4386afc3820d7f89a218f98c2d79.txt
Description after Implementation:
To implement this, we ended up adding a BsonRepresentation annotation. This makes it easy to specify that we want to store the field as one type in the database, but convert it to a different type when used in Java POJOs. To achieve what the above example was describing, one can now do:
public class TestModel { @BsonRepresentation(BsonType.ObjectId) public String _id; public String name; ... }