2007-10-26
关于org.hibernate.NonUniqueObjectException
关键字: 关于org.hibernate.NonUniqueObjectException具体异常说明:
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;
}
发表评论
- 浏览: 17528 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
2008-6-26
共 122 张
共 122 张
最近加入圈子
最新评论
-
创建XMLHttpRequest对象及 ...
返回XML文档 PrintWriter out = null; try ...
-- by weiweichen1985 -
存储过程的创建和调用。。 ...
嵌套循环 CREATE DEFINER=`root`@`localhost` ...
-- by weiweichen1985 -
js实现的年月日三级联动
日历来的更简洁,让用户操作三遍,不符合用户体验。
-- by igogo007 -
js实现的年月日三级联动
谢谢了,代码我收藏了啊
-- by hanhan7673 -
word提示用安全模式打开
...
-- by sunfengcheng






评论排行榜