Spring Hibernate配置导致会话NPE

时间:2012-06-12 21:57:20

标签: spring hibernate maven-2

我在设置Hibernate配置时遇到问题。在尝试将HibernateDaoSupport扩展为GenericDao并将其扩展到特定于类的daos后,但是当我在我的dao中调用findByNamedQuery时,getSession()会抛出一个NPE。

当我尝试切换到扩展HibernateTemplate时,hibernateTemplate没有正确实例化,并且仍为null:

java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:143)
at com.jmt.hibernate.dao.GenericDaoImpl.findByNamedQuery(GenericDaoImpl.java)

我错过了什么?

我使用maven2来构建项目, 1.将hibernate插件添加到我的pom.xml中:

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>3.0</version>

    <configuration>
        <components>
            <component>
            <name>hb2ddl</name>
            <implementation>jpaconfiguration</implementation>
            </component>
        </components>
        <componentProperties>
        <drop>true</drop>
        <outputfilename>output.sql</outputfilename>
        <format>false</format>
        <persistenceunit>MyEntityManager</persistenceunit>
        <ejb3>true</ejb3>
        </componentProperties>

    </configuration>
</plugin>

2。在persistence.xml中定义了我的实体管理器:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

   <persistence-unit name="MyEntityManager" transaction-type="RESOURCE_LOCAL"> <!-- "JTA"> -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/DefaultDS</jta-data-source>

        <class>com.jmt.model.UserEntity</class>

      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      <validation-mode>CALLBACK</validation-mode>
      <properties>

            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/><!-- org.hibernate.dialect.HSQLDialect" -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
         <property name="hibernate.connection.url"  value="${jdbc.url}"/>
         <property name="hibernate.connection.username" value="${jdbc.username}"/>
         <property name="hibernate.connection.password" value="${jdbc.password}"/>

      </properties>
   </persistence-unit>
</persistence>

3。在applicationContext.xml中连接我的daos:

 <bean id="hibernateDaoSupport" abstract="true"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
    <bean id="hibernateHelper" class="org.springframework.orm.hibernate3.HibernateTemplate" >
        <constructor-arg ref="sessionFactory"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="hibernateManagedSession" value="true"/>
</bean>

    <bean class="com.jmt.model.UserEntity"/>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="1" />
    </bean>

    <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <property name="order" value="2" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename"><value>messages</value></property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
        <property name="logAbandoned" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="10000"/>
        <property name="minEvictableIdleTimeMillis" value="60000"/>        
    </bean>   


    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                <prop key="hibernate.jdbc.batch_size">15</prop>   
                <prop key="hibernate.connection.isolation">2</prop>
            </props>       
        </property>
         <property name="packagesToScan">
            <list>
                <value>com.jmt.model</value>
                <value>com.jmt.hibernate.dao</value>
            </list>
        </property>        
    </bean>
    <bean id="userDao" class="com.jmt.hibernate.dao.UserDaoImpl">
        <constructor-arg value="com.jmt.hibernate.dao.UserDaoImpl"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

4。甚至在我的web.xml中添加了一个过滤器,希望能正确地注入会话:

<filter>
        <filter-name>sessionLoadingFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>  

我还没有添加一个经理,希望更少的图层可以让SOMETHING工作......

非常感谢任何帮助/想法!

2 个答案:

答案 0 :(得分:0)

我认为你应该尝试做openSession()而不是做getSession()。因为在这一点上可能存在你的会话没有被打开并且你正试图获得它的情况,因此它将导致NPE。

希望这会对你有所帮助。欢呼声。

答案 1 :(得分:0)

哎呀,我没有考虑到对dao的调用在哪里,是的,这就是没有开放会话的原因。有人用spring dwr直接从jsp文件调用dao。一旦我摆脱了它并从我的控制器发出呼叫,会话终于被正确地实例化了。谢谢你的帮助,Japs!