2014-05-06 112 views
4

我嘗試部署應用程序,但控制檯向我顯示這個錯誤,我不熟悉Spring MVC和Hibernate 3 this是我的第一個項目org.springframework.beans.factory.BeanCreationException在ServletContext中定義名稱爲'sessionFactory'的bean時出錯

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 

應用程序的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> 

調度-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; 
     } 

    } 
+0

看:這是一個標準的類路徑問題。用你的XML配置仔細檢查你的類名;他們區分大小寫。 – chrylis

+0

EtudiantId是使用休眠工具從數據庫自動生成的 –

回答

0

com.GestionEtudiant.model.EtudiantId需要在類路徑。它是否部署在戰爭文件中?

+0

它是! http://hpics.li/a0c4fb0 –

1

如果您在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" /> 
+0

是的,但我沒有EtudiantId.hbm.xml,因爲它適用於人與etudiant類之間的關係。 –

+0

問題在於這種缺失關係。自動生成的EtudiantId.class是否使用了轉位?你可以發佈該課程嗎? – Jay

+0

也許我得到了另一個問題是協會引用未映射類:com.GestionEtudiant.model.User –

1

試着清洗你的eclipse.It爲我工作。

轉到cmd並在該堆棧跟蹤的最終原因執行日食-clean

+0

也爲我工作。謝謝@sagar – Moitt

相關問題