运行getHibernateTemplate().save(xxx)时报错等问题 两天了坐等

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,具体百度。

空指针异常,会不会是配置文件配置的不对呀

会不会是你的dao没有依赖注入啊

1.在applicationContext.xml配置c3p0:

<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池: -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

jdbc.properties内容如下:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///firsthrd
jdbc.user = root
jdbc.password =518wsgtc

2.要配置相关的Service和dao,如果用到action,也要进行配置,不用,可以不配置。
但是dao一定要配置,因为你要用到hibernate的SessionFactory
<!-- Service的配置 id是接口名称的简单名称,class是实现类的全限定名 ===========================-->


<!-- Dao的配置  id是接口名称的简单名称,class是实现类的全限定名 ===========================-->
<bean id="advancedRegistrationDao" class="com.reitgn.dao.impl.AdvancedRegistrationDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
3.在你的Service中注入dao
  // 注入advancedRegistrationDao
private AdvancedRegistrationDao advancedRegistrationDao;

public void setAdvancedRegistrationDao(AdvancedRegistrationDao advancedRegistrationDao) {
    this.advancedRegistrationDao = advancedRegistrationDao;
}

使用advancedRegistrationDao做增删改查。

4.在dao中,继承HibernateDaoSupport
可以使用
// Dao层的保存订单额操作
public void save(Order order) {
this.getHibernateTemplate().save(order);
}
5.在Servlet中如何使用?如果是action需要注入service,调用service方法即可
在你的RegisterServlet的doPost()方法中使用service

private AdvanceRegistrationService advanceRegistrationService;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//获取ServletContext 再获取 WebApplicationContextUtils

ServletContext servletContext = this.getServletContext();

WebApplicationContext context =

WebApplicationContextUtils.getWebApplicationContext(servletContext);

advanceRegistrationService = (AdvanceRegistrationService) context.getBean("advanceRegistrationService");

    System.out.println("--------------------------");  
  //调用service的方法进行增删改查操作。
    System.out.println("--------------------------");  
}  

贴了那么多都没用,关键你先看
java.lang.NullPointerException
at com.reitgn.dao.impl.AdvancedRegistrationDaoImpl.addAdvancedRegistration(AdvancedRegistrationDaoImpl.java:34)
这34行是什么代码,是什么报空指针了,有没有可能捕获或者打印更详细的错误。这才是关键。

这个空指针的话,应该就是你没为这个**AdvanceRegistrationServiceImpl**类注入sessionFactory咯

楼上讨论了很久,也不知道怎么回事,我觉得能发布,配置文件应该就没什么问题,无法调用hibernatetemplate里的save方法