spring - org.springframework.beans.factory.BeanCreationException 创建 ServletContext 中定义的名称为 'sessionFactory' 的 bean 时出错

标签 spring hibernate jakarta-ee spring-mvc hibernate-mapping

我尝试部署应用程序,但控制台显示此错误,我不熟悉 Spring MVC 和 Hibernate 3 这是我的第一个项目

 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 org.hibernate.MappingException: component class not found: com.GestionEtudiant.model.EtudiantId
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
        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:4701)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.hibernate.MappingException: component class not found: com.GestionEtudiant.model.EtudiantId
        at org.hibernate.mapping.Component.getComponentClass(Component.java:104)
        at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:133)
        at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:43)
        at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:38)
        at org.hibernate.tuple.component.ComponentEntityModeToTuplizerMapping.<init>(ComponentEntityModeToTuplizerMapping.java:52)
        at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:50)
        at org.hibernate.mapping.Component.buildType(Component.java:152)
        at org.hibernate.mapping.Component.getType(Component.java:145)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:193)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
        ... 19 more
    Caused by: java.lang.ClassNotFoundException: com.GestionEtudiant.model.EtudiantId
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
        at org.hibernate.mapping.Component.getComponentClass(Component.java:101)
        ... 35 more

application-context.xml 代码
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/GestionEtudiant/model/Etudiant.hbm.xml</value>
                <value>com/GestionEtudiant/model/Commentaire.hbm.xml</value>
                <value>com/GestionEtudiant/model/Ensiegnant.hbm.xml</value>
                <value>com/GestionEtudiant/model/Person.hbm.xml</value>
                <value>com/GestionEtudiant/model/Compte.hbm.xml</value>
                <value>com/GestionEtudiant/model/Groupe.hbm.xml</value>
                <value>com/GestionEtudiant/model/Fiche.hbm.xml</value>
                <value>com/GestionEtudiant/model/Sujet.hbm.xml</value>
                <value>com/GestionEtudiant/model/Document.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Service -->
    <bean id="compteService"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target">
            <bean class="com.GestionEtudiant.service.CompteServiceImp">
                <property name="compteDAO" ref="compteDAO" />
            </bean>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <!-- DAO -->
    <bean id="compteDAO" class="com.GestionEtudiant.dao.CompteDAOHib"
        p:sessionFactory="sessionFactory" p:sessionFactory-ref="sessionFactory" />

</beans>

Dispatcher-servlet.xml 代码是:
<?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-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- Action Handling -->
    <!-- BeanNameUrlHandlerMapping This configuration is optional because BeanNameUrlHandlerMapping 
        is the default handler. In case no handler mapping is found in the web application 
        context, SpringMVC will create an instance of BeanNameUrlHandlerMapping. -->
    <bean name="beanNameUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <!-- SimpleUrlHandlerMapping The BeanNameUrlHandlerMapping class does not 
        support wildcards to resolve a request URL to a bean name. Configuration 
        can be simplified with Apache Ant–style wildcard path mapping with the SimpleUrlHandlerMapping. 
        <bean name="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
        <property name="mappings"> <props> <prop key="/*.html"></prop> </props> </property> 
        </bean> -->
    <!-- ControllerClassNameHandlerMapping <bean name="controllerClassNameHandlerMapping" 
        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> -->
    <!-- end of Action Handling -->


    <!-- View Handler -->
    <!-- InternalResourceViewResolver The InternalResourceViewResolver class 
        can determine the physical view in the web application archive given the 
        logical view name. -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    <!-- ResourceBundleViewResolver It allows the logical view name to physical 
        resource mapping to be configured in externalized properties or resource 
        bundle files. <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
        <property name="basename" value=""/> </bean> -->
    <!-- XmlViewResolver To use an XML-based view resolver, configuration information 
        should be moved in XML files from the properties file. This view configuration 
        file should be located in the WEB-INF folder and is called views.xml by default. 
        <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"/> -->
    <!-- end of View Handler -->


    <!-- Page Controller -->

    <bean name="/Comptes.html" class="com.affectation.controller.CompteController"
        p:compteService="compteService" p:compteService-ref="compteService" />
<!-- end of Page Controller -->


    <!-- <bean id="springUtilInitializer" class="com.affectation.controller.SpringApplicationContextHolder" 
        lazy-init="false" /> -->
</beans>

