[JAVA-3677] Support BsonRepresentation annotation on POJOs Created: 02/Apr/20 Updated: 28/Oct/23 Resolved: 17/Sep/20 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | None |
| Fix Version/s: | 4.2.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Dmitrii Volykhin | Assignee: | Brian DeLeonardis (Inactive) |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||
| Documentation Changes: | Needed | ||||
| Documentation Changes Summary: | See the changes in the commit to the .md file. |
||||
| Description |
|
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.
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 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:
|
| Comments |
| Comment by Githook User [ 17/Sep/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author: {'name': 'Brian DeLeonardis', 'email': 'bdeleonardis1@gmail.com', 'username': 'bdeleonardis1'}Message: Add BsonRepresentation to allow ObjectId to String conversion
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 31/Aug/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
We agreed that we should take a similar approach as the .NET driver and implement a BsonRepresentation annotation, with associated support in relevant Codec implementations, in particular StringCodec. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Andreas Keefer [ 29/Jun/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I would also love to see this feature! I implemented a custom Type to walk around this via a Codec. My Usecase is a bit different. I have two applications, one writeing _id as ObjectId and the other as String (UUID):
In your usecase the codec would look like this:
|