码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate映射(五)— 多对多

时间:2015-06-24 22:35:32      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:


单向:

情况:

一个用户拥有多个角色,一个角色可以包含多个用户。单用户可看到角色,角色看不到用户

原理:

两端都可以看到彼此

关系图:

技术分享

 

具体实现

1、实体
	user:
		/**
		 * 定义用户实体
		 * @author gxq
		 *
		 */
		public class User {
		
			private int id;
			
			private String name;
			
			private Set roles;
		
		
			public int getId() {
				return id;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			public String getName() {
				return name;
			}
		
			public void setName(String name) {
				this.name = name;
			}
		
			public Set getRoles() {
				return roles;
			}
		
			public void setRoles(Set roles) {
				this.roles = roles;
			}
		}
		
	role:
		package com.bjpowernode.hibernate;
		
		
		/**
		 * 定义角色实体
		 * @author gxq
		 *
		 */
		public class Role {
		
			private int id;
			
			private String name;
		
			public int getId() {
				return id;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			public String getName() {
				return name;
			}
		
			public void setName(String name) {
				this.name = name;
			}
			
			
		}
		
2、映射文件
	User.hbm.xml:
		<hibernate-mapping>
			<class name="com.bjpowernode.hibernate.User" table="t_user">
				<id name="id">
					<generator class="native"/>
				</id>
				<property name="name"/>
				
				<set name="roles" table="t_user_role">
					<key column="user_id"></key>
					<many-to-many class="com.bjpowernode.hibernate.Role" 
column="role_id"></many-to-many>
				</set>
			</class>
		</hibernate-mapping>
		
	Role.hbm.xml:
		<hibernate-mapping>
			<class name="com.bjpowernode.hibernate.Role" table="t_role">
				<id name="id">
					<generator class="native"/>
				</id>
				<property name="name"/>
			</class>
		</hibernate-mapping>
		
3、配置文件
	<hibernate-configuration>
		<session-factory>
			<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
			<property name="hibernate.connection.url">
	        <![CDATA[jdbc:mysql://localhost:3306/Hibernate?useUnicode=true&characterEncoding=utf8]]>
			</property>
			<property name="hibernate.connection.username">root</property>
			<property name="hibernate.connection.password">root</property>
			<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
			<property name="hibernate.hbm2ddl.auto">update</property>
			<property name="hibernate.show_sql">true</property>
			<!-- 
			<property name="hibernate.format_sql">true</property>
			 -->
			
			<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
			<mapping resource="com/bjpowernode/hibernate/Role.hbm.xml"/>
		</session-factory>
	</hibernate-configuration>
	
4、封装好的工具类:HibernateUtils 
	/**
	 * 工具类(封装开启session和事务)
	 * @classname   HibernateUtils 
	 * @author      高晓青
	 * @date        2015-4-16 下午2:56:42 
	 * @version     hibernate
	 */
	public class HibernateUtils {
		private static SessionFactory factory;
		
		static{
			try {
				//默认读取的是hibernate.cfg.xml
				Configuration cfg = new Configuration().configure();
				
				//建立sessionFactory
				factory = cfg.buildSessionFactory();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//开启session
		public static Session getSession(){
			return factory.openSession();
		}
		
		//关闭session
		public static void closeSession(Session session){
			//判断是否为空
			if(session!=null){
				//判断是否是打开状态再进行关闭
				if(session.isOpen()){
					session.close();
				}
			}
		}
	}
	
5、添加
	public void testSaveManyToMany1() {
			Session session = null;
	
			try {
				session = HibernateUtils.getSession();
				session.beginTransaction();
	
				// 定义经理角色并赋值
				Role managerRole = new Role();
				managerRole.setName("商务经理");
				session.save(managerRole);
	
				// 定义主管角色并赋值
				Role directorRole = new Role();
				directorRole.setName("商务主管");
				session.save(directorRole);
	
				// 定义职员角色并赋值
				Role clerkRole = new Role();
				clerkRole.setName("录入人员");
				session.save(clerkRole);
	
				// 设置管理员,并为其赋予橘色
				User admin = new User();
				admin.setName("张三");
				Set<Role> managerSet = new HashSet<Role>();
				managerSet.add(managerRole);
				managerSet.add(directorRole);
				managerSet.add(clerkRole);
	
				admin.setRoles(managerSet);
				session.save(admin);
	
				// 设置一般用户,并为其赋予角色
				User commonUser = new User();
				commonUser.setName("李四");
				Set<Role> commonSet = new HashSet<Role>();
				commonSet.add(directorRole);
				commonSet.add(clerkRole);
				commonUser.setRoles(commonSet);
				session.save(commonUser);
	
				session.getTransaction().commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

技术分享



 

 

Hibernate映射(五)— 多对多

标签:

原文地址:http://blog.csdn.net/gxq741718618/article/details/46627703

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!