close

[Solved] Spring-boot: required a bean named ‘entityManagerFactory’ that could not be found

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error Spring-boot: required a bean named ‘entityManagerFactory’ that could not be found in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Spring-boot: required a bean named ‘entityManagerFactory’ that could not be found Error Occurs?

Today I am facing the following error Spring-boot: required a bean named ‘entityManagerFactory’ that could not be found in Java. So Here I

How To Solve Spring-boot: required a bean named ‘entityManagerFactory’ that could not be found Error ?

  1. How To Solve Spring-boot: required a bean named 'entityManagerFactory' that could not be found Error ?

    To Solve Spring-boot: required a bean named 'entityManagerFactory' that could not be found Error For me, I've forgotten to remove DataSourceAutoConfiguration.class from exclusions.

  2. Spring-boot: required a bean named 'entityManagerFactory' that could not be found

    To Solve Spring-boot: required a bean named 'entityManagerFactory' that could not be found Error For me, I've forgotten to remove DataSourceAutoConfiguration.class from exclusions.

Solution 1

I was getting error message like-

required a bean named ‘entityManagerFactory‘ that could not be found.

After lot of googling solve this problem. I set manual configuration for JPA.

@Bean 
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 

    return sessionFactory;
}

but JPA by default search sessionFactory by name ‘entityManagerFactory‘ so change my code as:

@Bean(name="entityManagerFactory")
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

    return sessionFactory;
} 

Solution 2

For me, I’ve forgotten to remove DataSourceAutoConfiguration.class from exclusions.

So this:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
// Main class definition

Should be:

@SpringBootApplication()
// Main class definition

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read