Spring Data(JPA)context config XML - jpa:repositories

时间:2014-08-20 14:09:33

标签: java eclipse spring spring-data spring-data-jpa

我正在STS中创建一个Spring MVC应用程序

在我的JPA配置XML中,我在STS中突出显示以下错误

Error occured processing XML 'com/xxx/repository/ChannelProgrammeRepository : Unsupported major.minor version 51.0'. See Error Log for more details

对于

<jpa:repositories base-package="com.xxx.repository"/>

我的web.xml设置如此

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/jpaContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>xxxServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/servlet-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>xxxServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

我的jpaContext.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:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <context:annotation-config />

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

    <jpa:repositories base-package="com.xxx.repository"/>

My Maven依赖

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>1.5.10</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.31</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.6.2.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>0.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>0.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>

它抱怨的ChannelProgrammeRepository界面

package com.xxx.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.xxx.model.entity.ChannelProgramme;

public interface ChannelProgrammeRepository extends JpaRepository<ChannelProgramme, Long> {

}

快速谷歌似乎建议使用不同版本的Java编译ChannelProgrammeRepository,或者可能与我的某个Maven依赖项发生冲突?

我的项目设置为使用Java 7 build path

运行

编辑器错误

image1

image2

3 个答案:

答案 0 :(得分:1)

请检查,您的maven尝试编译代码的版本。属性部分中的内容

<properties> 
    ...
    <targetJdk>1.7</targetJdk> 
    ...
</properties>

然后再

<plugin> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>${maven-compiler-plugin.version}</version> 
    <configuration> 
        <encoding>${project.build.sourceEncoding}</encoding> 
        <source>${targetJdk}</source> 
        <target>${targetJdk}</target> 
        <testSource>${targetJdk}</testSource> 
        <testTarget>${targetJdk}</testTarget> 
    </configuration> 
</plugin> 

也许目标设置为6.0或更早。然后将调用编译器为/编译为6.0,并且可以抛出此失败。

将目标和来源设置为7.0,它应该有效......

答案 1 :(得分:0)

我认为你的jpa配置没有任何问题。这肯定是因为Eclipse或IDE中使用的编译级别不匹配。您可以尝试编译编译器合规性级别,进行清理构建。

当我们在编译时使用更高的JDK版本并在运行时使用更低的JDK版本时,通常会得到UnsupportedClassVersionError。

enter image description here

答案 2 :(得分:0)

尝试一下干净的构建,

Project -> Clean 

它会删除项目中已编译的文件,以便您可以进行全新的重建。从而摆脱了配置文件中的错误。