Spring Boot构建报错:NoSuchBeanDefinitionException找不到entityManagerFactory
Hey there, let's troubleshoot your NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available issue. Looking over your configuration and code, I can spot a few key problems that are almost certainly causing this error:
1. Dependency Version Conflicts (The Main Culprit)
Spring Boot's spring-boot-starter-data-jpa already manages compatible versions of Hibernate dependencies for you. But you've manually added two mismatched Hibernate artifacts:
hibernate-core:4.1.4.Finalhibernate-entitymanager:5.2.3.Final
These versions are incompatible with each other and with Spring Boot 1.5.13.RELEASE. This version mismatch breaks Spring's auto-configuration of the entityManagerFactory bean.
Fix: Remove these two dependencies entirely from your pom.xml:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.4.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.3.Final</version> </dependency>
Let Spring Boot's starter handle Hibernate versions automatically—this guarantees compatibility with the rest of your Spring Boot stack.
2. Repository Generic Type Mismatch
Your CardRepository is defined with JpaRepository<CardRegistration, Serializable>, but your entity class is Card. This mismatch prevents Spring Data JPA from properly creating the repository bean, which indirectly disrupts the entity manager setup.
Fix: Correct the repository's generic type to match your entity:
@Repository public interface CardRepository extends JpaRepository<Card, Serializable> { Optional<Card> findByUuid(String uuid); }
(Pro tip: If your AbstractBaseEntity uses a specific ID type like Long, replace Serializable with that type for better type safety—e.g., JpaRepository<Card, Long>.)
3. Redundant & Potentially Problematic Configuration
Your application.properties has duplicate dialect settings:
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
Stick with the Spring Boot-standard property spring.jpa.database-platform and delete the hibernate.dialect line to avoid confusion.
Also, double-check your PostgreSQL password (it's currently empty in your config)—if your database requires a password, this could cause connection failures that also block entity manager creation.
4. Component/Entity Scan Check (Just to Be Safe)
Ensure your main Spring Boot application class is in a parent package that covers your entities and repositories. For example, if your app class is in com.start.new.project, your entities should live in com.start.new.project.entity and repositories in com.start.new.project.repository—Spring Boot auto-scans subpackages by default.
If your entities/repos are in unrelated packages, add these annotations to your main application class:
@SpringBootApplication @EntityScan(basePackages = "com.start.new.project.entity") @EnableJpaRepositories(basePackages = "com.start.new.project.repository") public class ExternalPocApplication { public static void main(String[] args) { SpringApplication.run(ExternalPocApplication.class, args); } }
After making these changes, run mvn clean install to refresh your dependencies, then restart your application. This should resolve the entityManagerFactory bean missing error.
内容的提问来源于stack exchange,提问作者nirvair

