I have a entity which has a unique string value that I would like to use instead of the auto-generated objectId in spring mongodb documents. However, when I save the object it still generates an ObjectId. Here is the document.
@Document(collection="products")
class Product {
@Id
@Indexed(unique = true,direction = IndexDirection.ASCENDING)
String productCode;
String descr;
Integer quantity;
public Product(String productCode,String descr,Integer quantity)
{
this.productCode = productCode;
this.descr = descr;
this.quantity = quantity;
}
Here is the repository definition:
@Repository
public interface ProductRepository extends ReactiveMongoRepository<Product,String> {
}
Here is what is saved to the repository, the productCode is not saved and a autogenerated ObjectId is provided instead:
{
"_id" : ObjectId("63f511be4eaf1f75b27ff928"),
"descr" : "Best in breed watches",
"quantity" : 122222
}
Why is the provided productCode that I annotate with id and set in tbe constructor ignored? Please help me to solve this issue.
I found the problem. I was missing the @PersistenceCreator annotation on the constructor