1

I have a POJO class in java

@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class TestModel{
    protected ObjectId id;
    private String name;
}

Is there any way to populate id field in case of insertion. I can see that if I use Document, then after insertion I can get objectId by using

document.get("_id");

So is there any way I can achieve same using custom POJO class.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Lakshay Jain
  • 446
  • 1
  • 3
  • 17
  • 1
    You don't. You set the `id` within your class **before** you persist it for saving. See [JAVA-2674](https://jira.mongodb.org/browse/JAVA-2674). That's basically intentional design within the driver, and not a bug. – Neil Lunn Nov 25 '18 at 21:36

0 Answers0