Hibernate with Springs .hbm.xml filenotfoundException

时间:2016-10-06 12:04:22

标签: java spring hibernate

我正在使用netbeans IDE,我正在将hibernate与spring集成。运行程序时出现以下异常:

Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [Employee.hbm.xml] cannot be opened because it does not exist

这是我的 applicationContext xml 文件:

 <?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">      
        <property name="driverClassName"  value="org.apache.derby.jdbc.ClientDriver"></property>  
        <property name="url" value="jdbc:derby://localhost:1527/sample"></property>  
        <property name="username" value="app"></property>  
        <property name="password" value="app"></property>      
    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>  

        <property name="mappingResources">
            <list>
                <value>Employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.show_sql">true</prop>  

            </props>  
        </property>  

    </bean>

    <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>  



    <bean class="com.office.businessLayer.Security" id="security">
    </bean>


    <bean id="dao" class="com.office.dao.EmployeeDao">
        <property name="template" ref="template"/>
    </bean>

</beans>

我在互联网上尝试了所有可能的解决方案,但例外仍然存在。有人可以帮忙吗?

这是项目目录:

Directory

2 个答案:

答案 0 :(得分:0)

如果您的employee.hbm.xml位于应用程序的根目录下,那么您的映射资源应该是这样的。

<property name="mappingResources">
    <list>
        <value>classpath:Employee.hbm.xml</value>
    </list>
</property>

答案 1 :(得分:0)

将您的Employee.hbm.xml文件放在Employee.java所在的同一个包中,然后保留mappingResources配置(<value>Employee.hbm.xml</value>)。