How to create an "unique" contraint in MongoDB through Hibernate OGM and JPA

1.2k views Asked by At

I'm trying to define a unique constraint on a non-id field. The answer might seem obvious:

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

However, this is not working. I've checked the indexes in the collection through the mongo command line, but there is no trace of a unique index (only a _id_ index is being generated).

I have also tried with the @Index annotation without joy:

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

The @Column(unique = true) annotation doesn't have any effect either.

How can I get Hibernate OGM to create a unique index for this collection?

Thank you in advance,

Guillermo

1

There are 1 answers

1
Gunnar On BEST ANSWER

Hibernate OGM does not yet consider this index/constraint meta-data for MongoDB. I've opened OGM-910 for tracking it.