Home > Blockchain >  Java Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configurat
Java Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configurat

Time:01-03

I'm working on a basic application using Java Spring Boot, I'm stuck on this error:

This is the error message:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-16 12:44:40.321 ERROR 5698 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.htamayo.sbcrashcourse.SbcrashcourseApplication required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
  1. My main class looks like this:

     package com.htamayo.sbcrashcourse;
    
     import com.htamayo.sbcrashcourse.lendingengine.domain.model.User;
     import com.htamayo.sbcrashcourse.lendingengine.domain.repository.UserRepository;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.boot.CommandLineRunner;
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.ComponentScan;
     import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
     @SpringBootApplication
     @ComponentScan(basePackages = {"com.htamayo.sbcrashcourse.lendingengine"})
     @EnableJpaRepositories("com.htamayo.sbcrashcourse.lendingengine.domain.repository")
     public class SbcrashcourseApplication implements CommandLineRunner {
    
     @Autowired
     private UserRepository userRepository;
    
     public static void main(String[] args) {SpringApplication.run(SbcrashcourseApplication.class, args);}
    
     @Override
     public void run(String... args) throws Exception {
         userRepository.save(new User(1, "John", "B", 27, "Software Developer"));
         userRepository.save(new User(2, "Peter", "C", 21, "Pilot"));
         userRepository.save(new User(3, "Henry", "E", 21, "Unemployed"));
     }
    

    }

  2. UserRepository class is this:

    package com.htamayo.sbcrashcourse.lendingengine.domain.repository;
    
     import com.htamayo.sbcrashcourse.lendingengine.domain.model.User;
     import org.springframework.data.jpa.repository.JpaRepository;
    
     public interface UserRepository extends JpaRepository<User, Long> {
    
     }
    
  3. And my pom.xml looks like this:

     <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-orm</artifactId>
     </dependency>
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>5.4.32.Final</version>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>5.3.9</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>5.3.9</version>
     </dependency>
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>5.5.7.Final</version>
     </dependency>
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
         <version>5.5.7.Final</version>
     </dependency>
     <dependency>
         <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
         <version>2.3.1</version>
     </dependency>
    
    org.springframework.boot spring-boot-maven-plugin

I've been searching for solutions on Google but so far nothing, so my question is: I don't understand how to overcome the EntityManagerFactory error, am I missing a dependency? or should I refactor my code? any solutions?

Thanks a lot for your suggestions.

CodePudding user response:

Well, after 17 days finally I got the solution:

my problem was on properties file, for some reason I used this line (I can't remember why): spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

I just commented it and problem solved.

Lessons learned:

  1. always document your code
  2. JitterTed's discord is a great source of answers.
  • Related