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

前端 未结 8 622
夕颜
夕颜 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:42

    For future users stumbling on this issue. There are lots of great suggestions here; read through them as well as your error messages; they will be enough to resolve your problem.

    A few things I picked up in my quest to get @Column(unique=true) working. In my case I had several issues (I was using Spring Boot, FYI). To name a couple:

    • My application.properties was using spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect despite using MySQL 8. I fixed this with spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect. Check your version (I did this through the command line: mysql> STATUS).
    • I had a User class annotated as an @entity which meant that JPA was trying to create a user table which is one of MySQL's (as well as postgres) reserved keywords. I fixed this with @Table(name = "users").

提交回复
热议问题