I have a Java bean. Now, I want to be sure that the field should be unique.
I am using the following code:
@UniqueConstraint(columnNames={"username"})
public String username;
But I'm getting some error:
@UniqueConstraint is dissallowed for this location
What's the proper way to use unique constraints?
Note: I am using play framework.
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):