title: To catch the reason of the unique constraint on JPA
body: <p>I am trying to learn JPA and I have a problem that I stucked in since 2 days. </p>
  
  <p>I have a table named "User" includes id,email,password,username and status.</p>
  
  <p>As you guess email and username columns are unique.</p>
  
  <p>I also have a Class called User something like that :</p>
  
  <pre><code>@Entity
  @Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = {"username", "email"}))
  public class User {    
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private int id;
      @Column(name="email", nullable=false)
      private String email;
      @Column(name="password", nullable=false)
      private String password;
      @Column(name="username", nullable=false)
      private String username;
      @Column(name="status", nullable=false)
      private String status; 
  </code></pre>
  
  <p>Rest of this class is getters and setters.</p>
  
  <p>I am trying to insert a value by using JPA with Hibernate.</p>
  
  <pre><code>    try {
              em = jpaResourceBean.getEMF().createEntityManager();
              em.getTransaction().begin();
              user.setStatus(UserStatus.PENDING.toString());
              em.persist(user);
              em.getTransaction().commit();
              logger.info("User " + user.getUsername() + " has been registered");
      // Attention starts
          } catch (XXXXXX) { 
      if (XXXXX.getYYY().equals("ZZZZZ")) logger.info("User name is already exist");
      if (XXXXX.getMMM().equals("LLLLL")) logger.info("Email is already exist");
          } 
      // Attention end
  </code></pre>
  
  <p>All I want to know : How can I understand is the problem with the username constraint or the email unique constraint? While you can check my Attention start and end block, I am sure that you get my point : ) </p>
  
  <p>Thanks in advance.</p>