Cascade maintenance of the hibernate3 associated ing table

Source: Internet
Author: User

Development Environment: eclipse3.4.2 + weblogic10.3 + jdk1.6.0 _ 13 + Struts2.1.8 + Spring3.0.1 + Hiberante3.3.2


In the process of developing the permission management system, I used Spring security3, an open source component.

This open-source component has powerful functions, but for Chinese developers, users, roles, permissions, and resources cannot be dynamically managed using databases (of course, users and permissions are dynamically managed, but not suitable for Chinese developers ). In order to meet the requirements of using the database to dynamically manage users, roles, permissions, and resources, the author extended security3, in addition to creating the above four tables, A total of seven tables are created: user and role Association tables, role and permission Association tables, and permission and resource Association tables. And develop related setting modules for the above seven tables.

In terms of the relationship between users and roles, a single user can be associated with multiple roles, and there is a one-to-many relationship between them.
In terms of the relationship between roles and permissions, a role can be associated with Multiple permissions, which are one-to-multiple relationships.
In terms of the relationship between permissions and resources, one permission can be associated with multiple resources, and there is a one-to-many relationship between them.

The appendix only describes the modules for role and permission maintenance and associated relationship maintenance, and only describes the related saving and deletion code. The role table is Sys_Roles, the permission table is Sys_Authorities, and the association table is Sys_Roles_Authorities. On the maintenance page of the relationship between roles and permissions, you can allow users to add roles. You can also use a role to find the list of permissions related to this role. You can also use a role, use checkbox to select multiple permission lists to save the relationship between the role and Multiple permissions.

For more information about how to configure and deploy and run SSH2, see other related articles.

I. Action-Control Layer Code

Public class RolesAct extends ActionSupport {

...... The private variables defined are omitted.

...... Omit other methods, such as searching and saving roles.

/**//*
* Save the one-to-multiple relationship between roles and permissions.
*/
Public String savePermissionAndRole (){

RolesBuz. savePermissionAndRole (roleId, permisskey );

Return SUCCESS;
}

/**//*
* Delete a role
*/
Public String deleteRole (){

RolesBuz. deleteRole (roleId );

Return SUCCESS;
}

...... Omit getter and setter
}
 

Ii. dao-Persistence Layer Code

Public class SysRolesDao extends HibernateDaoSupport {

/***//**
* Delete A role based on its id.
*
* @ Author sparta
* @ Param roleId
*/
Public void delete (String roleId ){
Log. debug ("delete this role based on the role id! Role id: "+ roleId );
Try {

SysRoles role = findById (roleId );

GetHibernateTemplate (). delete (role );

Log. debug ("role id" + roleId + "deleted successfully! ");
} Catch (RuntimeException re ){
Log. error ("role id" + roleId + "failed to delete! ", Re );
Throw re;
}
}

/**//*
* Save the one-to-multiple relationship between roles and permissions.
*/
Public boolean savePermissionAndRole (String roleId, String [] permisskey ){

SysRoles sysRoles = findById (roleId );
SysAuthorities sysAuthorities = null;
SysRolesAuthorities sysRolesAuthorities;

// Delete all existing mappings before resetting
SysRolesAuthoritiesDao. deleteOldRoleAndPermissionRelative (roleId );

Try {
/**//*
* Extract all the permission IDS (permisskey) selected by the user in the front-end through the checkbox.
* Before saving the permission, you must first extract the instance with this permission by id,
* Provides data for the setSysAuthorities () of the role permission Association Table sysRolesAuthorities.
* Finally, save the newly created sysRolesAuthorities instance using the save () method of hibernate.
*/
For (String perId: permisskey ){
SysAuthorities = sysAuthoritiesDao. findById (perId );

SysRolesAuthorities = new SysRolesAuthorities ();
SysRolesAuthorities. setId (Util. getPkId ());
SysRolesAuthorities. setSysRoles (sysRoles );
SysRolesAuthorities. setSysAuthorities (sysAuthorities );
SysRolesAuthorities. setEnabled (true );

GetHibernateTemplate (). save (sysRolesAuthorities );
}

} Catch (Exception ex ){
Ex. printStackTrace ();
Return false;
}
Return true;
}
}

 


Iii. Table hibernate configuration file SysRoles. hbm. xml

 

<Hibernate-mapping>
<Class name = "avatar. base. security. entity. SysRoles" table = "SYS_ROLES" schema = "SCJDGL">
<Id name = "roleId" type = "string">
<Column name = "ROLE_ID" length = "32"/>
<Generator class = "assigned"/>
</Id>
<Property name = "roleName" type = "string">
<Column name = "ROLE_NAME" length = "40"/>
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.