@UniqueConstraint annotation in Java

287,186

Solution 1

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

Solution 2

You can use at class level with following syntax

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

Solution 3

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.

Solution 4

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)

Solution 5

Way1 :

@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.

Way2 :

@Entity
@Table(name = "table_name", 
       uniqueConstraints={@UniqueConstraint(columnNames ={"column1","column2"})})

-> Here both column1 and column2 combined values acts as unique constraints

Share:
287,186

Related videos on Youtube

xyz
Author by

xyz

Updated on September 24, 2021

Comments

  • xyz
    xyz over 2 years

    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.

    • Jon Skeet
      Jon Skeet almost 14 years
      "But am geting some error." Always specify what error you're getting in the question. You have relevant information which may very well help us to solve your problem - don't keep it to yourself.
  • naoru
    naoru almost 6 years
    Its important to note that it will only work if you let JPA create your tables
  • Stimpson Cat
    Stimpson Cat about 3 years
    I hope you do not code with these fields in real life ;)
  • FrancescoM
    FrancescoM over 2 years
    Of course not, but i do write examples with such fields :D

Related