SessionFactory为null

时间:2013-08-13 18:49:47

标签: spring hibernate sessionfactory

我正在使用带有Hibernate的Spring并且最初设置了我的项目with a hibernate xml config,这导致了性能问题,并且看起来这样做是错误的。我现在正在尝试注入我的SessionFactory,从1 dao开始,但是在调用sessionFactory.getCurrentSession()时会得到一个空指针异常。我认为我的代码看起来像我见过的例子。我很难过。我也尝试不使用资源,而是将sessionFactory注入到应用程序上下文中的dao中。结果相同。

的applicationContext.xml

 <context:component-scan base-package="path.to.base">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingDirectoryLocations">
       <list>
           <value>classpath*:/path/to/mapping/files</value>
       </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
             <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

myDAO

@Repository
public class myDAO {
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory(){
    return sessionFactory;
}

@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public myDAO() {

}

@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Things> getAllThings() {
    return sessionFactory.getCurrentSession().createCriteria(EvalMasterEvaluationType.class)
            .add(Restrictions.eq("active", "Y")).addOrder(Order.desc("createDtTm")).list();

}

}

Spring 3.2.1,Hibernate 3.6.10

1 个答案:

答案 0 :(得分:0)

我得到了它,但我不确定哪个修改解决了这个问题。 SRT_KP可能是关于数据源的,因为我添加了一些属性(maxactive,maxidle,validationquery)。我切换到LocalSessionFactoryBean,因为我正在使用xml映射并将映射文件后缀添加到mappingLocations属性。我还将@Transactional移动到它所属的服务层。

这是我最终的结果:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<context:property-placeholder location="classpath*:WEB-INF/*.properties"/>
<context:component-scan base-package="org.base.to.scan">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>



<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="oraDataSource" />
    <property name="mappingLocations" value="classpath*:org/path/to/mapping/files/*.hbm.xml" />
</bean>

<bean id="oraDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="10" />
    <property name="maxIdle" value="5" />
    <property name="validationQuery" value="SELECT 'x' FROM dual" />
</bean>

<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

BTW精彩教程:http://www.byteslounge.com/tutorials/spring-with-hibernate-persistence-and-transactions-example