Are these @Table(uniqueConstraints) and @Table(indexes) annotations the same?

920 Views Asked by At

When creating indexes in JPA / Hibernate, are these exactly the same?

@Table (uniqueConstraints = @UniqueConstraint (columnNames = {"series", "date"}))

And:

@Table (indexes = {@Index (name = "idx", columnList = "series, date", unique = true)})

And if not, what's the difference?

1

There are 1 best solutions below

1
On

Both @UniqueConstraint and @Index are just hints for the schema generation tool, which is dialect/DB sensitive. Your specific example will likely produce the same index ddl for most DB providers. General difference between the two is that you can index a column without enforcing uniqueness. And of course, both are irrelevant if you do not generate your schema with JPA/hibernate schema gen. If you need more control over your indexes or schema in general, you might want to consider tools like liquibase or flywheel.