@UniqueConstraint annotation in Java @UniqueConstraint annotation in Java java java

@UniqueConstraint annotation in Java


To ensure a field value is unique you can write

@Column(unique=true)String username;

The @UniqueConstraint annotation is for annotating multiple unique keys at the table level, which is why you get an error when applying it to a field.

References (JPA TopLink):


You can use at class level with following syntax

@Entity@Table(uniqueConstraints={@UniqueConstraint(columnNames={"username"})})public class SomeEntity {    @Column(name = "username")    public String username;}


I'm currently using play framework too with hibernate and JPA 2.0 annotation and this model works without problems

@Entity@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"id_1" , "id_2"})})public class class_name {@Id@GeneratedValuepublic Long id;@NotNullpublic Long id_1;@NotNullpublic Long id_2;}

Hope it helped.