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

时间:2014-05-06 14:25:48

标签: spring hibernate java-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;
        }

    }

3 个答案:

答案 0 :(得分:1)

如果已在EtudiantId.hbm.xml中定义了com.GestionEtudiant.model.EtudiantId的映射 将其添加到&#34; mappingResources&#34;像这样

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

<强>更新: -

如果您的自动生成的类使用注释,请添加packagesToScan属性,如下所示

<bean id="sessionFactory"
     <property name="packagesToScan" value="com.GestionEtudiant.model" />

答案 1 :(得分:1)

尝试清理你的日食。它对我有用。

GoTo cmd并执行 eclipse -clean

答案 2 :(得分:0)

com.GestionEtudiant.model.EtudiantId需要位于类路径中。它是否部署在war文件中?