@UniqueConstraint annotation in Java

后端 未结 8 1577
囚心锁ツ
囚心锁ツ 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

    Unique annotation should be placed right above the attribute declaration. UniqueContraints go into the @Table annotation above the data class declaration. See below:

    @Entity
    @Table(uniqueConstraints= arrayOf(UniqueConstraint(columnNames = arrayOf("col_1", "col_2"))))
    data class Action(
            @Id @GeneratedValue @Column(unique = true)
            val id: Long?,
            val col_1: Long?,
            val col_2: Long?,
    )
    

提交回复
热议问题