hibernate配置文件:
<hibernate-mapping> <class name="com.database.BoxBillMng" table="BOX_BILL_MNG" schema="ZHENGJIANG"> <id name="billId" type="java.lang.Long"> <column name="BILL_ID" precision="2" scale="0" /> <generator class="sequence"> <param name="sequence">BoxBILLMng_sequence</param> </generator> </id> <many-to-one name="boxBillType" class="com.database.BoxBillType" fetch="select"> <column name="BILL_TPID" precision="2" scale="0" not-null="true" /> </many-to-one> <property name="boxMac" type="java.lang.String"> <column name="BOX_MAC" length="12" not-null="true" /> </property> <property name="icCode" type="java.lang.String"> <column name="IC_CODE" length="20" not-null="true" /> </property> <property name="userTel" type="java.lang.String"> <column name="USER_TEL" length="15" not-null="true" /> </property> <property name="billAmnt" type="java.lang.Double"> <column name="BILL_AMNT" precision="10" not-null="true" /> </property> <property name="appTime" type="java.util.Date"> <column name="APP_TIME" length="7" /> </property> <property name="openTime" type="java.util.Date"> <column name="OPEN_TIME" length="7" /> </property> <property name="leftTime" type="java.lang.Long"> <column name="LEFT_TIME" precision="10" scale="0" /> </property> <property name="state" type="java.lang.Long"> <column name="STATE" precision="1" scale="0" not-null="true" /> </property> </class> </hibernate-mapping>
执行查询 List<BoxBillMng> boxBillMngLst = boxBillMngDAO.findByBoxMac(MacAddress);
时报错:Hibernate: select boxbillmng0_.BILL_ID as BILL1_7_, boxbillmng0_.BILL_TPID as BILL2_7_, boxbillmng0_.BOX_MAC as BOX3_7_, boxbillmng0_.IC_CODE as IC4_7_, boxbillmng0_.USER_TEL as USER5_7_, boxbillmng0_.BILL_AMNT as BILL6_7_, boxbillmng0_.APP_TIME as APP7_7_, boxbillmng0_.OPEN_TIME as OPEN8_7_, boxbillmng0_.LEFT_TIME as LEFT9_7_, boxbillmng0_.STATE as STATE7_ from ZHENGJIANG.BOX_BILL_MNG boxbillmng0_ where boxbillmng0_.BOX_MAC=?
java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:830)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:849)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:840)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:858)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
查找问题:org.hibernate.tuple.AbstractEntityTuplizer.createProxy
网上说在<many-to-one name="boxBillType" class="com.hansun.p2pdatabase.BoxBillType" fetch="select">
<column name="BILL_TPID" precision="2" scale="0" not-null="true" />
</many-to-one>
中加 lazy="false" 属性
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-manytoone
加上后 <many-to-one name="boxBillType" lazy="false" class="com.hansun.p2pdatabase.BoxBillType" fetch="select">
<column name="BILL_TPID" precision="2" scale="0" not-null="true" />
</many-to-one>
重启问题解决。
2.插入时出现违反唯一性约束条件的问题,加入dynamic-insert="true" dynamic-update="true"两个属性试试
<class name="com.hansun.p2pdatabase.BoxBillMng" table="BOX_BILL_MNG" schema="ZHENGJIANG" dynamic-insert="true" dynamic-update="true">