[JAVA-2674] POJO ID property is not populated after insert Created: 24/Nov/17 Updated: 01/Jul/19 Resolved: 29/Jan/19 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | 3.5.0 |
| Fix Version/s: | 3.10.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Eric Tray | Assignee: | Ross Lawley |
| Resolution: | Done | Votes: | 7 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Description |
|
When inserting a new POJO into a collection, the _id is properly correctly generated in the document representation but the POJO ID property is not populated:
|
| Comments |
| Comment by Githook User [ 04/Feb/19 ] |
|
Author: {'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}Message: Added IdGenerator to the POJOs docs.
|
| Comment by Githook User [ 04/Feb/19 ] |
|
Author: {'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}Message: Added IdGenerator to the POJOs docs.
|
| Comment by Eric Tray [ 30/Jan/19 ] |
|
Nice work, much appreciated! |
| Comment by Githook User [ 29/Jan/19 ] |
|
Author: {'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}Message: Added Pojo id generation Added IdGenerator to ClassModel to support to Pojos generating
|
| Comment by Ross Lawley [ 22/Jan/19 ] |
| Comment by Ross Lawley [ 22/Aug/18 ] |
|
Scheduling to prompt future support. |
| Comment by Ross Lawley [ 24/May/18 ] |
|
Hi all, To clarify, my original response to this still stands; This is not a bug but a known limitation; the POJOCodec does not support automatic creation of the _id and does not implement the CollectableCodec. As it stands currently, when calling insertOne the _id is generated by the database and there is no way currently, to pass that _id back into the PojoCodec. This ticket is deliberately being kept open, so in the future we can review and hopefully, extend the ClassModel to provide some support for mutable POJO's and auto-generating _id. Also mentioned there is the wider issue of insertOne returning null, which when using immutable objects is not very helpful, so improvements there are also kept in review. In the meantime, the work around is to generate your own id values, if they don't exist and you want to insert the POJO. Ross
|
| Comment by Eric Tray [ 01/May/18 ] |
|
Hi Ross, I was wondering if you planned on revisiting this issue at all? At the time of your initial response you indicated that the PojoCodec did not support this feature. Can you give a more definitive answer as to whether or not you think this issue could or should be resolved at some point?
Thanks, Eric |
| Comment by Ralph [ 16/Mar/18 ] |
|
This is really annoying! Wasted quite some time playing with WriteConcern to end up finding this bug. |
| Comment by aneta stępień [ 03/Jan/18 ] |
|
Hi, |
| Comment by Zoe Snape [ 19/Dec/17 ] |
|
Just chiming in to agree with Eric that it would be useful to know the _id value that was automatically generated on insert. If there's issues with setting it on the POJO, perhaps some sort of insertOneAndGetId() method that returns the generated ID could at least make that information accessible without the mutability concerns? |
| Comment by Eric Tray [ 24/Nov/17 ] |
|
Hi Ross, Thanks for the quick response. I understand what you are saying about the mutability aspect. Since the ClassModel is already aware of the "_id" PropertyModel though, shouldn't the PojoCodec have enough info available to know if the "_id" property has a setter available? Since the CollectibleCodec codec already mutates Documents without an "_id" property by automatically generating and adding the "_id" property, I would expect the PojoCodec to follow the same convention. Manually generating and setting the ID on the new POJO instance in order to have access to the generated ID after insert definitely works, but IMO having to manually create and set IDs for POJOs and having automatic ID creation for Documents seems conflicting. Thanks, |
| Comment by Ross Lawley [ 24/Nov/17 ] |
|
Hi etay2000, Thanks for the ticket. The PojoCodec does not support this, when reading from a Pojo it doesn't know if the Pojo is mutable, the PojoCodec only uses the getter properties to get the values to be stored in MongoDB. The reason when finding the widget it has an ObjectId is because one is automatically created by the database itself on insert. Any default values should be set either when constructing a new instance, when getting the fields value or potentially via a custom Convention. In this instance, you should set the id field before inserting it into the database, if you want to know the id as saved to MongoDB. I hope that helps, Ross |