mongodb - 如何通过Hibernate OGM和JPA在MongoDB中创建 "unique"约束

标签 mongodb jpa hibernate-ogm

我试图在非 id 字段上定义唯一约束。答案似乎很明显:

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"col1"}))
public class MyEntity { ... }

但是,这不起作用。我已通过 mongo 命令行检查了集合中的索引,但没有唯一索引的踪迹(仅生成 _id_ 索引)。

我也尝试过使用 @Index 注释,但没有高兴:

@Entity
@Table(indexes={ @Index(name = "myIndex", columnList="col1", unique = true) })
public class MyEntity { ... }

@Column(unique = true) 注释也没有任何效果。

如何让 Hibernate OGM 为此集合创建唯一索引?

提前谢谢您,

吉列尔莫

最佳答案

Hibernate OGM 尚未考虑 MongoDB 的索引/约束元数据。我已经打开OGM-910用于跟踪它。

关于mongodb - 如何通过Hibernate OGM和JPA在MongoDB中创建 "unique"约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876279/

相关文章:

node.js - Nodejs Express 多次回调错误

node.js - 我在 postman 中使用什么 URL?

java - org.hibernate.exception.SQLGrammarException : could not prepare statement

java - ValidationException 异常说明 : uses a non-entity as target entity in the relationship attribute

cassandra - Hibernate OGM 和 Kundera 有什么区别

java - hibernate -OGM [PersistenceUnit : person] Unable to build Hibernate SessionFactory

java - 意外的UpgradingStoreVersionException - "Expected ' v0.A。 3' but file is version "与 Neo4j 服务器和 Hibernate OGM

javascript - 使用从 MongoDB JSON 对象获取 "Pretty"数据

mongodb - Docker 中的 Mongo ReplicaSet - 无法添加用户 : not master

hibernate - 无法在Kotlin和JpaRepository中为嵌套列表对象找到适当的构造函数错误