我在正确设置 Hibernate 配置时遇到问题。在尝试将 HibernateDaoSupport 扩展到 GenericDao 并将其扩展到特定于类的 daos 之后,但是当我在我的 dao 中调用 findByNamedQuery 时,getSession() 会引发 NPE。
当我尝试切换到扩展 HibernateTemplate 时,hibernateTemplate 没有正确实例化,并且仍然为空:
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>
我还没有添加管理器,希望更少的层数可以让一些东西工作......
任何帮助/想法将不胜感激!