title: Hibernate Duplicate Rows in Many-to-Many XML Mapping
body: <p>I tried all the possible solution in stackoverflow. This is my XML:</p>
  
  <p>Role.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name="Role" table="T_ROLE"   dynamic-insert="true" dynamic-update="true"&gt;
      &lt;id name="id" type="java.lang.String"  column="ROLE_ID" length="32"&gt;
          &lt;generator class="uuid.hex" /&gt;
      &lt;/id&gt;
      &lt;property       name="roleName"         column="ROLENAME"           type="string"   length="50"     not-null="true" /&gt;
      &lt;property       name="description"      column="DESCN"              type="string"   length="255"    not-null="false" /&gt;
      &lt;property       name="createDatetime"   column="CREATE_DATETIME"    type="timestamp"/&gt;
      &lt;property       name="updateDatetime"   column="UPDATE_DATETIME"    type="timestamp"  /&gt;
  
      &lt;set name="permissionSet" table="T_ROLE_PERM" inverse="false" lazy="true" fetch="select" cascade="all"  &gt;
          &lt;key&gt;
              &lt;column name="ROLE_ID" /&gt;
          &lt;/key&gt;
          &lt;many-to-many column="PERMISSION_ID" class="Permission" /&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>Permission.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name="Permission" table="T_PERMISSION"&gt;
      &lt;id name="id" type="java.lang.String" column="PERMISSION_ID" length="32"&gt;
          &lt;generator class="uuid.hex" /&gt;
      &lt;/id&gt;
      &lt;property       name="name"             column="PERMNAME"           type="string"       not-null="true"     length="50" /&gt;
      &lt;property       name="type"             column="TYPE"               type="string"       not-null="true"     length="20" /&gt;
      &lt;property       name="description"      column="DESCN"              type="string"       not-null="false"    length="255"/&gt;
      &lt;property       name="createDatetime"   column="CREATE_DATETIME"    type="timestamp"/&gt;
      &lt;property       name="updateDatetime"   column="UPDATE_DATETIME"    type="timestamp"  /&gt;
  
      &lt;set name="roleSet" table="T_ROLE_PERM" inverse="true" lazy="true" fetch="select" &gt;
          &lt;key&gt;
              &lt;column name="PERMISSION_ID" /&gt;
          &lt;/key&gt;
          &lt;many-to-many class="Role" column="ROLE_ID"/&gt;          
      &lt;/set&gt;
  
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>Initializer.Java:</p>
  
  <pre><code>public class Initializer {
  
  private PermissionDao permissionDao;
  
  @Autowired
  private RoleDao roleDao;
  
  public PermissionDao getPermissionDao() {
      return permissionDao;
  }
  
  public void setPermissionDao(PermissionDao permissionDao) {
      System.out.println("PerDAO set for Initializer");
      this.permissionDao = permissionDao;
  }
  
  public void initMethod() {
      System.out.println("Initializer called");
      initRolePerm();
      System.out.println("Initializer done");
  }
  
  private void initRolePerm(){
      Permission deletePermission = new Permission();
      deletePermission.setName("ABC");
      deletePermission.setType("ABC");
  
      Permission postPermission = new Permission();
      postPermission.setName("XYZ");
      postPermission.setType("XYZ");
  
      Role adminRole = new Role();
      adminRole.setRoleName("Admin");
  
      adminRole.getPermissionSet().add(postPermission);
      adminRole.getPermissionSet().add(deletePermission);
      permissionDao.insert(postPermission);
      permissionDao.insert(deletePermission);
  
      adminRole.getPermissionSet().add(postPermission);
      adminRole.getPermissionSet().add(deletePermission);
  
      roleDao.insert(adminRole);
  }
  }
  </code></pre>
  
  <p>Not sure how there are duplicates that are getting inserted into the table here. I run this initializer when the tomcat starts up. When I check the db there is exactly one duplicate for each of the row in T_ROLE, T_PERMISSION and eventually in T_ROLE_PERM. I am not able to figure out where the second row is being inserted from. TIA.</p>