title: Hibernate Avoid Duplicate Rows
body: <p><strong>ER Dagram:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/lSNAk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lSNAk.png" alt="ER Diagram"></a></p>
  
  <p>My Beans and Service class are given below:</p>
  
  <pre><code>@Entity
  @Table(name = "EMPLOYEE")
  public class EmployeeBean implements Serializable { 
      @Id
      @Column(name="EMP_ID")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private int employeeid;
  
      @Column(name="EMP_NAME")
      private String employeeName;
  
      @ManyToMany(cascade=CascadeType.ALL)  
      @JoinTable(name="EMPLOYEE_SKILLS",  
      joinColumns={@JoinColumn(name="EMPLOYEE_ID", referencedColumnName="EMP_ID")},  
      inverseJoinColumns={@JoinColumn(name="SKILLS_ID")})  
      private Set&lt;SkillsBean&gt; skills;
  
      //Getters and Setters
  }
  
  
  
  @Entity
  @Table(name="SKILLS")
  public class SkillsBean implements Serializable {   
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="SKILL_ID")
      private String skillId;
  
      @Column(name="SKILL_NAME", unique = true)
      private String skillName;
  
      //Getters and Setters
  }
  
  //Service Class with insert method
  @Service
  public class EmployeeSkillsServiceImpl {
        public boolean insert(String employeeName,String skills) {
              String[] skillNames = skills.split(SKILLS_DELIMITER);
              Set&lt;SkillsBean&gt; skillSet = new HashSet&lt;SkillsBean&gt;();
              for(int i=0;i&lt;skillNames.length;i++){
                  skillBeanObj = new SkillsBean();
                  skillBeanObj.setSkillName(skillNames[i]);
                  skillSet.add(skillBeanObj);
              }
  
              //Call DAO class now
              EmployeeBean employeeBean = new EmployeeBean();
              employeeBean.setEmployeeName(employeeName);
              employeeBean.setSkills(skillSet);
              employeeSkillsDAO.insert(employeeBean); //simple insert with hibernate session and flush
  }
  }
  </code></pre>
  
  <p>Requirement:
  Recruiter can add Employee Name and Skill Names (comma separated) through UI (JSP page).</p>
  
  <p>Current problem in steps:
  Step(1) Recruiter enters 'Joe' and 'Java,Oracle', then:</p>
  
  <blockquote>
    <p>Employee table:</p>
  </blockquote>
  
  <p>1 Joe</p>
  
  <blockquote>
    <p>Skills table:</p>
  </blockquote>
  
  <p>1 Java 
  2 Oracle</p>
  
  <blockquote>
    <p>Employee_skills table:</p>
  </blockquote>
  
  <p>1 1
  1 2</p>
  
  <p>Upto here I am successful with out any issues, now the actual problem starts in step(2).</p>
  
  <p>Step(2) Recruiter enters 'James' and 'Java' then, then I am expecting below records in tables, but I am facing an exception.</p>
  
  <blockquote>
    <p>Employee table:</p>
  </blockquote>
  
  <p>1 Joe</p>
  
  <p>2 James</p>
  
  <blockquote>
    <p>Skills table:</p>
  </blockquote>
  
  <p>1 Java </p>
  
  <p>2 Oracle</p>
  
  <blockquote>
    <p>Employee_skills table:</p>
  </blockquote>
  
  <p>1 1</p>
  
  <p>1 2</p>
  
  <p>2 1</p>
  
  <p><strong>Exception thrown in logs:</strong></p>
  
  <blockquote>
    <p>org.hibernate.exception.ConstraintViolationException: could not
    insert: [com.esm.beans.SkillsBean] Caused by:
    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    Duplicate entry 'Java' for key 'SKILL_NAME'</p>
  </blockquote>
  
  <p><strong>This error clearly states that it is because of Unique constraint on SKILL_NAME, but I can't remove it as I don't want to 
  insert the same skill name multiple times into skills table.</strong></p>
  
  <p>*So how can I achive my desired output in the tables by modifying the EmployeeBean and SkillsBean beans ? What am I missing here ? *</p>
  
  <p>Please help.</p>