hibernate many-to-many实例

该实例有两个对象User和Group,User和Group建立双向关联关系,有Group来管理关联关系。
1 User.hbm.xml
        <set
            name="groups"
            table="TB_UserGroup"
            lazy="true"
            inverse="true"  //Group来管理关联关系
            cascade="none"  //不使用级联操作
            sort="unsorted"
        >

            <key
                column="UserID"
            >
            </key>

            <many-to-many
                class="com.vo.Group"
                column="GroupID"
                outer-join="auto"
             />

        </set>

2 Group.hbm.xml
        <set
            name="users"
            table="TB_UserGroup"
            lazy="true"
            inverse="false"  //管理关联关系
            cascade="none"   //不使用级联操作 
            sort="unsorted"
        >
            <key
                column="GroupID"
            >
            </key>
            <many-to-many
                class="com.vo.User"
                column="UserID"
                outer-join="auto"
             />

        </set>

3 Group端的操作相对简单点
	public boolean deleteGroup(Long pid) {
		dao.removeObject(Group.class, pid);
		return true;
	}
         //添加修改Group
	public boolean saveGroup(Group p) {
		Long[] users=p.getNewUsers();
		HashSet<User> hs=new HashSet<User>();
		if(users!=null)
			for(int i=0;i<users.length;i++){
				hs.add(new User(users[i]));
			}
		p.setUsers(hs);		
		
		dao.saveObject(p);
		return true;
	}

添加Group的SQL语句:
Hibernate: insert into TB_Group (GroupSN, Name, Description, GroupID) values (?, ?, ?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
修改Group的SQL语句:
Hibernate: update TB_Group set GroupSN=?, Name=?, Description=? where GroupID=?
Hibernate: delete from TB_UserGroup where GroupID=?
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
删除Group的SQL语句:
Hibernate: delete from TB_UserGroup where GroupID=?
Hibernate: delete from TB_GroupPrivilege where GroupID=?
Hibernate: delete from TB_Group where GroupID=?
4 User端的操作复杂一点
	public boolean deleteUser(Long pid) {
		User u=this.getUserByID(pid);
		//删除旧关联的关系
		Set grps=u.getGroups();
		if(grps!=null){
			Iterator it=grps.iterator();
			Group g=null;
			
			while(it.hasNext()){
				g=(Group)it.next();
				g.getUsers().remove(u);
				dao.saveObject(g);
			}						
		}		
		dao.removeObject(User.class, pid);
		//u.setStatus(User.STATUS_UNABLE);//逻辑删除
		//dao.saveObject(u);
		return true;
	}


	public boolean saveUser(User p) {
		//先saveuser
		dao.saveObject(p); //必须先saveUser,因为Group管理管理关系,建立关联关系时候User必须是已经插入数据库

		//删除旧关联的关系
		Set grps=p.getGroups();
		if(grps!=null){
			Iterator it=grps.iterator();
			Group g=null;
			
			while(it.hasNext()){
				g=(Group)it.next();
				g.getUsers().remove(p);
				dao.saveObject(g);
			}						
		}
		
		Long[] groups=p.getNewGroups();			
		//将用户添加到每个组里
		Group g=null;
		if(groups!=null)
			for(int i=0;i<groups.length;i++){
				g=(Group)dao.getObject(Group.class, groups[i]);
				Set users=g.getUsers();
				users.add(p);
				
				dao.saveObject(g);
			}					
		return true;
	}

添加User的SQL:
Hibernate: insert into TB_User (UserSN, LoginName, Password, UserName, Sex, Email, Address, Birthday, PostalCode, MobilePhone, QQ, MSN, Status, OrganizationID, DepartmentID, UserID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
修改User的SQL:
Hibernate: select groups0_.UserID as UserID0_, groups0_.GroupID as GroupID0_ from TB_UserGroup groups0_ where groups0_.UserID=?
Hibernate: select group0_.GroupID as GroupID4_0_, group0_.GroupSN as GroupSN4_0_, group0_.Name as Name4_0_, group0_.Description as Descript4_4_0_ from TB_Group group0_ where group0_.GroupID=?
Hibernate: select users0_.GroupID as GroupID0_, users0_.UserID as UserID0_ from TB_UserGroup users0_ where users0_.GroupID=?
Hibernate: select group0_.GroupID as GroupID4_0_, group0_.GroupSN as GroupSN4_0_, group0_.Name as Name4_0_, group0_.Description as Descript4_4_0_ from TB_Group group0_ where group0_.GroupID=?
Hibernate: select users0_.GroupID as GroupID0_, users0_.UserID as UserID0_ from TB_UserGroup users0_ where users0_.GroupID=?
Hibernate: select group0_.GroupID as GroupID4_0_, group0_.GroupSN as GroupSN4_0_, group0_.Name as Name4_0_, group0_.Description as Descript4_4_0_ from TB_Group group0_ where group0_.GroupID=?
Hibernate: select users0_.GroupID as GroupID0_, users0_.UserID as UserID0_ from TB_UserGroup users0_ where users0_.GroupID=?
Hibernate: update TB_User set UserSN=?, LoginName=?, Password=?, UserName=?, Sex=?, Email=?, Address=?, Birthday=?, PostalCode=?, MobilePhone=?, QQ=?, MSN=?, Status=?, OrganizationID=?, DepartmentID=? where UserID=?
Hibernate: delete from TB_UserGroup where GroupID=?
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
Hibernate: insert into TB_UserGroup (GroupID, UserID) values (?, ?)
删除User的SQL
Hibernate: delete from TB_UserGroup where GroupID=? and UserID=?
Hibernate: delete from TB_UserGroup where GroupID=? and UserID=?
Hibernate: delete from TB_User where UserID=?

你可能感兴趣的:(DAO,sql,xml,Hibernate,qq)