Search code examples
mysqljpajakarta-eeorm

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


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

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique=true )
    private String name;

    ...
}

I set the name using regular EL in JSF. I did not create table using JPA


Solution

  • 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);