@UniqueConstraint annotation in Java

后端 未结 8 1575
囚心锁ツ
囚心锁ツ 2020-12-02 05:20

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\         


        
8条回答
  •  日久生厌
    2020-12-02 06:04

    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):

    • @UniqueConstraint
    • @Column

提交回复
热议问题