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

时间:2018-08-10 04:17:53

标签: spring spring-boot spring-data-jpa

大家好, 我在运行项目时遇到问题

Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

我不知道如何解决它。我看到了同样的话题,他们在文件pom.xml中说了这个问题。但是我的文件pom.xml中没有看到任何错误。所以有人可以帮我吗 这是我的代码 文件pom.xml

<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-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>

    

DemoApplication

package com.example.demo;


 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication

public class DemoApplication  {

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

}

具有@Entity的模型

package com.example.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

 @Entity
 @Table(name = "contact")
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private int id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "email")
private String email;

@Column(name = "phone")
private String phone;

public Contact() {
    super();
}

public Contact(int id, String name, String email, String phone) {
    super();
    this.id = id;
    this.name = name;
    this.email = email;
    this.phone = phone;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;

}

 public String getEmail() {
    return email;
 }

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
 }

我不知道如何解决

2 个答案:

答案 0 :(得分:0)

您的pom.xml中有

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

但是您没有定义带有@Entity批注的任何类。您至少需要其中之一才能成功创建实体管理器。如果没有,请删除上面的依赖项。

答案 1 :(得分:0)

您的SpringBootApplication依赖于程序包com.example.demo;,而您的实体依赖于程序包com.example.model;。默认情况下,@SpringBootApplication将尝试查看其软件包及其下方。它无法找到您的实体,因为它们位于不同的数据包中,除非您明确指定,例如通过

@EntityScan(basePackages = "com.example.model")