Добрый день! Мне нужно было сделать пустой проект с Hibernate, Spring и Lombok. Я его сделал, но решил поэкспериментировать, добавляя какую-то логику. После добавления бина для SessionFactory проект перестал запускаться с ошибкой Цитата | Description: A component required a bean named 'entityManagerFactory' that could not be found.
Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
|
Подскажите пожалуйста, в чём может быть дело? 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(); }
}
|
Это сообщение отредактировал(а) Leklerk - 13.4.2024, 15:21
|