I am using Spring-Boot 1.5.1 and MongoDB 3.4.6
I've a MongoDB document which has some @Indexed(unique=true) annotation on some field.
@Document(collection="Product")
public class Product{
@Id
private String id;
@Indexed(unique=true)
private String name;
@Indexed(unique=true)
private String searchName;
When there is any duplicate name or searchName it throws org.springframework.dao.DuplicateKeyException.
Stacktrace :
Caused by: org.springframework.dao.DuplicateKeyException: E11000 duplicate key error index: Product.name dup key: { : "name" }; nested exception is com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: Product.name dup key: { : "name" }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:52)
How can we get the key on which the the exception was thrown.
Something like when we put @NotNull(message = "Product briefDescription cannot be null") on some filed and it gives you the message in the exception, but there is no message attribute for @Indexed annotation.
Any way to do it?