新建springboot项目时,entityManagerFactory报错的解决

  • Post category:http

针对“新建springboot项目时,entityManagerFactory报错的解决”,我可以提供以下的攻略过程:

1. 确认错误信息

在解决任何错误之前,我们需要首先确定错误信息,才能更好地定位问题。在新建springboot项目时,entityManagerFactory报错可能有很多种错误信息,比如说:

No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
Cannot construct instance of `javax.persistence.EntityManagerFactory` (no Creators, like default construct, exist)

根据错误信息,我们可以知道问题出在javax.persistence.EntityManagerFactory这个类上,我们需要确认该类是否能够正确被注入。

2. 检查配置文件

在确定了错误信息之后,我们需要检查springboot项目的配置文件是否正确配置。通常,引发entityManagerFactory报错的问题通常出在application.properties或application.yml文件中,这些文件通常包含有关数据库连接信息、hibernate配置等等。

关于数据库连接的配置信息,我们可以查看官方文档,比如:

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

需要注意的是,如果我们是在properties文件中配置的话,需要使用spring.jpa.hibernate.ddl-auto的属性来定义如何创建表格,常见的属性有create和update两种,如果使用create的话,会在应用启动时删除旧表并重新创建表,使用update的话,会在应用启动时根据Entity信息修改表结构(适用于数据表数据量不大的情况)

3. 检查依赖

除了配置文件之外,我们还需要检查应用程序的依赖关系。很多时候,实体管理工厂错误通常是由于依赖不正确引起的。

我们可以使用maven进行依赖冲突解决,其中常见的依赖互斥有两种方式,一种是去除项目中已有的互斥依赖,以Hibernate为例,可能会存在多个依赖:

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

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

我们可以去除其中的某一个依赖,以此达到冲突解决的目的。

另外一种方式是通过排除冲突模块的子依赖项,避免依赖冲突。以Hibernate为例,可以使用以下方式排除依赖子项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

4. 示例说明

为了更好地理解和应用以上方法,我提供以下两个示例:

示例1:找不到EntityManagerFactory

错误信息:

Description:

Field customerRepository in com.example.demo.controller.CustomerController required a bean of type 'com.example.demo.repository.CustomerRepository' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

解决方案:

    1. 检查application.properties是否设置正确。如果是使用H2数据库,需要配置正确,例如:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
    1. 检查pom.xml是否有Hibernate JPA依赖。由于这是一个Spring Boot项目,可以使用以下依赖关系:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
     <version>2.5.4</version>
</dependency>
    1. 确保您的实体类设置了适当的JPA注解,例如:
@Entity
@Table(name="customer")
public class Customer {
  @Id
  @GeneratedValue
  private Long id;
  private String firstName;
  private String lastName;

  // getters and setters
}

示例2:无法构造EntityManagerFactory

错误信息:

Description:

Field customerRepository in com.example.demo.controller.CustomerController required a bean of type 'com.example.demo.repository.CustomerRepository' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

解决方案:

    1. 检查application.properties是否正确。更改DataSource属性或database.properties。
spring.datasource.url = jdbc:mysql://localhost:3306/coon_blog?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 1234

spring.jpa.database-platform = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
    1. 检查pom.xml文件。在这里,检查以下依赖项:
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    1. 其他配置,比如:
spring:
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        show_sql: true
        format_sql: true
        use_generated_keys: true

以上为对“新建springboot项目时,entityManagerFactory报错的解决”的详细讲解,希望对你有所帮助。