2015-7-25 18:29:33 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet [RegisterServlet] in context with path [/FirstHRD1] threw exception
java.lang.NullPointerException
at com.reitgn.dao.impl.AdvancedRegistrationDaoImpl.addAdvancedRegistration(AdvancedRegistrationDaoImpl.java:34)
at com.reitgn.service.impl.AdvanceRegistrationServiceImpl.addAdvancedRegistration(AdvanceRegistrationServiceImpl.java:34)
at com.reitgn.servlet.RegisterServlet.doPost(RegisterServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2476)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2465)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)
我的想法:
Hibernate:DaoImpl類繼承HibernateDaoSupport,使用getHibernateTemplate()或者Session操作數據。
Spring:加進去什麼都不干,就是使用C3P0。
Servlet:控制等等。
幫忙看看我的applicationContext.xml對不對:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/firsthrd">
</property>
<property name="username" value="root"></property>
<property name="password" value="518wsgtc"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/reitgn/entity/AdvancedRegistration.hbm.xml</value>
<value>com/reitgn/entity/CurrentWorkUnit.hbm.xml</value>
<value>com/reitgn/entity/ChinaQualification.hbm.xml</value>
<value>com/reitgn/entity/InternationalQualification.hbm.xml</value>
<value>com/reitgn/entity/Recommended.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" /> <!-- 支持當前事務,如果執行到save開頭的任何方法時沒有事務則開啟一個事務 這是最常見的方式 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="revise*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <!-- 支持當前事務,如果當前沒有事務,就以非事務方式執行。只讀 -->
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="productServiceMethods"
expression="execution(* com.reitgn.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
</aop:config>
目錄結構:
java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session
hibernate 4 中沒有 HibernateDaoSupport,包括數據訪問都不需要 hibernatetemplate,必須改寫 dao,直接使用 hibernate 的 session 和 query 接口。
我使用了 Hibernate 4 的同時使用了 getHibernateTemplate().save(xxx) 所以報錯找不出原因,可以的話將 Hibernate 3 中對應的 jar 包復制過來覆蓋一下或者復制 jar 裡面的方法,改寫 Hibernate 4 的 jre,具體百度。