2014-02-09 174 views
0

我在eclipse中使用hibernate配置我的spring mvc項目(使用maven)時遇到問題。我有這樣的錯誤:spring mvc hibernate - configuration

Build path is incomplete. Cannot find class file for org/hibernate/TransactionException

這是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>myapp</artifactId> 
    <name>MVC</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.6</java-version> 
     <org.springframework-version>4.0.1.RELEASE</org.springframework-version> 
     <hibernate.version>4.2.2.Final</hibernate.version> 
     <org.aspectj-version>1.6.10</org.aspectj-version> 
     <org.slf4j-version>1.6.6</org.slf4j-version> 
     <jackson.version>1.9.10</jackson.version> 
    </properties> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${org.springframework-version}</version> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
       <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <!-- 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> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- Jackson JSON Mapper --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>${jackson.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.25</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>3.1.2.RELEASE</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

,這是我的servlet-context.xml中:

<?xml version="1.0" encoding="UTF-8"?> 

<beans:beans 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.mycompany.myapp" /> 

    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <beans:property name="driverClassName" value="${jdbc.driverClassName}"/> 
     <beans:property name="url" value="${jdbc.url}"/> 
     <beans:property name="username" value="${jdbc.username}"/> 
     <beans:property name="password" value="${jdbc.password}"/> 
    </beans:bean> 

<!-- Hibernate Session Factory --> 
<beans:bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="mySessionFactory"> 
    <beans:property name="dataSource" ref="myDataSource" /> 
    <beans:property name="packagesToScan"> 
    <beans:array> 
    <beans:value>org.ananth.spring</beans:value> 
    </beans:array> 
    </beans:property> 
    <beans:property name="hibernateProperties"> 
    <beans:value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</beans:value> 
    </beans:property> 
</beans:bean> 

<!-- Hibernate Transaction Manager --> 
<beans:bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager"> 
    <beans:property name="sessionFactory" ref="mySessionFactory" /> 
</beans:bean> 
<!-- Activates annotation based transaction management --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

</beans:beans> 

錯誤是通過定義豆引起的for org.springframework.orm.hibernate4.HibernateTransactionManager

我是什麼做錯了?即時試圖解決這個問題幾個小時。

好吧,我想通了。我使用的是最新的spring版本(4.0.1),但不是最新的hibernate版本(4.2.2而不是4.3.1)。當我切換到休眠4.3.1它的工作

+1

爲什麼使用舊版本的spring-tx(3.1.2.RELEASE)? – Nitek

回答

0

Hibernate的會話工廠bean定義你使用myDataSource參考:

<beans:property name="dataSource" ref="myDataSource" /> 

但你的數據源定義被命名爲dataSource

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <beans:property name="driverClassName" value="${jdbc.driverClassName}"/> 
     <beans:property name="url" value="${jdbc.url}"/> 
     <beans:property name="username" value="${jdbc.username}"/> 
     <beans:property name="password" value="${jdbc.password}"/> 
    </beans:bean> 

是這是一個錯字?

此外,您應該在應用程序上下文中而不是前端控制器(調度程序)中定義您的bean。

+0

啊,是的,它是錯字,但問題是這條線: - 它給出了這個錯誤:找不到類文件org/hibernate/TransactionException – Phomerus

+0

請按照@Nitek的說法,嘗試將spring-tx更新爲其餘彈簧庫的相同版本 –

相關問題