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

前端 未结 8 616
夕颜
夕颜 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").
    0 讨论(0)
  • 2020-12-09 16:45

    I was also facing the similar issue but got it resolved.

    • First, drop the table from Database

        drop table Customer;
      
    • Stop your spring boot application and launch it again.

      This worked for me.

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

    MySQL Hibernate Object Relational Mapping - Alter Contains after schema was generated.

    1. In case you change your entity class [Customer] after the Hibernate generated the schemas, you can drop the schema and re-generate it. The constrains will apply.

    2. Instead of dropping schemas you can manually alter the table

    ALTER TABLE Customer ADD CONSTRAINT customer_name_unq UNIQUE (name);

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

    For InnoDB tables , there are limit for indexed columns. That means, you have to set max length for te field:

    @Column(unique = true, length = 32)
    private String name;
    
    0 讨论(0)
  • 2020-12-09 16:56

    The unique=true element of the Column annotation and / or the UniqueConstraint annotation that can be used at the table level are used to specify that a unique constraint is to be included in the generated DDL.

    In other words, they don't do anything during the runtime, the verification is left to the database (which makes sense as unicity can't be tested at the Java level reliably1) and if for whatever reason you don't have the corresponding constraint(s) defined at the database level, nothing will happen.

    Add the constraint manually:

    ALTER TABLE Customer ADD CONSTRAINT customer_name_unq UNIQUE (name);
    

    See also

    • JPA 1.0 specification
      • 9.1.4 UniqueConstraint Annotation
      • 9.1.5 Column Annotation
    • MySQL Documentation
      • 12.1.7. ALTER TABLE Syntax

    1 Unless you acquire a table lock (ouch!), you can't check for unicity with a SQL query in a concurrent environment.

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

    I did not create table using JPA

    Then you should add the unique constraint to your table in your CREATE statement, for example, if you are using MySQL:

    create Customer (id int primary key, name varchar(255) unique);
    
    0 讨论(0)
提交回复
热议问题