Hibernate在spring配置时尝试查找hibernate.cfg.xml

时间:2017-01-11 19:47:58

标签: java spring hibernate

我已经通过spring(4.3.5)配置了Hibernate(5.2.6)但是当我运行我的Junit(4.12)测试时,它发出一个错误,说它找不到hibernate.cfg.xml文件。任何人都可以帮助我吗?

运行Junit测试后,在数据库中创建表但没有添加数据,我可以看到正在运行的sql查询,因为我启用了show_sql

enter image description here

2 个答案:

答案 0 :(得分:0)

你在web.xml中指定了hibernate配置的xml文件名吗? 如果没有在web.xml中指定xml文件的名称

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:hibernateContext.xml</param-value>
</context-param>

答案 1 :(得分:0)

如何为spring和hibernate集成配置applicationContext?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:properties/jdbc.properties</value>
        </property>
    </bean>

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

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="configLocation">    
          <value>classpath:hibernate/hibernate.cfg.xml</value>
        </property>     
    </bean>