问题描述
publicclassCollegeDaoImplTestextendsTestCase{publicvoidtestCollegeDaoImpl(){Sessionsession=HibernateUtils.getSession();session.beginTransaction();Queryquery=session.createQuery("selectcfromCollegec");for(inti=0;i<query.list().size();i++){Collegec=(College)query.list().get(i);System.out.println(c.getCollegeName());}session.getTransaction().commit();}}College类/***Thisiscollegeentity**@authorlpll*@hibernate.classtable="t_college"*/@SuppressWarnings("unchecked")publicclassCollege{/***collegeuniqueidentifierauto_increment**@hibernate.idgenerator-class="native"*/privateintid;/***college'sserialnumber**@hibernate.propertyunique="true"not-null="true"*/privateStringcollegeSerialNumber;/***college'sname**@hibernate.property*/privateStringcollegeName;/***collegeoffice**@hibernate.property*/privateStringcollegePlace;/***collegeincludestaffrooms**@hibernate.setlazy="extra"inverse="true"*@hibernate.keycolumn="collegeid"*@hibernate.one-to-manyclass="oa.dao.hbm.StaffRoom"*/privateSetstaffRooms;/***collegeincludemanyteachers**@hibernate.setlazy="flash"inverse="true"*@hibernate.keycolumn="collegeid"*@hibernate.one-to-manyclass="oa.dao.hbm.Teacher"*/privateSetteachers;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetCollegeSerialNumber(){returncollegeSerialNumber;}publicvoidsetCollegeSerialNumber(StringcollegeSerialNumber){this.collegeSerialNumber=collegeSerialNumber;}publicStringgetCollegeName(){returncollegeName;}publicvoidsetCollegeName(StringcollegeName){this.collegeName=collegeName;}publicStringgetCollegePlace(){returncollegePlace;}publicvoidsetCollegePlace(StringcollegePlace){this.collegePlace=collegePlace;}publicSetgetStaffRooms(){returnstaffRooms;}publicvoidsetStaffRooms(SetstaffRooms){this.staffRooms=staffRooms;}publicSetgetTeachers(){returnteachers;}publicvoidsetTeachers(Setteachers){this.teachers=teachers;}}执行上面的测试程序会暴空指针异常java.lang.NullPointerExceptionatorg.hibernate.type.AbstractType.getHashCode(AbstractType.java:112)atorg.hibernate.type.AbstractType.getHashCode(AbstractType.java:120)atorg.hibernate.engine.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:63)atorg.hibernate.engine.EntityUniqueKey.<init>(EntityUniqueKey.java:44)atorg.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)atorg.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)atorg.hibernate.loader.Loader.getRow(Loader.java:1206)atorg.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)atorg.hibernate.loader.Loader.doQuery(Loader.java:701)atorg.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)atorg.hibernate.loader.Loader.loadCollection(Loader.java:1994)atorg.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)atorg.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)atorg.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)atorg.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)atorg.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)atorg.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:797)atorg.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:241)atorg.hibernate.loader.Loader.doList(Loader.java:2220)atorg.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)atorg.hibernate.loader.Loader.list(Loader.java:2099)atorg.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)atorg.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)atorg.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)atorg.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)atorg.hibernate.impl.QueryImpl.list(QueryImpl.java:79)atoa.dao.impl.CollegeDaoImplTest.testCollegeDaoImpl(CollegeDaoImplTest.java:20)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atjunit.framework.TestCase.runTest(TestCase.java:164)atjunit.framework.TestCase.runBare(TestCase.java:130)atjunit.framework.TestResult$1.protect(TestResult.java:106)atjunit.framework.TestResult.runProtected(TestResult.java:124)atjunit.framework.TestResult.run(TestResult.java:109)atjunit.framework.TestCase.run(TestCase.java:120)atjunit.framework.TestSuite.runTest(TestSuite.java:230)atjunit.framework.TestSuite.run(TestSuite.java:225)atorg.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)但是其他的类就可以这样查询的,就这个类不行,配置文件也没问题,昨晚还可以的,就是今天不行了。。。。
解决方案
解决方案二:
怎么没人啊,急等啊,毕业设计自己先顶下
解决方案三:
hql查询语句写错了"fromCollegeasc"
解决方案四:
for(inti=0;i<query.list().size();i++){Collegec=(College)query.list().get(i);System.out.println(c.getCollegeName());}你怎么写要查多少次啊,换个写法试试吧~每次query.list()就查一下如果有n条记录你要查n+1次。
解决方案五:
publicclassCollegeDaoImplTestextendsTestCase{publicvoidtestCollegeDaoImpl(){Sessionsession=HibernateUtils.getSession();session.beginTransaction();Queryquery=session.createQuery("fromCollege");//Listlist=query.list();for(inti=0;i<list.size();i++){Collegec=(College)list().get(i);System.out.println(c.getCollegeName());}session.getTransaction().commit();}}
解决方案六:
publicclassCollegeDaoImplTestextendsTestCase{publicvoidtestCollegeDaoImpl(){Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromCollege");System.out.println(query.list().size());}}
解决方案七:
确保你的list有数据?
解决方案八:
那些都不是主要的,表中也有数据,主要是会报这种异常严重:Servlet.service()forservletjspthrewexceptionjava.lang.NullPointerExceptionatorg.hibernate.type.AbstractType.getHashCode(AbstractType.java:112)atorg.hibernate.type.AbstractType.getHashCode(AbstractType.java:120)atorg.hibernate.engine.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:63)atorg.hibernate.engine.EntityUniqueKey.<init>(EntityUniqueKey.java:44)atorg.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)atorg.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)atorg.hibernate.loader.Loader.getRow(Loader.java:1206)atorg.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)atorg.hibernate.loader.Loader.doQuery(Loader.java:701)atorg.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)atorg.hibernate.loader.Loader.loadEntity(Loader.java:1860)atorg.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)atorg.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)atorg.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)atorg.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)atorg.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)atorg.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)atorg.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)atorg.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)atorg.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)atorg.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)atorg.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)atorg.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)atoa.dao.hbm.StaffRoom$$EnhancerByCGLIB$$65968924.getStaffRoomName(<generated>)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atjavax.el.BeanELResolver.getValue(BeanELResolver.java:62)atjavax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)atorg.apache.el.parser.AstValue.getValue(AstValue.java:118)atorg.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)atorg.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:925)atorg.apache.jsp.admin.user.userIndex_jsp._jspService(userIndex_jsp.java:471)atorg.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)atorg.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)atorg.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)atorg.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)atorg.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)atorg.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)atorg.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)atorg.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)atorg.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)atorg.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)atorg.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)atorg.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:617)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atoa.filter.CharacterFilter.doFilter(CharacterFilter.java:30)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atoa.filter.OffsetAndPageSizeFilter.doFilter(OffsetAndPageSizeFilter.java:28)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)atjava.lang.Thread.run(UnknownSource)其他的类都可以,就是跟有关College的类就会出现这种异常
解决方案九:
是配置的问题的,正常情况是可能做到级联保存,但查询会出问题,里面是多对一的问题,我只知道问题出在这里,但没好的方法解决,也许是Hibernate一个足的地方吧!