Hibernate:org.hibernate.MappingNotFoundException

时间:2015-04-15 11:28:28

标签: java spring hibernate

我的问题是:

NOW MY SPRING-CFG.xml看起来像这样 弹簧cfg.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

        <bean id="dataSource" 
            class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:driverClassName="org.h2.Driver" 
            p:url="jdbc:h2:~/test"
            p:username="sa" 
            p:password="sa"/>


        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources" value="/WEB-INF/hibernateConf/App1User.hbm.xml" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.default_schema">PUBLIC</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.cache.use_second_level_cache">false</prop>
                </props>
            </property>


        </bean>

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


        <bean id="userManagerDAOImpl" class="app1.persistance.UserManageDAOImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>




        <bean id="userManagerServiceImpl" class="app1.service.UserManageServiceImpl">
            <property name="userManageDAOIF" ref="userManagerDAOImpl"/>
        </bean>

        <import resource="spring-security.xml"/>

</beans>

例外:

WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springConf/spring-cfg.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2257)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2229)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2209)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2113)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:348)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 25 more

апр 15, 2015 3:21:52 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springConf/spring-cfg.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2257)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2229)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2209)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2113)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:348)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 25 more

апр 15, 2015 3:21:52 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springConf/spring-cfg.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernateConf/App1User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2257)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2229)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2209)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2113)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:348)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 25 more

我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

   <session-factory>

       <property name="hibernate.current_session_context_class">thread</property> 

        <property name="hibernate.default_schema">PUBLIC</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <property name="hibernate.transaction.auto_close_session">false</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="/WEB-INF/hibernateConf/App1User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

我的项目结果:

Structure

可以了解问题所在。 而且有时我也看到像ava.lang.NoClassDefFoundError这样的异常:[Lorg / hibernate / engine / FilterDefinition。这都与这个问题有关吗? 有什么问题?

4 个答案:

答案 0 :(得分:1)

根据文件

 Set Hibernate mapping resources to be found in the class path,
 like "example.hbm.xml" or "mypackage/example.hbm.xml".

因此你不能使用

<property name="mappingResources" value="/WEB-INF/hibernateConf/App1User.hbm.xml" />

你应该使用mappingLocations,路径不应该以/ WEB-INF开头它应该是相对路径。

Set locations of Hibernate mapping files, for example as classpath
resource "classpath:example.hbm.xml". Supports any resource location
via Spring's resource abstraction, for example relative paths like
"WEB-INF/mappings/example.hbm.xml" when running in an application context.

因此,您的属性应该是mappingLocations,其值为相对路径,如下所示。      

但是,我建议您将所有配置移动到src / main / resources目录。在战争生成期间,maven插件会将此src / main / resources目录复制到/ WEB-INF / classes目录,这是类路径,使用可以使用class-path引用所有文件。与上述方法相比,这是一种更好,更清洁的方法。

例如,您可以将App1User.hbm.xml放在src/main/resources/hibernatConf目录中。在战争生成期间,文件将被复制到{context path}/WEB-INF/classes/hibernateConf/App1User.hbm.xm l,您可以参考如下所示。

 <property name="mappingLocations" value="classpath:hibernateConf/App1User.hbm.xml" />

答案 1 :(得分:0)

Bu默认spring试图找到带有class-path作为父目录的路径....所以你应该以这种方式使用你的目录....如果你已经定义的话你也不需要在hibernate cfg文件中映射它在春天的cfg文件中。

答案 2 :(得分:0)

您已在Spring和hibernate配置文件中配置了映射资源。此外,您当前的配置会破坏Spring的正确事务管理。

我强烈建议您删除hibernate.cfg.xml并将所有内容移至弹簧配置中。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources" value="/WEB-INF/hibernateConf/App1User.hbm.xml" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema">PUBLIC</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
        </props>
    </property>
</bean>

配置中的transactionconnection属性无效,因为注入DataSource current_session_context摆弄会破坏正确的tx管理。使用上述配置,您可以删除hibernate.cfg.xml文件。

答案 3 :(得分:0)

中的

<property name="mappingResources" value="/WEB-INF/hibernateConf/App1User.hbm.xml" />

试试这个:

<property name="mappingResources" value="classpath:/WEB-INF/hibernateConf/App1User.hbm.xml" />