Spring Boot 如何修复“错误创建bean与名称'entityManagerFactory'定义在类路径(...):UUID

vojdkbi0  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(69)

我在运行Springboot应用程序时遇到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: UUID

字符串
我试图解决这个问题,但我只找到描述类似问题的页面,但用“调用init方法失败”而不是“UUID”。
出现“创建名为”entityManagerFactory“的Bean时出错”错误消息时,无法找到“UUID”,错误消息为:/
chatGPT告诉我:“您遇到的org.springframework.beans.factory.BeanCreationException中包含消息“创建名为”entityManagerFactory“的Bean时出错”,通常表示entityManagerFactory Bean的配置存在问题,该Bean负责管理Sping Boot 应用程序中的JPA(Java持久性API)实体管理器。
该错误消息包括对Hibernate JpaConfiguration.class的引用和术语“UUID”。这表明您正在使用的库的版本可能不匹配,尤其是与Hibernate相关的版本。
这很有道理,不过我已经仔细检查了maven仓库的所有兼容性,看起来我是最新的。下面是我的pom.xml:

<?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 https://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>3.1.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.globomatics</groupId>
    <artifactId>bike</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bike</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring.boot.version>3.1.3</spring.boot.version>
    </properties>
    <dependencies>
        <!-- Other dependencies -->

        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- Spring Boot Starter Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- SQLite Driver -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.42.0.0</version>
        </dependency>

        <!-- Hibernate-->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.7.Final</version>
        </dependency>

        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!-- Spring Boot Starter Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


此外,我无法理解“创建名为”entityManagerFactory“的bean时出错”,因为我使用的是spring数据jpa仓库,在这种情况下,我不必配置entityManagerFactory或数据源。
这里也是我的application.properties

spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true

spring.datasource.url=jdbc:sqlite:bike.db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.sqlite.JDBC


另外,我的注解中也有一些信息应该是正确的:

@SpringBootApplication
@EntityScan("com.globomatics.bike.models")
@EnableJpaRepositories("com.globomatics.bike.repositories")
public class BikeApplication {
    public static void main(String[] args) {
        SpringApplication.run(BikeApplication.class, args);
    }
}

x

@RestController
@RequestMapping("/api/v1/bikes")
public class BikeController {

    private final BikeRepository bikeRepository;

    @Autowired
    public BikeController(BikeRepository bikeRepository) {
        this.bikeRepository = bikeRepository;
    }

    @GetMapping
    public List<Bike> list() {
        return bikeRepository.findAll();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.OK)
    public void create(@RequestBody Bike bike){
        bikeRepository.save(bike);
    }

    @GetMapping("/{id}")
    public Bike get(@PathVariable("id") long id){

        return bikeRepository.getReferenceById(id);
    }
}
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Bike {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    private String phone;
    private String model;
    private String serialNumber;
    private BigDecimal purchasePrice;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy")
    private Date purchaseDate;
    private boolean contact;

// GETTERS AND SETTERS

}

的一个字符串
最后,标准JPA存储库接口:

public interface BikeRepository extends JpaRepository <Bike, Long> {

}


这是来自下列类别:https://app.pluralsight.com/library/courses/building-first-app-with-spring-boot-angularjs/table-of-contents
在过去的几年里,内容更新得很差。考虑到一些主要的依赖迁移,我正在尝试实现自springboot 3. 0 ++以来的主要变化。
某些相关性兼容性可能是导致该问题的原因。
此外,错误“创建名为”entityManagerFactory“的bean时出错”对我来说也是一个严重的误导,如上所述。
因此,经过2天之间的搜索,文档,mvn干净安装,试验和错误,随机猜测之间的版本和chatgpt,我在这里下降了一个消息寻求帮助。
非常感谢.
查尔斯

qmelpv7a

qmelpv7a1#

我终于解决了这个问题。

1)Hibernate社区是关键

首先,重要的是要理解,自Hibernate 6+以来,sqlite方言已经从“hibernate core”移动到“hibernate community dialect”。这在我的一些研究中得到了支持:
https://www.baeldung.com/spring-boot-sqlite
https://discourse.hibernate.org/t/varargssqlfunction-was-deleted-hibernate-6-0s-development/6826
所以我必须明确地加上
1.我的pom.xml中的依赖项“hibernate-community^-dialect”
1.添加到application.properties:spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect
在这一点上,不像以前,我的项目将建立成功。但我仍然会失败的开始。

2)Hibernate core在sping data jpa中,必须排除

其次,Spring Data JPA将Hibernate作为其默认的JPA提供程序。在我的例子中,“spring-data-jpa”包括“Hibernate core 6.2.7.FINAL”。当您运行应用程序时,默认情况下会使用此选项,并且springboot正在Hibernate core中查找sqlite方言,该方言不存在。
所以这里的技巧是在pom. xml中将hibernate核心的排除添加到spring数据jpa依赖项中。
总而言之,你会发现我的pom和我的application.properties。

# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect

# Datasource config for Sqlite
spring.datasource.url=jdbc:sqlite:bike.db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.sqlite.JDBC

个字符

相关问题