【发布时间】:2015-06-10 14:15:24
【问题描述】:
我的应用程序使用 Spring Boot / JPA / MongoDB。
我使用
将我的域类映射到 MongoDBorg.springframework.data.mongodb.core.mapping.Document;
org.springframework.data.mongodb.core.index.Indexed;
org.springframework.data.mongodb.core.mapping.DBRef;
一切都很好,除非尝试使 DBRef 独一无二:
@DBRef @Indexed(unique = true)
private User owner;
我尝试了@DBRef、@Indexed (unique=true) 的不同组合,但无法使 DBRef 独一无二。我可以使其他字段类型唯一,例如以下示例中的“名称”
@Indexed(unique = true)
@Size(min = 2, max = 100)
@Column(length = 100)
private String name;
但找不到如何使我的 DBRef 字段唯一。
【问题讨论】:
-
出于兴趣:索引在数据库级别上的外观如何(
colleciton.getIndexes()) -
@xeraa - 请参阅下面的代码。我用 smeCode 替换了名称不佳的“名称”字段。 DBRef 所有者是有问题的变量。我使用了 db.NT_SMES.getIndexes(),所有者没有被索引。 smeCode 和 _id 被列为索引。 (抱歉没有发布代码 - 我无法正确格式化评论)
标签: spring mongodb spring-data spring-data-mongodb