如何修复“加载应用程序上下文失败,至少必须存在 1 个 JPA 元模型”

Posted

技术标签:

【中文标题】如何修复“加载应用程序上下文失败,至少必须存在 1 个 JPA 元模型”【英文标题】:how to fix "Failed to Load Application Context at least 1 JPA metamodel must be present" 【发布时间】:2019-06-13 18:47:18 【问题描述】:

我是 springboot 新手,我正在尝试解决以下运行时错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

任何帮助将不胜感激

这是我的 pom:

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.rosesnconcrete.services</groupId>
<artifactId>sixersfacts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sixersfacts</name>
<description>restful service for sixers facts</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</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.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

这是我的 app.properties:

spring.data.url = jdbc:mysql://localhost:3306/sixersfacts_app?useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true 
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MYSQL5Dialect
spring.jpa.hibernate.ddl-auto = update
spring.autoconfigure.exclude = 
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

这是我的模型:

package io.rosesnconcrete.services.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "facts")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = "createdAt", "updatedAt", allowGetters = 
true)
public class SixersFact implements Serializable 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank
private String title;

@NotBlank
private String content;

@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;

@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;

public Long getId() 
    return id;


public void setId(Long id) 
    this.id = id;


public String getTitle() 
    return title;


public void setTitle(String title) 
    this.title = title;


public String getContent() 
    return content;


public void setContent(String content) 
    this.content = content;


public Date getCreatedAt() 
    return createdAt;


public void setCreatedAt(Date createdAt) 
    this.createdAt = createdAt;


public Date getUpdatedAt() 
    return updatedAt;


public void setUpdatedAt(Date updatedAt) 
    this.updatedAt = updatedAt;


@Override
public String toString() 
    return "SixersFact" +
            "id=" + id +
            ", title='" + title + '\'' +
            ", content='" + content + '\'' +
            ", createdAt=" + createdAt +
            ", updatedAt=" + updatedAt +
            '';

这里是回购:

package io.rosesnconcrete.services.repository;

    import io.rosesnconcrete.services.model.SixersFact;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;

@Repository
public interface SixersFactRepository extends 
JpaRepository<SixersFact,Long> 

这是应用程序:

package io.rosesnconcrete.services.sixersfacts;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class SixersfactsApplication 

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

【问题讨论】:

为什么需要排除entitymanager依赖? @code_Baldy 你能把你的 SixersfactsApplication 移到 o.rosesnconcrete.services 包下并运行测试吗? 【参考方案1】:

@SpringBootApplication 仅扫描当前(和子)包(see1),因此io.rosesnconcrete.services.modelio.rosesnconcrete.services.repository 都不会被(spring 的)“组件扫描”拾取。并且在类路径中使用spring-boot-starter-data-jpa并且没有@Entityies(在春季上下文中)会导致此错误消息。 (see2)

快速且“按设计”的解决方案:

SixersfactsApplication 移动到io.rosesnconcrete.services 包(.. 甚至更高)。

【讨论】:

以上是关于如何修复“加载应用程序上下文失败,至少必须存在 1 个 JPA 元模型”的主要内容,如果未能解决你的问题,请参考以下文章

如何修复drv?

如何修复漏洞

如何修复WMI

PHP网站漏洞怎么修复 如何修补网站程序代码漏洞

如何修复这些漏洞? (npm audit fix 无法修复这些漏洞)

如何修复AppScan漏洞