我使用Spring-Boot 1.5.1
和MongoDB 3.4.6
如何获得这@Indexed(唯一= TRUE)失败
我有一个MongoDB
文件,其中对某些领域的一些@Indexed(unique=true)
注解。
@Document(collection="Product")
public class Product{
@Id
private String id;
@Indexed(unique=true)
private String name;
@Indexed(unique=true)
private String searchName;
当有任何重复的名称或searchName它会抛出org.springframework.dao.DuplicateKeyException
。
堆栈跟踪:
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)
我们怎样才能在其上被抛出的异常的关键。
喜欢的东西,当我们把一些申请@NotNull(message = "Product briefDescription cannot be null")
和它给你的exception
的message
,但没有message
属性为@Indexed
注释。
任何方式来做到这一点?
这将是很好,如果你可以多放一些异常堆栈跟踪的。通常,它写入stackTrace。 –
@KaustubhKallianpur已添加。 –
所以你想知道在哪个键上抛出了重复的异常并将其用于进一步?根据堆栈跟踪,E11000已经作为名称存在于数据库中,该数据库已抛出异常。它不是searchName字段。 –