package main;

import hibernate.HibernateInitialize;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import role.Role;
import user.User;

public class Manager {
	/**
	 * 用户信息集合
	 */
	public static void main(String[] args) {
		Session session = null;
		try {
			// Hibernate地持久化操作
			session = HibernateInitialize.getSession();// 获取Session
			// 事务开启
			session.beginTransaction();
			// 装载用户对象
			User user = (User) session.get(User.class, new Integer("1"));
			// 获取权限名称集合
			Set<Role> roles = user.getRoles();
			System.out.println(user.getName() + "用户所拥有地权限为");
			// 通过迭代输出信息
			for (Iterator<Role> it = roles.iterator(); it.hasNext();) {
				Role roles2 = (Role) it.next();
				System.out.println(roles2.getRoleName() + "||");
			}

			// 获取用户名称集合
			Role rol = (Role) session.get(Role.class, new Integer("2"));
			// 获取用户名集合
			Set<User> users = rol.getUsers();// 获取用户名称集合
			System.out.println(rol.getRoleName() + "劝降杯赋予用户名:");
			// 通过迭代输出信息
			for (Iterator<User> it = users.iterator(); it.hasNext();) {
				User users2 = (User) it.next();
				System.out.println(users2.getName() + "||");
			}
			// 事务提交
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			// 事务回滚
			session.getTransaction().rollback();
		} finally {
			// 关闭session
			HibernateInitialize.closeSession();
		}
	}
}
