title: How to create UNIQUE column with JPA?
body: <p>My POJO is using JPA and when I apply unique value in the column is not working for me I tried to use the <code>UniqueConstraint</code> and also not working for me .</p>
  
  <p>below is my code </p>
  
  <pre><code>@Entity
  @Table(name = "users", uniqueConstraints={@UniqueConstraint(columnNames = {"user_id"}),@UniqueConstraint(columnNames = {"username"}),@UniqueConstraint(columnNames = {"email"})})
  
      public class Users {
  
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Column(name="user_id",unique=true,nullable = false)
       private int UserId;
  
       @Column(name = "username" ,unique=true,nullable = false)
       private String Username;
  
       @Column(name = "email",unique=true ,nullable = false)
       private String email;
  
       @Column(name = "firstname",nullable = false)
       private String firstname;
  
       @Column(name = "lastname", nullable = false)
       private String lastname;
  
       @Column(name = "password", nullable = false)
       private String password;
  
       @Column(name = "active")
       private int active;
  
       @ManyToMany(cascade=CascadeType.ALL)
       @JoinTable(name="user_role", joinColumns=@JoinColumn(name="user_id"), inverseJoinColumns=@JoinColumn(name="role_id"))
       private Set&lt;Role&gt; roles;
  </code></pre>
  
  <p>below is the generated table in the database (MySQL)</p>
  
  <pre><code>| users | CREATE TABLE users (
    user_id int(11) NOT NULL,
    username varchar(255) NOT NULL,
    active int(11) DEFAULT NULL,
    email varchar(255) NOT NULL,
    firstname varchar(255) NOT NULL,
    lastname varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    PRIMARY KEY (user_id)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
  </code></pre>
  
  <p>Hibernate log after spring start</p>
  
  <p><code>Hibernate: create table users (user_id integer not null, username varchar(255) not null, active integer, email varchar(255) not null, firstname varchar(255) not null, lastname varchar(255) not null, password varchar(255) not null, primary key (user_id)) engine=InnoDB
  Hibernate: alter table users drop index UKfnranlqhubvw04boopn028e6
  Hibernate: alter table users add constraint UKfnranlqhubvw04boopn028e6 unique (username, email)
  Hibernate: alter table users drop index UK_r43af9ap4edm43mmtq01oddj6
  Hibernate: alter table users add constraint UK_r43af9ap4edm43mmtq01oddj6 unique (username)
  Hibernate: alter table users drop index UK_6dotkott2kjsp8vw4d0m25fb7</code></p>