Many-to-many relationship transformations in hibernate

Source: Internet
Author: User

    1. Source of the problem
      When using the SSH architecture to develop a Web application, there is always a one-to-many, many-to-a, many-to-many relationships between tables, and for many-to-many relationships, in terms of operation and performance are not ideal, so many-to-many mapping use less, in practice, preferably converted into a one-to-many object model Hibernate will create an intermediate association table for us, converted to two one-to-many.

    2. Problem solving
      A multi-to-many relationship between role tables and permissions in the development of an OA project provides recommendations for solving problems
      Example: Roles and Permissions
      (1) See Requirements first

      (2) Analysis
      First, roles and permissions: Many-to-many
      A role can have multiple permissions, and a permission can be used by multiple roles
      Second, in the role of add and remove changes involved in the permissions, so for two-way (different from the second example), in this will be a lot of a simple transformation of the relationship, through the form of the intermediate table (Role_privilege) saved to the database
      Thirdly, according to the demand, the authority is fixed, not a single table, of course, also can not construct a separate entity class, which is set to map

 Public classConstant {/ *----------system Rights Management-----------------------* /     Public StaticString PRIVILEGE_XZGL ="Xzgl"; Public StaticString PRIVILEGE_HQFW ="HQFW"; Public StaticString privilege_zxxx ="Zxxx"; Public StaticString PRIVILEGE_NSFW ="NSFW"; Public StaticString Privilege_space ="Spaces"; Public StaticMap<string,string> Privilege_map;Static{Privilege_map =NewHashmap<string, string> (); Privilege_map.put (PRIVILEGE_XZGL,"Administration"); Privilege_map.put (PRIVILEGE_HQFW,"Logistics Service"); Privilege_map.put (Privilege_zxxx,"Online learning"); Privilege_map.put (PRIVILEGE_NSFW,"Tax Services"); Privilege_map.put (Privilege_space,"My Space."); }
(3)问题解决在role_Privilege表中是联合主键,在此将联合主键通过类的方式进行设置,联合主键要求:实现序列化接口、重写hashCode()和equals方法,设置的原因:在对角色Entity进行增删改查时,需要将Role与联合主键的Role进行equals,至于实现Serializable,则是对Entity方便IO传输Demo:Role实体类:
publicclass Role implements Serializable {    private String roleId;    private Set<RolePrivilege> rolePrivileges;//因为根据需求进行增删改查需要用到权限,双向    ............

Roleprivilege class:

publicclass RolePrivilege implements Serializable {    private RolePrivilegeId id;//联合主键

Roleprivilegeid class:

publicclass RolePrivilegeId implements Serializable {    //为什么运用Role而不是运用roleId,原因:需求    private Role role;//角色    //private String roleId;//角色    private String code;//权限

Configuration file Role.hbm.xml:

        <!--1. A many-to-many relationship between the role table and the permission table is converted into a one-to-many 2 by the intermediate table. Setting inverse to true means canceling unilateral maintenance 3. Lazy loading: Load when needed, in this setting False 4. Set Cascade: When updating and deleting, you need to delete the corresponding data in Role_privilege, save        <set name="roleprivileges" inverse="true" lazy=" False " cascade=" Save-update,delete ">            <key>                <column name="role_id"></column>            </key>            <one-to-many class="Cn.test.nsfw.role.entity.RolePrivilege"/>        </Set>

Configuration file RolePrivilege.hbm.xml

class name="Cn.test.nsfw.role.entity.RolePrivilege"table="Role_privilege"> <composite-ID name="id" class="Cn.test.nsfw.role.entity.RolePrivilegeId"> <key-many- to-onename="Role" class="Cn.test.nsfw.role.entity.Role"lazy="false"> <columnname="role_id"></column> </key-many- to-one> <key- Property name="Code"Type="Java.lang.String"> <columnname="Code" length="a"></column> </key- Property> </composite-ID> </class>

Operation Result:


3. Summary
(1) Do not neglect the importance of demand, he is the direction of our development process (2) personal feeling the way XML configuration files are used is more structured than annotations

Many-to-many relationship transformations in hibernate

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.