首页 > 代码库 > 多对多转化一对多

多对多转化一对多

1、由问题引出一个多对多拆分成两个多对一

    问题:Hibernate 关联映射 之 多对多 关联(一)中中间表只是一个存放用户和角色的表,并无其他作用,如果客户有其他的需求,该表就无法扩展。

 

2、问题解决过程:

用户、角色 之间的关系拆分成用户和用户角色、角色和用户角色的关系,即:两个多对一的关系。

小技巧:如何去判断多对一还是一对多对象,要从对象的角度来看,即:从一个对象的角度去看另一个对象。

用户:

public class User {

 

        private int id;

        private String name;

        private String password;

        private Set<UserRole> roles;

用户配置文件:

<hibernate-mapping>

        <class name="com.shangxuetang.User" table="t_user">

               <id name="id">

                       <generator class="native"/>

               </id>

               <property name="name"></property>

               <property name="password"></property>

               <set name="roles">

                   <key column="userID"></key>

                   <one-to-many class="com.shangxuetang.UserRole"/>

               </set>

        </class>

</hibernate-mapping>

角色:

public class Role {

        private int id;

        private String name;

        private Set<UserRole> users;

角色配置文件:

<hibernate-mapping>

        <class name="com.shangxuetang.Role" table="t_role">

               <id name="id">

                       <generator class="native"/>

               </id>

               <property name="name"></property>

               <set name="users">

                    <key column="roleID"></key>

                    <one-to-many class="com.shangxuetang.UserRole"/>

               </set>

        </class>

</hibernate-mapping>

用户角色:

public class UserRole {

 

        private int id;

        private User user;

        private Role role;

        private Date assignTime;

       

        public UserRole() {}

        public UserRole(User u,Role r) {

               this.user = u;

               this.role = r;

               this.assignTime = new Date();

        }

用户角色配置文件:

<hibernate-mapping>

        <class name="com.shangxuetang.UserRole" table="t_userrole">

               <id name="id">

                       <generator class="native"/>

               </id>

               <property name="assignTime" type="date"></property>

               <many-to-one name="user" column="userID"></many-to-one>

               <many-to-one name="role" column="roleID"></many-to-one>

              

        </class>

</hibernate-mapping>

测试示例代码:

public void  testManytoManyChaiFen() {

               Session session = HibernateUtil.getSession();

               try {

                       session.beginTransaction();

                      

                       User u1 = new User("zhangsan");

                       session.save(u1);

                       User u2 = new User("lisi");

                       session.save(u2);

                      

                       Role r1 = new Role("adminstor");

                       session.save(r1);

                       Role r2 = new Role("usualuser");

                       session.save(r2);

                       Role r3 = new Role("VIPuser");

                       session.save(r3);

                      

                       UserRole ur1 = new UserRole(u1,r1);

                       session.save(ur1);

                      

                       UserRole ur2 = new UserRole(u1,r2);

                       session.save(ur2);

                      

                       UserRole ur3 = new UserRole(u2,r1);

                       session.save(ur3);

                      

                       UserRole ur4 = new UserRole(u2,r3);

                       session.save(ur4);

                      

                       UserRole ur5 = new UserRole(u2,r2);

                       session.save(ur5);

                      

                       session.getTransaction().commit();

               }catch(Exception e) {

                       e.printStackTrace();

                       session.getTransaction().rollback();

               }finally {

                       session.close();

               }

        }

 

多对多转化一对多