IllegalArgumentException:必须至少存在一个JPA元模型

时间:2016-11-22 10:01:05

标签: java spring maven jpa

从春季休息开始,我得到了以下错误

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为' jpaMappingContext的bean':调用init方法   失败;嵌套异常是java.lang.IllegalArgumentException:At   必须至少有一个JPA元模型!         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1595)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:566)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)   〜[弹簧上下文5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)   〜[弹簧上下文5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)   〜[弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:736)   [弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)   [弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在org.springframework.boot.SpringApplication.run(SpringApplication.java:313)   [弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在org.springframework.boot.SpringApplication.run(SpringApplication.java:1161)   [弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在org.springframework.boot.SpringApplication.run(SpringApplication.java:1150)   [弹簧引导2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]         在com.java.app.QuickPollApplication.main(QuickPollApplication.java:14)   [类/:NA]       引起:java.lang.IllegalArgumentException:必须至少存在一个JPA元模型!         在org.springframework.util.Assert.notEmpty(Assert.java:438)〜[spring-core-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.data.jpa.mapping.JpaMetamodelMappingContext。(JpaMetamodelMappingContext.java:52)   〜[弹簧 - 数据 - JP-A-1.11.0.BUILD-SNAPSHOT.jar:NA]         在org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:71)   〜[弹簧 - 数据 - JP-A-1.11.0.BUILD-SNAPSHOT.jar:NA]         在org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)   〜[弹簧 - 数据 - JP-A-1.11.0.BUILD-SNAPSHOT.jar:NA]         在org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1654)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1591)   〜[弹簧豆-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]         ...省略了16个常见帧

以下代码

@SpringBootApplication
@RestController
public class QuickPollApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuickPollApplication.class, args);
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String testRest() {
        return "rest tested";
    }

以下是我的pom.xml

            UTF-8             UTF-8             1.8         

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

请帮帮我。我不知道如何解决这个错误

4 个答案:

答案 0 :(得分:24)

您已添加

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

在你的pom.xml中。

Spring启动会尝试为JPA自动创建实体工厂,但是您没有定义任何有关JPA模型的内容。

尝试删除它以测试到目前为止你做了什么。

之后,您可以使用spring-data-starter-jpa查看教程,例如this guy

答案 1 :(得分:8)

我已经通过设置较新版本的Hibernate修复了它。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
         <exclusions>
             <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

你可以在这里找到一个完整的例子: https://github.com/zobarov/ptc-task-executor

答案 2 :(得分:2)

您还可以通过排除以下自动配置类(例如,在@EnableAutoConfiguration@SpringBootApplication批注中)来禁用Spring Data JPA的存储库支持

org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration

例如:

@EnableAutoConfiguration(exclude = JpaRepositoriesAutoConfiguration.class)

答案 3 :(得分:0)

这对我有用。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
     <exclusions>
         <exclusion>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.10.Final</version>
</dependency>