找不到配置的本地DataSource - 必须在LocalSessionFactoryBean上设置'dataSource'属性

时间:2012-06-01 12:30:49

标签: java spring hibernate

我使用的是Spring 3.0和Hibernate 3.6,数据源定义为

<bean id="test-pool"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="url"
                value="jdbc:sqlserver://Machine:1433;databaseName=TEST;maxPoolSize=100;
                minPoolSize=5;acquireIncrement=5;checkoutTimeout=5000;maxStatements=100;idleConnectionTestPeriod=3000" />
            <property name="username" value="${user}" />
            <property name="password" value="${database.password}"/>

    </bean>

带有数据源的会话工厂

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="test-pool"/>
        <property name="configLocation" value="WEB-INF/classes/test.hibernate.cfg.xml"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
            </props>
        </property>
    </bean>

在工厂类中:

LocalSessionFactoryBean localfactorybean = (LocalSessionFactoryBean)AppContext.getBean("&sessionFactory");
        Configuration configuration = localfactorybean.getConfiguration();
        SessionFactory sessionFactory = configuration.buildSessionFactory();

但我得到了以下异常

org.hibernate.HibernateException: No local DataSource found for configuration - 'dataSource' property must be set on LocalSessionFactoryBean
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.configure(LocalDataSourceConnectionProvider.java:49)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)

我目前的代码出了什么问题?

如果我再次明确地获取SessionFactory,我就摆脱了这个异常:

SessionFactory sessionFactory = (SessionFactory) AppContext.getBean("sessionFactory");

你可以看到它不整洁,我想摆脱它。

1 个答案:

答案 0 :(得分:0)

您使用的是什么版本的Hibernate? This article表明3.2.1存在问题,其症状与您相同。也许是时候更新你的版本 - hibernate现在是4.1.4