关于org.hibernate.NonUniqueObjectException 博客分类: hibernate HibernateBean
程序员文章站
2024-03-16 09:49:22
...
具体异常说明: 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 批量删除
-
关于org.hibernate.NonUniqueObjectException 博客分类: hibernate HibernateBean
-
关于org.hibernate.NonUniqueObjectException 博客分类: hibernate HibernateBean
-
批量删除 博客分类: hibernate 批量删除
-
分页查询 博客分类: hibernate 分页查询
-
输出导航条 博客分类: hibernate HibernateJSP
-
分页查询 博客分类: hibernate 分页查询
-
关于配置 博客分类: hibernate HibernateSQLMyeclipseMicrosoftJDBC
-
面向接口编程在hibernate中的应用 博客分类: hibernate 编程HibernateJavaBeanDAO
-
hibernate下的根据日期查询 博客分类: mysql HibernateDAO