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
@GeneratedValue
public Long id;
@NotNull
public Long id_1;
@NotNull
public Long id_2;
}
Hope it helped.
Note: In Kotlin the syntax for declaring the arrays in annotations uses arrayOf(...)
instead of {...}
@Entity
@Table(uniqueConstraints=arrayOf(UniqueConstraint(columnNames=arrayOf("book", "chapter_number"))))
class Chapter(@ManyToOne var book:Book,
@Column var chapterNumber:Int)
Note: As of Kotlin 1.2 its is possible to use the [...]
syntax so the code become much simpler
@Entity
@Table(uniqueConstraints=[UniqueConstraint(columnNames=["book", "chapter_number"])])
class Chapter(@ManyToOne var book:Book,
@Column var chapterNumber:Int)
@Entity
@Table(name = "table_name",
uniqueConstraints={
@UniqueConstraint(columnNames = "column1"),
@UniqueConstraint(columnNames = "column2")
}
)
-> Here both Column1 and Column2 acts as unique constraints separately. Ex : if any time either the value of column1 or column2 value matches then you will get UNIQUE_CONSTRAINT Error.
@Entity
@Table(name = "table_name",
uniqueConstraints={@UniqueConstraint(columnNames ={"column1","column2"})})
-> Here both column1 and column2 combined values acts as unique constraints
@Entity @Table(name = "stock", catalog = "mkyongdb",
uniqueConstraints = @UniqueConstraint(columnNames =
"STOCK_NAME"),@UniqueConstraint(columnNames = "STOCK_CODE") }) public
class Stock implements java.io.Serializable {
}
Unique constraints used only for creating composite key ,which will be unique.It will represent the table as primary key combined as unique.
@UniqueConstraint this annotation is used for annotating single or multiple unique keys at the table level separated by comma, which is why you get an error. it will only work if you let JPA create your tables
Example
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder(builderClassName = "Builder", toBuilder = true)
@Entity
@Table(name = "users", uniqueConstraints = @UniqueConstraint(columnNames = {"person_id", "company_id"}))
public class AppUser extends BaseEntity {
@Column(name = "person_id")
private Long personId;
@ManyToOne
@JoinColumn(name = "company_id")
private Company company;
}
https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/UniqueConstraint.html
On the other hand To ensure a field value is unique you can write
@Column(unique=true)
String username;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With