Multiple unique constraints in JPA

The @Table‘s attribute uniqueConstraints actually accepts an array of these. Your example is just a shorthand for an array with a single element. Otherewise it would look like:

@Table(name="person",  uniqueConstraints={
   @UniqueConstraint(columnNames={"code", "uid"}),
   @UniqueConstraint(columnNames={"anotherField", "uid"})
})

Whenever the unique constraint is based only on one field, you can use @Column(unique=true) on that column.

Leave a Comment