问题描述
Struts Problem ReportStruts has detected an unhandled exception:[/color]Messages:No more resultsFile:[color=blue]org/hibernate/impl/IteratorImpl.java[/size]Line number:132Stacktracesjava.util.NoSuchElementException: No more results[size=x-small] org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:132) action.LoginAction.getSpace(LoginAction.java:88) action.LoginAction.execute(LoginAction.java:42) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) java.lang.Thread.run(Thread.java:619) 问题补充:这是getSpace( )函数:public void getSpace() {String sql1 = "from Space where username='"+username+"'";SessionFactory sessionFactory1 = new Configuration().configure().buildSessionFactory();Session session1 = sessionFactory1.openSession();Transaction tx1 = session1.beginTransaction();Query q1 = session1.createQuery(sql1);Space space = (Space)q1.iterate().next();HttpServletRequest request1 = ServletActionContext.getRequest();HttpSession sess1 = request1.getSession();sess1.setAttribute("current_userspace", space);tx1.commit();}怎么还要判断的吗?请问怎样修改呢?
解决方案
虽然不知道里面space变量哪来的,不过如果已经有了,不用重新load 直接用也可以的,另外,你要确定userspace=(Space)session.load(Space.class, space.getSpaceID());里面space.getSpaceID()是存在的而不是nullpublic void updateSpace() { int updateused; int updateunused; updateused = space.getUsedsize()+(int)file.length(); updateunused = space.getUnusedsize()-(int)file.length(); SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); space.setUnusedsize(updateunused); space.setUsedsize(updateused); session.update(space); session.flush(); tx.commit(); session.close(); }
解决方案二:
引用应该们问题呀:username 两张表都是varchar类型,长度为45;你们这个是怎么关联的呢?是用外键么,其实你可以不用username关联的啊,直接用user表里的id当做userspace表里的外键
解决方案三:
应该是米没有查找到关联的对象,两个表关联字段的值不匹配提示说userspace.setUnusedsize(updateunused); 这句有问题
解决方案四:
引用有什么本质的不同吗? 这个这个防止SQL注入吧,如果把username直接拼接上去,可能别人会输入特殊的字符串 比如 aaa' or '1'= '1拼接起来就是from Space where username= 'aaa' or '1'= '1'"from Space where username= :name"则是留一个参数位置给 username,填入的值只能当做是username的值 而不能拼接,hql会自动把 :name 换成q1.setParameter("name", username); 里设置的值
解决方案五:
Space space = (Space)list.get(0);这里最好再加上判断if(list!= null && !list.isEmpty()) { space = (Space)list.get(0);}
解决方案六:
因为可能取出来的是空集,所以要判读吧另外一般查询是不需要transaction没关系的,updat和insert是一定要用transaction的public void getSpace() { String sql1 = "from Space where username= :name"; SessionFactory sessionFactory1 = new Configuration().configure().buildSessionFactory(); Session session1 = sessionFactory1.openSession(); Query q1 = session1.createQuery(sql1); q1.setParameter("name", username);List list = q1.list(); //如果结果有多个Space space = (Space)list.get(0); //如果结果确定是唯一的,直接 Space space=(Space) q1.uniqueResult();HttpServletRequest request1 = ServletActionContext.getRequest(); HttpSession sess1 = request1.getSession(); sess1.setAttribute("current_userspace", space); session1.close();}
解决方案七:
action.LoginAction.getSpace() 里调用IteratorImpl.next()出错了 iterator遍历的时候没有判断还有没有下一个就直接去取数据了
解决方案八:
java.util.NoSuchElementException: No more resultsaction.LoginAction.getSpace()取出的是空值,没有判断直接next()了?