异常:
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;
}
}