hibernate 多对多试验
通过用户与角色为例子,使用中间表,试验多对多的各种操作,包括
级联保存、更新、删除和查询。
创建用户表
CREATE TABLE TEST.TUSER(
UID BIGINT NOT NULL,
NAME VARCHAR(60),
PRIMARY KEY(UID)
)
创建角色表
CREATE TABLE TEST.TROLE(
RID BIGINT NOT NULL,
DESCR VARCHAR(200),
PRIMARY KEY(RID)
)
创建用户表和角色表的中间表
CREATE TABLE TEST.TUSER_ROLE(
UID BIGINT NOT NULL,
RID BIGINT NOT NULL,
PRIMARY KEY(UID,RID),
FOREIGN KEY(UID) REFERENCES TEST.TUSER (UID),
FOREIGN KEY(RID) REFERENCES TEST.TROLE(RID)
)
src/com/lwq/study/hib/imp/User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lwq.study.hib.imp">
<class name="User" table="TEST.TUSER">
<id name="userId" column="UID" type="long">
<generator class="increment"/>
</id>
<property name="userName" column="NAME" type="string"/>
<set name="roles" table="TEST.TUSER_ROLE" cascade="save-update">
<key column="UID" />
<many-to-many class="com.lwq.study.hib.imp.Role" column="RID"/>
</set>
</class>
</hibernate-mapping>
src/com/lwq/study/hib/imp/Role.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lwq.study.hib.imp">
<class name="Role" table="TEST.TROLE">
<id name="roleId" column="RID" type="long">
<generator class="increment"/>
</id>
<property name="description" column="DESCR" type="string"/>
<set name="users" table="TEST.TUSER_ROLE" cascade="save-update">
<key column="RID" />
<many-to-many class="com.lwq.study.hib.imp.User" column="UID"/>
</set>
</class>
</hibernate-mapping>
src/com/lwq/study/hib/imp/User.java
package com.lwq.study.hib.imp;
import java.util.HashSet;
import java.util.Set;
public class User {
private Long userId;
private String userName;
private Set roles;
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void addRole(Role role){
if(roles==null){
roles=new HashSet();
}
roles.add(role);
}
}
src/com/lwq/study/hib/imp/Role.java
package com.lwq.study.hib.imp;
import java.util.HashSet;
import java.util.Set;
public class Role {
private Long roleId;
private String description;
private Set users;
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getRoleId() {
return roleId;
}
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
public void addUser(User user){
if(users==null){
users=new HashSet();
}
users.add(user);
}
}
src/com/lwq/study/hib/sptest/DaoTest_Many_To_Many_Save.java
package com.lwq.study.hib.sptest;
import com.lwq.study.hib.IDao;
import com.lwq.study.hib.imp.Role;
import com.lwq.study.hib.imp.User;
import com.lwq.study.hib.junit.AbstractTestBean;
public class DaoTest_Many_To_Many_Save extends AbstractTestBean {
IDao dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (IDao) applicationContext.getBean("dao");
}
protected void tearDown() throws Exception {
super.tearDown();
}
/***************
*
* <p>
* 保存用户,关联的角色也将被保存。(实际应用意义不大)
* </p>
* @param
* @exception
* @return
*/
public final void testSaveUser() {
User user1=new User();
user1.setUserName("用户1");
Role role1=new Role();
role1.setDescription("角色一");
Role role2=new Role();
role2.setDescription("角色二");
Role role3=new Role();
role3.setDescription("角色三");
user1.addRole(role1);
user1.addRole(role2);
user1.addRole(role3);
dao.save(user1);
}
/***************
*
* <p>
* 保存角色,关联的用户也将被保存。(实际应用意义不大)
* </p>
* @param
* @exception
* @return
*/
public final void testSaveRole() {
Role role1=new Role();
role1.setDescription("角色B");
User user1=new User();
user1.setUserName("用户A");
User user2=new User();
user2.setUserName("用户B");
User user3=new User();
user3.setUserName("用户C");
role1.addUser(user1);
role1.addUser(user2);
role1.addUser(user3);
dao.save(role1);
}
/***************
*
* <p>
* 保存角色,关联原有用户数据。(较常用)
* </p>
* @param
* @exception
* @return
*/
public final void testSaveRoleWithOldUsers() {
Role role1=new Role();
role1.setDescription("角色xxxxxxxxxxxxxxxxx");
User userExtra =dao.getUser(new Long(8));
role1.addUser(userExtra);
dao.save(role1);
}
}
src/com/lwq/study/hib/sptest/DaoTest_Many_To_Many_Get.java
package com.lwq.study.hib.sptest;
import java.util.Iterator;
import java.util.List;
import com.lwq.study.hib.IDao;
import com.lwq.study.hib.imp.Role;
import com.lwq.study.hib.imp.User;
import com.lwq.study.hib.junit.AbstractTestBean;
public class DaoTest_Many_To_Many_Get extends AbstractTestBean {
IDao dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (IDao) applicationContext.getBean("dao");
}
protected void tearDown() throws Exception {
super.tearDown();
}
/*************
*
* <p>
* 通过用户取角色
* </p>
* @param
* @exception
* @return
*/
public final void testGetByUser() {
Long userId=new Long(1);
User user=dao.getUser(userId);
Iterator it=user.getRoles().iterator();
while(it.hasNext()){
Role role=(Role)it.next();
assertNotNull(role.getRoleId());
System.out.println(role.getDescription());
}
assertNotNull(user.getUserId());
}
/*********
*
* <p>
* 通过角色取用户
* </p>
* @param
* @exception
* @return
*/
public final void testGetByRole(){
Long roleId=new Long(4);
Role role=dao.getRole(roleId);
Iterator it=role.getUsers().iterator();
while(it.hasNext()){
User user =(User)it.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
/************
*
* <p>
* 多对多查询 通过用户条件过滤角色
* select 父 from 父类 父 left join 父.set 子 where 子.属性=XXXXX
* </p>
* @param
* @exception
* @return
*/
public final void testQueryRoles(){
List result =dao.queryRoles();
Iterator it=result.iterator();
while(it.hasNext()){
User user =(User)it.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
}
src/com/lwq/study/hib/sptest/DaoTest_Many_To_Many_UpdateByRole.java
package com.lwq.study.hib.sptest;
import java.util.Iterator;
import com.lwq.study.hib.IDao;
import com.lwq.study.hib.imp.Role;
import com.lwq.study.hib.imp.User;
import com.lwq.study.hib.junit.AbstractTestBean;
public class DaoTest_Many_To_Many_UpdateByRole extends AbstractTestBean {
IDao dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (IDao) applicationContext.getBean("dao");
}
protected void tearDown() throws Exception {
super.tearDown();
}
/****************
*
* <p>
* 某角色里加一个人:
* 先Load角色
* 在Load要关联的用户
* 将用户对象添加到角色的关联Set中
* 保存角色
* </p>
* @param
* @exception
* @return
*/
public final void testUpdateByRole_addRelation(){
Long roleId=new Long(4);
Role role=dao.getRole(roleId);
Iterator it=role.getUsers().iterator();
while(it.hasNext()){
User user =(User)it.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
User userExtra =dao.getUser(new Long(8));
role.addUser(userExtra);
dao.update(role);
Role role2=dao.getRole(roleId);
Iterator it2=role2.getUsers().iterator();
while(it2.hasNext()){
User user =(User)it2.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
/**************
*
* <p>
* 角色里去掉一个人:
* 先Load角色
* 遍历角色关联的用户Set
* 从Set中Remove满足条件的User对象
* 保存角色
* </p>
* @param
* @exception
* @return
*/
public final void testUpdateByRole_delRelation(){
Long roleId=new Long(4);
Role role=dao.getRole(roleId);
Iterator it=role.getUsers().iterator();
while(it.hasNext()){
User user =(User)it.next();
if(user.getUserId().equals(new Long(8))){
it.remove();
}
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
dao.update(role);
Role role2=dao.getRole(roleId);
Iterator it2=role2.getUsers().iterator();
while(it2.hasNext()){
User user =(User)it2.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
/**************
*
* <p>
* 更新角色时,添加一个新用户(这种情况实际用处不大,纯属试验):
* 先Load角色
* 创建一个新的用户
* 将该用户添加到角色的Set中
* 保存角色
* </p>
* @param
* @exception
* @return
*/
public final void testUpdateByRole_addUser(){
Long roleId=new Long(4);
Role role=dao.getRole(roleId);
Iterator it=role.getUsers().iterator();
while(it.hasNext()){
User user =(User)it.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
User userExtra =new User();
userExtra.setUserName("通过角色新添加的用户");
role.addUser(userExtra);
dao.update(role);
Role role2=dao.getRole(roleId);
Iterator it2=role2.getUsers().iterator();
while(it2.hasNext()){
User user =(User)it2.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
/**************
*
* <p>
* 重新设置角色关联的用户:
* 先Load角色
* 删除原有关联的用户
* 添加新的关联用户
* 保存角色
* </p>
* @param
* @exception
* @return
*/
public final void testUpdateByRole_ResetUsers(){
Long roleId=new Long(4);
Role role=dao.getRole(roleId);
role.setUsers(null);
//或者
//role.getUsers().removeAll(role.getUsers());
User userExtra =dao.getUser(new Long(8));
role.addUser(userExtra);
dao.update(role);
Role role2=dao.getRole(roleId);
Iterator it2=role2.getUsers().iterator();
while(it2.hasNext()){
User user =(User)it2.next();
assertNotNull(user.getUserId());
System.out.println(user.getUserName());
}
}
}
src/com/lwq/study/hib/sptest/DaoTest_Many_To_Many_DeleteRole.java
package com.lwq.study.hib.sptest;
import com.lwq.study.hib.IDao;
import com.lwq.study.hib.imp.Role;
import com.lwq.study.hib.junit.AbstractTestBean;
public class DaoTest_Many_To_Many_DeleteRole extends AbstractTestBean {
IDao dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (IDao) applicationContext.getBean("dao");
}
protected void tearDown() throws Exception {
super.tearDown();
}
/**************
*
* <p>
* 删除角色:
* </p>
* @param
* @exception
* @return
*/
public final void testDeleteRole(){
Long roleId=new Long(11);
Role role=dao.getRole(roleId);
dao.delete(role);
}
}