我需要使用 Hibernate、Spring 和 Lombok 制作一个空白项目。我做到了,但决定通过添加一些逻辑来进行实验。为 SessionFactory 添加 bean 后,项目停止启动并出现错误
描述:组件需要一个名为“entityManagerFactory”的 bean,但无法找到。操作:考虑在您的配置中定义一个名为“entityManagerFactory”的 bean。
请告诉我可能出了什么问题?
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.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>VTBInnotehDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>VTBInnotehDemo</name>
<description>Demo project for VTB Innoteh</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@Configuration
public class MyConfig {
@Bean
public SessionFactory getSessionFactory() {
return buildSessionFactory(Person.class, Phone.class);
}
private SessionFactory buildSessionFactory(Class<?> ...annotatedClasses) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
MetadataSources metadataSources = new MetadataSources(createServiceRegistry(configuration));
Arrays.stream(annotatedClasses).forEach(metadataSources::addAnnotatedClass);
Metadata metadata = metadataSources.getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
}
private StandardServiceRegistry createServiceRegistry(Configuration configuration){
return new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
}
}