hibernate 多对多处理设置
例如角色与权限:
角色中:
private Set privileges= new HashSet();
权限中:
private Set<Role> roles = new HashSet<Role>();
<class name="entity.Role" table="ROLE" dynamic-update="true">
<id name="roleId" type="java.lang.Integer">
<column name="roleID" />
<generator class="identity"></generator>
</id>
<property name="roleName" type="java.lang.String">
<column name="roleName" length="50" />
</property>
<property name="mender" type="java.lang.Integer">
<column name="mender" />
</property>
<property name="modifyTime" type="java.sql.Timestamp">
<column name="modifyTime" length="50" />
</property>
<set name="privileges" cascade="all" table="ROLE_PRIVILEGE" lazy="true">
<key column="roleID" />
<many-to-many class="entity.Privilege" column="pvID" order-by="sequence"/>
</set>
</class>
<class name="entity.Privilege" table="PRIVILEGE">
<id name="pvId" type="java.lang.Integer">
<column name="pvID" />
<generator class="identity"></generator>
</id>
<property name="parentId" type="java.lang.Integer">
<column name="parentID" />
</property>
<property name="pvName" type="java.lang.String">
<column name="pvName" length="50" />
</property>
<property name="level" type="java.lang.Short">
<column name="level" />
</property>
<property name="url" type="java.lang.String">
<column name="url" length="50" />
</property>
<property name="isView" type="java.lang.Short">
<column name="isView" />
</property>
<property name="modifyTime" type="java.sql.Timestamp">
<column name="modifyTime" length="50" />
</property>
<set name="roles" table="ROLE_PRIVILEGE" lazy="true" cascade="none">
<key column="pvID" />
<many-to-many class="entity.Role" column="roleID" />
</set>
</class>
修改角色的时候 1 根据id去查询角色实体
2 将需要更新的字段更新到查询出来的角色实体中
修改角色的权限 1删除当前角色的权限
2 添加当前角色权限
实例:
public void updatePvRole(Integer[] pvIds , Integer roleId ){
if( pvIds==null || pvIds.length==0){
return ;
}
Role role = roleDao.getRole(roleId);
Set setOld = role.getPrivileges();
Iterator it = setOld.iterator();
while(it.hasNext()){
it.next();
it.remove();
}
for( Integer pvId :pvIds ){
Privilege privilege = privilegeDao.getPrivilege( pvId );
setOld.add( privilege );
}
roleDao.updateRole( role );
}
hibernate多对多
猜你喜欢
转载自username2.iteye.com/blog/1668997
今日推荐
周排行