1

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

应用程序上下文.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>

我的DAO

@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();

}

}

春天 3.2.1,休眠 3.6.10

4

1 回答 1

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>

顺便说一句,很棒的教程:http ://www.byteslounge.com/tutorials/spring-with-hibernate-persistence-and-transactions-example

于 2013-08-21T17:04:26.737 回答