@Column(unique=true) does not seem to work

前端 未结 8 617
夕颜
夕颜 2020-12-09 16:37

Even though I set the attribute to be @Column(unique=true), I still insert a duplicate entry.

@Entity
public class Customer {

    @Id
    @Ge         


        
相关标签:
8条回答
  • 2020-12-09 16:59

    Try specifying the unique constraint at the class level using

    @Table(uniqueConstraints={@UniqueConstraint(columnNames={"name"})})
    public class Customer {
    ...
        private String name;
    }
    

    @Column(unique=true) doesn't work for me but when I used @UniqueConstraint at the class level, the column in the database (MySql) was appropriately set as unique.

    I hope this helps someone else who may face this same issue in the future

    0 讨论(0)
  • 2020-12-09 16:59

    Make sure to delete the tables created by hibernate in your database. And then re-run your hibernate application again.

    0 讨论(0)
提交回复
热议问题