gpt4 book ai didi

java - Hibernate 多对多 XML 映射中的重复行

转载 作者:行者123 更新时间:2023-11-29 12:19:39 25 4
gpt4 key购买 nike

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

角色.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>

权限.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>

初始化程序.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。

最佳答案

您已在角色实体中指定Cascade All。保存角色实体也会保存权限实体。

引用号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 和权限实体中的 inverse=false

关于java - Hibernate 多对多 XML 映射中的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com