Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot (mysql with jpa ): No bean named 'entityManagerFactory' available

Started building a(first) spring boot application and this is my spring boot main class (Full code on github)

@EnableAutoConfiguration
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And here is the 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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-jersey</artifactId>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I am keep getting the below error. What I am missing to add? Why I need to add a bean bean named 'entityManagerFactory' ?? and is it necessary even after using spring boot ??

My repo looks like this if that matter

public interface RegistrationRepo extends JpaRepository<User, Long> {
     User findByEmail(String email); 
}  

. ____ _ __ _ _ /\ / ' __ _ ()_ __ __ _ \ \ \ \ ( ( )_ | '_ | '| | ' / ` | \ \ \ \ \/ )| |)| | | | | || (| | ) ) ) ) ' |____| .|| ||| |__, | / / / / =========|_|==============|___/=///_/ :: Spring Boot :: (v1.5.6.RELEASE)

2017-09-13 12:48:38.808 INFO 15361 --- [ main] com.rightlink.RightLinkApplication : Starting RightLinkApplication on Admins-MacBook-Pro.local with PID 15361 (/Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink/target/classes started by sureshatta in /Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink) 2017-09-13 12:48:38.811 INFO 15361 --- [ main] com.rightlink.RightLinkApplication : No active profile set, falling back to default profiles: default 2017-09-13 12:48:38.865 INFO 15361 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@120d6fe6: startup date [Wed Sep 13 12:48:38 IST 2017]; root of context hierarchy 2017-09-13 12:48:40.380 INFO 15361 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8181 (http) 2017-09-13 12:48:40.402 INFO 15361 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-09-13 12:48:40.403 INFO 15361 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2017-09-13 12:48:40.566 INFO 15361 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-09-13 12:48:40.567 INFO 15361 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1705 ms 2017-09-13 12:48:40.743 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-09-13 12:48:40.752 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/] 2017-09-13 12:48:40.844 WARN 15361 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerController': Unsatisfied dependency expressed through field 'regServices'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'regService': Unsatisfied dependency expressed through field 'registrationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationRepo': Cannot create inner bean '(inner bean)#43c67247' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#43c67247': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available 2017-09-13 12:48:40.849 INFO 15361 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-09-13 12:48:40.903 INFO 15361 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-09-13 12:48:40.975 ERROR 15361 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Field registrationRepo in com.rightlink.services.RegistrationService required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/Test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
server.port=8181

Tried adding @Repository annotation for repo, and also added @enableJpaRepositories annotation in main class. Nothing helps.

like image 567
Suresh Atta Avatar asked Sep 13 '17 06:09

Suresh Atta


People also ask

How do I get EntityManagerFactory in spring boot?

The complete example of getting EntityManager using the custom configuration in Spring Boot. Open eclipse and create maven project, Don't forget to check 'Create a simple project (skip)'click on next. Fill all details(GroupId – entitymanager, ArtifactId – entitymanager and name – entitymanager) and click on finish.

What is EntityManagerFactory spring boot?

EntityManager is an interface provided by Java Persistence API (JPA) specification. We use EntityManager as a general-purpose DAO interface for managing lifecycle of entity instances, such as: Create & Remove persistent entity instances. Find entities by their primary key.


1 Answers

Issue is not with boot version directly and issues are with staled hibernate jars. Since I added JPA support to the project, boot internally using(??) Hibernate as a JPA implementation and trying to use (??) those jars. Since my local repo have have corrupted hibernate-entity-* jars, this weird error happening.

Their comment in the code confirming that they use hibernate internally.

 <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Removed the complete hibernate folder from my maven local repo and nowit seems ok.

like image 82
Suresh Atta Avatar answered Nov 15 '22 17:11

Suresh Atta