欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate框架双向多对多配置,以及级联操作

程序员文章站 2022-04-24 14:19:44
...

java代码

package bean;

import java.util.HashSet;
import java.util.Set;

public class Role {
	private Long role_id;
	private String role_name;//'角色名称',
	private String role_memo;// '备注',
	private Set<User> users=new HashSet<User>();
	public Long getRole_id() {
		return role_id;
	}
	public void setRole_id(Long role_id) {
		this.role_id = role_id;
	}
	public String getRole_name() {
		return role_name;
	}
	public void setRole_name(String role_name) {
		this.role_name = role_name;
	}
	public String getRole_memo() {
		return role_memo;
	}
	public void setRole_memo(String role_memo) {
		this.role_memo = role_memo;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
}






package bean;

import java.util.HashSet;
import java.util.Set;

public class User {
	private Long user_id;//'用户id',
	private String user_code;// '用户账号',
	private String user_name;// '用户名称',
	private String user_password;// '用户密码',
	private String user_state;//'1:正常,0:暂停',
	private Set<Role> role=new HashSet<Role>();
	public Long getUser_id() {
		return user_id;
	}
	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}
	public String getUser_code() {
		return user_code;
	}
	public void setUser_code(String user_code) {
		this.user_code = user_code;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getUser_password() {
		return user_password;
	}
	public void setUser_password(String user_password) {
		this.user_password = user_password;
	}
	public String getUser_state() {
		return user_state;
	}
	public void setUser_state(String user_state) {
		this.user_state = user_state;
	}
	public Set<Role> getRole() {
		return role;
	}
	public void setRole(Set<Role> role) {
		this.role = role;
	} 
}

xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="bean.Role" table="sys_role">
    		<id name="role_id" column="role_id"> 
    			<generator class="native"></generator>
    		</id>
    		<property name="role_name" column="role_name"></property>
    		<property name="role_memo" column="role_memo"></property>
    		<!-- 多对多的关系  集合(roles) 
				name:自己里面对方的集合名
				table:中间表的表名
			-->
    		<set name="users" table="sys_user_role" inverse="true" cascade="save-update,delete">
				<!-- 自己在中间表的外键字段名 -->
				<key column="role_id"></key>
				<!-- class:对方全限定名
				column:对方在中间表的外键字段名 -->
				<many-to-many class="bean.User" column="user_id"></many-to-many>    		
    		</set>
    	</class>
    </hibernate-mapping>







<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="bean.User" table="sys_user">
    		<id name="user_id" column="user_id"> 
    			<generator class="native"></generator>
    		</id>
    		<property name="user_code" column="user_code"></property>
    		<property name="user_name" column="user_name"></property>
    		<property name="user_password" column="user_password"></property>
    		<property name="user_state" column="user_state"></property>
    		<!-- 多对多的关系  集合(roles) 
				name:自己里面对方的集合名
				table:中间表的表名
			-->
    		<set name="role" table="sys_user_role" cascade="save-update">
				<!-- 自己在中间表的外键字段名 -->
				<key column="user_id"></key>
				<!-- class:对方全限定名
				column:对方在中间表的外键字段名 -->
				<many-to-many class="bean.Role" column="role_id"></many-to-many>    		
    		</set>
    	</class>
    </hibernate-mapping>

级联操作

package test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Role;
import bean.User;
import utils.HibernateUtils;
//多对多
public class test2 {

	public static void main(String[] args) {
		
		//普通保存
		//test1();
		//级联保存
		//test2();
		//普通删除
		//test3();
		//级联删除
		//test4();
		 // 多对多的其它操作
		
		  // 给jack用户分配一个角色
		//test5();
		 // 给rose用户删除一个角色
		//test6();
		 // 给rose用户修改一个角色
		test7();
	}
	
	private static void test7() {
		// 给rose用户修改一个角色
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		User user=session.get(User.class, 2L);
		//获取到以前的角色
		Role role=session.get(Role.class, 2L);
		//获取到修改以后的角色
		Role role1=session.get(Role.class, 3L);
		user.getRole().remove(role);
		user.getRole().add(role1);
		
		tx.commit();
		session.close();
	}

	private static void test6() {
		 // 给rose用户删除一个角色
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		User user=session.get(User.class, 2L);
		Role role=session.get(Role.class, 3L);
		user.getRole().remove(role);
		tx.commit();
		session.close();
		
		
	}

	private static void test5() {
		 // 给jack用户分配一个角色
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		User user=session.get(User.class, 1L);
		Role role=session.get(Role.class, 3L);
		user.getRole().add(role);
		tx.commit();
		session.close();
		
	}

	private static void test4() {
		// TODO Auto-generated method stub
		// 需求: 级联删除rose用户
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
//		User user = session.get(User.class, 1L);
//		session.delete(user);
		Role r=session.get(Role.class, 4L);
		session.delete(r);
		tx.commit();
		session.close();
	}

	private static void test3() {
		Session session = HibernateUtils.openSession();
		Transaction tr = session.beginTransaction();
		
		User user=session.get(User.class, 4L);
		session.delete(user);
		
		tr.commit();
		session.close();
		
		
		
	}

	private static void test2() {
		
		Session session = HibernateUtils.openSession();
		Transaction tr = session.beginTransaction();
		//多对多表的关系中,被动的一方放弃外键维护权
		//保存两个用户,三个职业
		User user = new User();
		User user2=new User();
		
		user.setUser_name("jack1");
		user2.setUser_name("rose1");
		
		Role role1 =new Role(); 
		Role role2 =new Role(); 
		Role role3 =new Role(); 
		
		role1.setRole_name("法师");
		role2.setRole_name("战士");
		role3.setRole_name("道士");
		//让用户1关联
		user.getRole().add(role1);//user-法师
		user.getRole().add(role3);//user-道士
		//让用户2关联
		user2.getRole().add(role2);//user2-战士
		user2.getRole().add(role3);//user2-道士
		
		//role1关联
		role1.getUsers().add(user);
		//role2关联;
		role2.getUsers().add(user2);
		//role3关联
		role3.getUsers().add(user);
		role3.getUsers().add(user2);
		
		//保存用户
		session.save(user);
		session.save(user2);
		//保存职业
//		session.save(role1);
//		session.save(role2);
//		session.save(role3);
		tr.commit();
		session.close();
		
	}

	private static void test1() {
		Session session = HibernateUtils.openSession();
		Transaction tr = session.beginTransaction();
		//多对多表的关系中,被动的一方放弃外键维护权
		//保存两个用户,三个职业
		User user = new User();
		User user2=new User();
		
		user.setUser_name("jack");
		user2.setUser_name("rose");
		
		Role role1 =new Role(); 
		Role role2 =new Role(); 
		Role role3 =new Role(); 
		
		role1.setRole_name("法师");
		role2.setRole_name("战士");
		role3.setRole_name("道士");
		//让用户1关联
		user.getRole().add(role1);
		user.getRole().add(role3);
		//让用户2关联
		user2.getRole().add(role2);
		user2.getRole().add(role3);
		
		//role1关联
		role1.getUsers().add(user);
		//role2关联;
		role2.getUsers().add(user2);
		//role3关联
		role3.getUsers().add(user);
		role3.getUsers().add(user2);
		
		//保存用户
		session.save(user);
		session.save(user2);
		//保存职业
		session.save(role1);
		session.save(role2);
		session.save(role3);
		tr.commit();
		session.close();
		
	}

}