org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
来源:互联网 发布:力港网络笔试题 编辑:程序博客网 时间:2024/05/18 03:15
异常:
exception
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.edu.bjtu.CRM.EB.po.Userinfo#4]
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:150)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
org.hibernate.impl.SessionImpl.load(SessionImpl.java:784)
cn.edu.bjtu.CRM.EB.dao.Imp.UserinfoDAO.deleteById(UserinfoDAO.java:100)
cn.edu.bjtu.CRM.EB.model.Imp.UserInfoManageModelImp.delete(UserInfoManageModelImp.java:154)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
又是一个令人头疼的异常:
有错误的代码:
public boolean deleteById(long id) {
log.debug("deleting userInfo instance");
try {
tx = getSession().beginTransaction();
//这里用了getSession();
Userinfo deleteInfo =new Userinfo();
getSession().load(deleteInfo, id);//跟据ID取得一个对象
//这里又用了getSession();
getSession().delete(deleteInfo);//这里还用了getSession();
log.debug("delete successful");
tx.commit();
getSession().close();//这里还敢用了getSession();
return true;
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
自己的分析:这个错误很明白,hibernate里面的同一个session有两个标识符相同但是不同的实体,运行CRUD时报着个错误,这个错误我遇到不止一次,第一次是在数据库插入更新时,解决方法是将update改为merge(),算是临时解决了问题,但是今天又遇到问题,分析了半天,原来是这里的session不是一个,所以,上述错误代码在于取用不同的session处理数据,导致实体重复……所以
解决方法:只要事先声明一个session对象,每次用它处理数据就OK了,还有,session用完之后最好关闭close;
改后代码:
public boolean deleteById(long id) {
log.debug("deleting userInfo instance");
try {
session=HibernateSessionFactory.getSessionFactory().openSession();
//等价于session=getSession();
tx = session.beginTransaction();
Userinfo deleteInfo =new Userinfo();
session.load(deleteInfo, id);//跟据ID取得一个对象
session.merge(deleteInfo);//不用可以,但是有时会出错,还是加上为好
session.delete(deleteInfo);
log.debug("delete successful");
tx.commit();
session.close();//最好关闭
return true;
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
- org.hibernate.NonUniqueObjectException:a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- Hibernate错误:a different object with the same identifier value was already associated with the sessio
- Hibernate Exception: a different object with the same identifier value was already associated with the session
- Hibernate更新a different object with the same identifier value was already associated with the session
- [Hibernate]a different object with the same identifier value was already associated with the session
- Hibernate异常: a different object with the same identifier value was already associated with the sessi
- Hibernate:a different object with the same identifier value was already associated with the session
- hibernate a different object with the same identifier value was already associated with the session
- Hibernate :A different object with the same identifier value was already associated with the session
- hibernate:a different object with the same identifier value was already associated with the session
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException:a different object with the same identifier value was alread
- c# GDI+简单绘图(四)
- 北大青鸟ASP.NET听课笔记一:001--Web应用程序的介绍
- org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
- 苹果欲借iPhone 4击溃Android防线
- 对ecmascript 中 prototype的理解
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- SQL Server 使用Log Explorer恢复日志方法 转自CSDN论坛
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio
- linux 调整系统时间
- 关于JTable不能显示表头的问题(个人小结)
- dl的使用
- C#四种排序算法
- 英语经典语句
- 拉伸图形形成solid实例