[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:
Duplicate
is duplicated by JAVA-2946 I have Question regarding saving java... Closed
is duplicated by JAVA-3345 Get ID of last inserted document in a... Closed

 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:

MongoCollection<Document> collection;
Document document = new Document();
collection.insertOne(document);
// document.get("_id") = 5a1786851ce328045c0c5276    <----- _id populated as expected after insert
 
public class Widget {
  private ObjectId id;
 
  public String getId() {
    return id;
  }
 
  public void setId(ObjectId id) {
    this.id = id;
  }
 
}
 
MongoCollection<Widget> collection;  // (PojoCodecProvider configured for automatic)
Widget widget = new Widget();
collection.insertOne(widget);
// widget.getId() == null    <-----   id value still null;
 
// The ID property is properly populated when using find
Widget widget = collection.find(...);
widget.getId() == 5a1786851ce328045c0c5276   <--- id value set as expected



 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.

JAVA-2674
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/56835250ac6596248c309cac472848967b841ee9

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.

JAVA-2674
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/dea26b9f94e43c37d4b1e0ef21c10d03673a0f1b

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
their own id's. Supports both mutable and immutable Pojos. Mutable
Pojos will update their Id property's value.

JAVA-2674
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/762108da5c8062dd873c723331f6f50bedacb2ef

Comment by Ross Lawley [ 22/Jan/19 ]

PR: https://github.com/rozza/mongo-java-driver/pull/312

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,
I agree with the comments above - I found it quite surprising that the generated id is not accessible (it is neither populated in the pojo, as it used to be with the Document - and there is no return value of method insertOne that would give us the id).

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,
Eric

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

Generated at Thu Feb 08 08:57:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.