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

关于org.hibernate.NonUniqueObjectException 博客分类: hibernate HibernateBean 

程序员文章站 2024-03-16 09:44:58
...
具体异常说明:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

解决方案:
在多次检索过程中,共用一个Session对象


示例:
-----------------------------
/**
	 * <p>
	 * 更改指定用户的角色
	 * </p>
	 * 
	 * @author chenwei
	 * @param eid:用户ID
	 * @param roleids:角色ID数组
	 * 
	 */
	public static void opeRolesOfEmp(Integer eid, String[] roleids)
			throws HibernateMsgException {
		org.hibernate.Session session = null;
		org.hibernate.Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
			Employee emp = (Employee) session.load(Employee.class, eid);
			java.util.Set roles = new java.util.HashSet();
			for (int i = 0; i < roleids.length; i++) {
				//查询角色(此处应传入一个Session对象,否则会抛出org.hibernate.NonUniqueObjectException)
				Role role = new RoleDao().getRoleById(session,new Integer(roleids[i]));
				System.out.println("rolename=" + role.getRolename());
				roles.add(role);
			}
			emp.setRoles(roles);
			session.saveOrUpdate(emp);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}
	}




/**
	 * <p>
	 * 根据角色ID查找角色(传入一个Session对象)
	 * </p>
	 * 
	 * @param roleid:角色ID
	 * @return com.supersit.hibernate.bean.Role
	 * @throws HibernateMsgException
	 */
	public com.supersit.hibernate.bean.Role getRoleById(Session session,
			Integer roleid) throws HibernateMsgException {
		Role role = new Role();
		try {
			java.util.List list = session.createQuery(
					"from Role r where r.roleid=:id").setInteger("id", roleid)
					.list();
			if (list != null && list.size() > 0)
				role = (Role) list.get(0);

		} catch (Exception e) {

		}
		return role;
	}




/**
	 * <p>
	 * 根据角色ID查找角色(创建一个新的Session)
	 * </p>
	 * 
	 * @param roleid:角色ID
	 * @return com.supersit.hibernate.bean.Role
	 * @throws HibernateMsgException
	 */
	public com.supersit.hibernate.bean.Role getRoleById(Integer roleid)
			throws HibernateMsgException {
		Role role = new Role();
		Session session = null;
		Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
			java.util.List list = session.createQuery(
					"from Role r where r.roleid=:id").setInteger("id", roleid)
					.list();
			if (list != null && list.size() > 0)
				role = (Role) list.get(0);
			tran.commit();
		} catch (Exception e) {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}
		return role;
	}
相关标签: Hibernate Bean