org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the sessio

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以通过sysconf(_SC_CLK_TCK)函数来获取系统的tick计数频率。这个频率表示内核的计时频率,而不是运行频率。具体的代码示例如下: #include <stdio.h> #include <unistd.h> #include <sys/times.h> int main() { long sysHz = sysconf(_SC_CLK_TCK); printf("系统的tick计数频率为:%ld\n", sysHz); unsigned long tickCount = (unsigned long) times(NULL); printf("当前tick计数为:%ul\n", tickCount); sleep(1); tickCount = (unsigned long) times(NULL); printf("一后的tick计数为:%ul\n", tickCount); sleep(1); tickCount = (unsigned long) times(NULL); printf("再过一后的tick计数为:%ul\n", tickCount); return 0; } 这段代码会输出系统的tick计数频率以及当前的tick计数,然后分别让程序休眠1并再次获取tick计数。这样就可以通过比较tick计数的差值来得到时间的流逝。注意,tick计数的单位是时钟滴答数,需要根据tick计数频率转换为实际的时间单位。 另外,在Linux中还可以使用clock_gettime函数来获取系统的时间。CLOCK_MONOTONIC代表系统启动后流逝的时间,而CLOCK_REALTIME代表现实的时间。具体的代码示例如下: #include <stdio.h> #include <time.h> int main() { struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); printf("系统启动后流逝的时间为:%ld %ld纳\n", tp.tv_sec, tp.tv_nsec); clock_gettime(CLOCK_REALTIME, &tp); printf("现实的时间为:%ld %ld纳\n", tp.tv_sec, tp.tv_nsec); return 0; } 这段代码会输出系统启动后流逝的时间和现实的时间,单位分别是和纳
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值