在多对多XML映射中休眠重复行

时间:2015-03-23 18:44:28

标签: java mysql xml hibernate

我在stackoverflow中尝试了所有可能的解决方案。这是我的XML:

Role.hbm.xml:

<hibernate-mapping>
<class name="Role" table="T_ROLE"   dynamic-insert="true" dynamic-update="true">
    <id name="id" type="java.lang.String"  column="ROLE_ID" length="32">
        <generator class="uuid.hex" />
    </id>
    <property       name="roleName"         column="ROLENAME"           type="string"   length="50"     not-null="true" />
    <property       name="description"      column="DESCN"              type="string"   length="255"    not-null="false" />
    <property       name="createDatetime"   column="CREATE_DATETIME"    type="timestamp"/>
    <property       name="updateDatetime"   column="UPDATE_DATETIME"    type="timestamp"  />

    <set name="permissionSet" table="T_ROLE_PERM" inverse="false" lazy="true" fetch="select" cascade="all"  >
        <key>
            <column name="ROLE_ID" />
        </key>
        <many-to-many column="PERMISSION_ID" class="Permission" />
    </set>
</class>

Permission.hbm.xml:

<hibernate-mapping>
<class name="Permission" table="T_PERMISSION">
    <id name="id" type="java.lang.String" column="PERMISSION_ID" length="32">
        <generator class="uuid.hex" />
    </id>
    <property       name="name"             column="PERMNAME"           type="string"       not-null="true"     length="50" />
    <property       name="type"             column="TYPE"               type="string"       not-null="true"     length="20" />
    <property       name="description"      column="DESCN"              type="string"       not-null="false"    length="255"/>
    <property       name="createDatetime"   column="CREATE_DATETIME"    type="timestamp"/>
    <property       name="updateDatetime"   column="UPDATE_DATETIME"    type="timestamp"  />

    <set name="roleSet" table="T_ROLE_PERM" inverse="true" lazy="true" fetch="select" >
        <key>
            <column name="PERMISSION_ID" />
        </key>
        <many-to-many class="Role" column="ROLE_ID"/>          
    </set>

</class>

Initializer.Java:

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

不确定如何在此处插入重复项。我在tomcat启动时运行这个初始化程序。当我检查数据库时,T_ROLE,T_PERMISSION中的每一行都有一个副本,最终在T_ROLE_PERM中。我无法弄清楚第二行的插入位置。 TIA。

1 个答案:

答案 0 :(得分:1)

您已在角色实体中提供级联全部。保存角色实体也将保存权限实体。

参考Cascade

只需删除这些行并仅保存角色实体

permissionDao.insert(postPermission);
permissionDao.insert(deletePermission);

在权限中添加角色引用

 postPermission.getRoleSet().add(adminRole);
 deletePermission.getRoleSet().add(adminRole);

将方法修改为

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

  postPermission.getRoleSet().add(adminRole);
  deletePermission.getRoleSet().add(adminRole);
  adminRole.getPermissionSet().add(postPermission);
  adminRole.getPermissionSet().add(deletePermission);

  roleDao.insert(adminRole);
 }

也改变反转

inverse = true in role Entity and inverse = false in permission entity