org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'sessionFactory'的bean时出错

时间:2014-07-19 16:32:05

标签: spring hibernate maven java-ee

我正在使用maven项目和hibernate 4

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.1.9.Final</version>
    <!-- will come with Hibernate core-->
</dependency>

和运行tomcat服务器中的Spring 3.2.0.RELEASE和jsf我有这个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: 
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;

这是我的application-context.xml:

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/gestionnotes"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>  
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="fr.ensa.agadir.model"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config/>
    <context:component-scan base-package="fr.ensa.agadir"/>
</beans>

这是我的pom.xml:

  

http://maven.apache.org/maven-v4_0_0.xsd">

     

4.0.0 EnsaAgadir     EnsaGestionNotes战争     EnsaGestionNotes Maven Webapp     1.0-SNAPSHOT   4.0.0         EnsaAgadir         EnsaGestionNotes         战争         EnsaGestionNotes Maven Webapp         1.0-SNAPSHOT

  <!-- les proprietés 3.1.4 -->
  <properties>
      <logback.version>0.9.15</logback.version>
      <spring.version>3.2.0.RELEASE</spring.version>
      <slf4j.version>1.7.7</slf4j.version>
      <env>LOCAL</env>
  </properties>

  <dependencies>

      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-digester3</artifactId>
          <version>3.2</version>
      </dependency>

      <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.1.2</version>
      </dependency>

      <dependency>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils</artifactId>
          <version>1.9.2</version>
      </dependency>
      <dependency>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils-bean-collections</artifactId>
          <version>1.8.3</version>
      </dependency>
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-dbcp2</artifactId>
          <version>2.0.1</version>
      </dependency>


      <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-api</artifactId>
          <version>2.2.6</version>
          <scope>runtime</scope>
      </dependency>
      <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-impl</artifactId>
          <version>2.2.6</version>
          <scope>runtime</scope>
      </dependency>
      <!-- primafaces -->
      <dependency>
          <groupId>org.primefaces</groupId>
          <artifactId>primefaces</artifactId>
          <version>5.0</version>
      </dependency>
      <dependency>
          <groupId>org.primefaces.themes</groupId>
          <artifactId>all-themes</artifactId>
          <version>1.0.9</version>
      </dependency>

      <!-- Junit 4 -->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.0</version>
      </dependency>
      <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-entitymanager</artifactId>
           <version>4.1.9.Final</version>
           <!-- will come with Hibernate core-->
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.3.5.Final</version>
          <type>jar</type>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.hibernate.common</groupId>
          <artifactId>hibernate-commons-annotations</artifactId>
          <version>4.0.5.Final</version>
          <type>jar</type>
      </dependency>
      <dependency>
          <groupId>javax.validation</groupId>
          <artifactId>validation-api</artifactId>
          <version>1.0.0.GA</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-validator</artifactId>
          <version>5.1.1.Final</version>
      </dependency>

      <!-- Hibernate uses slf4j for logging, for our purposes here use the simple 
          backend -->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-jdk14</artifactId>
          <version>${slf4j.version}</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
      </dependency>

      <!-- Hibernate gives you a choice of bytecode providers between cglib and 
          javassist -->
      <dependency>
          <groupId>org.javassist</groupId>
          <artifactId>javassist</artifactId>
          <version>3.18.2-GA</version>
      </dependency>

      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-c3p0</artifactId>
          <version>4.3.5.Final</version>
      </dependency>
      <!-- c3p0 dependency -->
      <dependency>
          <groupId>c3p0</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.1.2</version>
      </dependency>
      <!-- spring -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc-portlet</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <!-- MySql jdbc -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.9</version>
      </dependency>
      <!-- jstl -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>


      <!-- javax.servlet -->


      <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
      </dependency>

      <!-- javax.servlet -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
          <scope>provided</scope>
      </dependency>

      <!-- upload -->
      <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
      </dependency>
      <!-- upload -->
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-web-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
      </dependency>
      <!-- Itext -->
      <dependency>
          <groupId>com.lowagie</groupId>
          <artifactId>itext</artifactId>
          <version>2.1.7</version>
      </dependency>
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.5-beta3</version>
      </dependency>

  </dependencies>

  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>1.7</source>
                  <target>1.7</target>
              </configuration>
          </plugin>
      </plugins>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>
  </build>
  <repositories>
      <repository>
          <id>jboss-repository</id>
          <name>JBoss Public Maven Repository</name>
          <url>https://repository.jboss.org/nexus/</url>
      </repository>
      <repository>
          <id>prime-repo</id>
          <name>PrimeFaces Maven Repository</name>
          <url>http://repository.primefaces.org</url>
          <layout>default</layout>
      </repository>
      <repository>
          <id>java.net2</id>
          <name>Repository hosting the jee6 artifacts</name>
          <url>http://download.java.net/maven/2</url>
      </repository>
  </repositories>
</project>

1 个答案:

答案 0 :(得分:0)

首先,尝试正确配置您的依赖项,您正在使用Hibernate 4.3.5与Hibernate-entitymanager 4.1.9 ...那看起来不太好......

使用此aproximation,然后重试。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.5.Final</version>
</dependency>

<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.5.Final</version>
</dependency>

<!-- optional -->

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-osgi</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-proxool</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-infinispan</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>4.3.5.Final</version>
</dependency>