创建名称为'jpaMappingContext'的bean时出错:调用init方法失败; :必须存在至少一个JPA元模型

时间:2018-07-25 06:05:15

标签: spring-boot spring-data

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

公用接口EmployeeInterface扩展了CrudRepository { }


@RestController
public class EmployeeController {

    @Autowired
    EmployeeInterface empInterface;

    @RequestMapping("/getAll")
    public List<Employee> getEmployees() {
        return (List<Employee>)empInterface.findAll();
    }
}

具有getter和setter的员工实体

@Entity
@Table(name="Employee")
public class Employee implements Serializable {

    private static final long serialVersionUID = 5406213393320888178L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private Long id;
    @Column(name="fname")
    private String fname;
    @Column(name="lname")
    private String lname;

执行创建的jar时

java -Djava.security.egd = file:/ dev /./ urandom -Dspring.profiles.active = container -jar demo.jar

我看到此错误

由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为'jpaMappingContext'的bean时出错:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:必须至少存在一个JPA元模型!

3 个答案:

答案 0 :(得分:0)

使用此依赖项:

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

您使用的是Spring Boot 1.5.6版,但提供了Spring Boot 2.0.3的jpa依赖项

更改您的mysql连接器版本:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>

答案 1 :(得分:0)

查看您的POM条目:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>

用于弹簧引导的版本为1.5和2.0。 Spring boot启动程序在您的情况下使用1.5.6,而启动程序jpa使用2.0,这在兼容性方面有很大不同。始终使用相同的版本。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>

以上内容应解决JPA bean创建的问题。

答案 2 :(得分:0)

我遇到了同样的错误,我试图连接到一个不存在的模式。必须从application.properties文件中删除架构名称。然后它起作用了。