web.xml 代码是:
<?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-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- Action Handling -->
    <!-- BeanNameUrlHandlerMapping This configuration is optional because BeanNameUrlHandlerMapping 
        is the default handler. In case no handler mapping is found in the web application 
        context, SpringMVC will create an instance of BeanNameUrlHandlerMapping. -->
    <bean name="beanNameUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <!-- SimpleUrlHandlerMapping The BeanNameUrlHandlerMapping class does not 
        support wildcards to resolve a request URL to a bean name. Configuration 
        can be simplified with Apache Ant–style wildcard path mapping with the SimpleUrlHandlerMapping. 
        <bean name="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
        <property name="mappings"> <props> <prop key="/*.html"></prop> </props> </property> 
        </bean> -->
    <!-- ControllerClassNameHandlerMapping <bean name="controllerClassNameHandlerMapping" 
        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> -->
    <!-- end of Action Handling -->


    <!-- View Handler -->
    <!-- InternalResourceViewResolver The InternalResourceViewResolver class 
        can determine the physical view in the web application archive given the 
        logical view name. -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    <!-- ResourceBundleViewResolver It allows the logical view name to physical 
        resource mapping to be configured in externalized properties or resource 
        bundle files. <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
        <property name="basename" value=""/> </bean> -->
    <!-- XmlViewResolver To use an XML-based view resolver, configuration information 
        should be moved in XML files from the properties file. This view configuration 
        file should be located in the WEB-INF folder and is called views.xml by default. 
        <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"/> -->
    <!-- end of View Handler -->


    <!-- Page Controller -->

    <bean name="/Comptes.html" class="com.affectation.controller.CompteController"
        p:compteService="compteService" p:compteService-ref="compteService" />
<!-- end of Page Controller -->


    <!-- <bean id="springUtilInitializer" class="com.affectation.controller.SpringApplicationContextHolder" 
        lazy-init="false" /> -->
</beans>

EtudiantId 类代码是:
package com.GestionEtudiant.model;


    public class EtudiantId implements java.io.Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private int idPersonne;
        private int idEtudiant;

        public EtudiantId() {
        }

        public EtudiantId(int idPersonne, int idEtudiant) {
            this.idPersonne = idPersonne;
            this.idEtudiant = idEtudiant;
        }

        public int getIdPersonne() {
            return this.idPersonne;
        }

        public void setIdPersonne(int idPersonne) {
            this.idPersonne = idPersonne;
        }

        public int getIdEtudiant() {
            return this.idEtudiant;
        }

        public void setIdEtudiant(int idEtudiant) {
            this.idEtudiant = idEtudiant;
        }

        public boolean equals(Object other) {
            if ((this == other))
                return true;
            if ((other == null))
                return false;
            if (!(other instanceof EtudiantId))
                return false;
            EtudiantId castOther = (EtudiantId) other;

            return (this.getIdPersonne() == castOther.getIdPersonne())
                    && (this.getIdEtudiant() == castOther.getIdEtudiant());
        }

        public int hashCode() {
            int result = 17;

            result = 37 * result + this.getIdPersonne();
            result = 37 * result + this.getIdEtudiant();
            return result;
        }

    }

最佳答案

如果您在 EtudiantId.hbm.xml 中定义了 com.GestionEtudiant.model.EtudiantId 的映射
也将其添加到“mappingResources”中

 <property name="mappingResources">
        <list>
          ...
           <value>com/GestionEtudiant/model/EtudiantId.hbm.xml</value>
          ...

更新:-

如果您的自动生成类使用注释,请添加 packagesToScan属性如下,
<bean id="sessionFactory"
     <property name="packagesToScan" value="com.GestionEtudiant.model" />

关于spring - org.springframework.beans.factory.BeanCreationException 创建 ServletContext 中定义的名称为 'sessionFactory' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497490/

相关文章:

java - Spring - Maven |贾斯珀依赖

java - 如何在 spring 集成中使用聚合器对事件进行分组/批处理

java - 为什么我的 Controller 没有与我的存储库连接

java - 避免 hibernate 惰性初始化异常

java - Hibernate 中稍微复杂的 ManyToMany 关系

java - 消息驱动bean可以从主机队列中拾取消息吗?

java - 从 JSON 对象中提取数组

java - Spring data mongodb搜索ISO日期

java - 在 oc4j 10.1.3 中使用 log4j

实体字段中的 Java 8 可选