首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Hibernate: org.hibernate.MappingNotFoundException

Hibernate: org.hibernate.MappingNotFoundException
EN

Stack Overflow用户
提问于 2015-04-15 11:28:27
回答 4查看 1.3K关注 0票数 0

我的问题是:

现在我的-cfg.xml看起来像这个 SPRING-CFG.xml

代码语言:javascript
运行
复制
<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>

例外:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
<?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>

我的项目结构:

能理解问题是什么。有时,我也会看到一些异常,比如ava.lang.NoClassDefFoundError:[Lorg/hibernate/engine/FilterDefinition“。这些都与这个问题有关吗?有什么问题吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-04-15 11:44:59

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

我强烈建议删除您的hibernate.cfg.xml,并将所有内容移到您的spring配置中。

代码语言:javascript
运行
复制
<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属性是无用的,因为注入了DataSourcecurrent_session_context篡改会破坏正确的tx管理。有了上面的配置,您可以删除hibernate.cfg.xml文件。

票数 0
EN

Stack Overflow用户

发布于 2015-04-15 12:47:47

根据文件

代码语言:javascript
运行
复制
 Set Hibernate mapping resources to be found in the class path,
 like "example.hbm.xml" or "mypackage/example.hbm.xml".

因此,您不能使用

代码语言:javascript
运行
复制
<property name="mappingResources" value="/WEB-INF/hibernateConf/App1User.hbm.xml" />

您应该使用mappingLocations,path不应该以/WEB开头,它应该是相对路径。

代码语言:javascript
运行
复制
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/class目录,这个目录是类路径,而use可以使用类路径引用所有文件。与上面的方法相比,这是一种很好的更干净的方法。

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

代码语言:javascript
运行
复制
 <property name="mappingLocations" value="classpath:hibernateConf/App1User.hbm.xml" />
票数 1
EN

Stack Overflow用户

发布于 2015-04-15 11:38:22

如果您已经在spring文件中定义了类路径,那么您应该使用您的目录,因为如果您已经在directory....so文件中定义了类路径,那么您不需要在hibernate way....Also文件中映射它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29649020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档