Spring data jpa- No bean named 'entityManagerFactory' is defined; Injection of autowired dependencies failed

前端 未结 8 1226
鱼传尺愫
鱼传尺愫 2020-11-27 15:19

I\'m developing application using spring data jpa,hibernate,mysql,tomcat7,maven and it\'s create error.I\'m trying to figure it out but i failed.

error are Cannot

相关标签:
8条回答
  • 2020-11-27 15:33

    Spring Data JPA by default looks for an EntityManagerFactory named entityManagerFactory. Check out this part of the Javadoc of EnableJpaRepositories or Table 2.1 of the Spring Data JPA documentation.

    That means that you either have to rename your emf bean to entityManagerFactory or change your Spring configuration to:

    <jpa:repositories base-package="your.package" entity-manager-factory-ref="emf" /> 
    

    (if you are using XML)

    or

    @EnableJpaRepositories(basePackages="your.package", entityManagerFactoryRef="emf")

    (if you are using Java Config)

    0 讨论(0)
  • 2020-11-27 15:34

    just updated the springboot version to 2.1.3 and it worked of me

    0 讨论(0)
  • 2020-11-27 15:35

    In my specific case I seemed to have been missing the dependency

     <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>5.1.3.RELEASE</version>
     </dependency>
    
    0 讨论(0)
  • 2020-11-27 15:36

    Had this issue when migrated spring boot 1.5.2 to 2.0.4. Instead of creating bean I've used @EnableAutoConfiguration in the main class and it solved my problem.

    0 讨论(0)
  • 2020-11-27 15:41

    I had the same problem and got it resolved by deleting .m2 maven repo (C:\Users\user\ .m2)

    0 讨论(0)
  • 2020-11-27 15:42

    In your application context, change the bean with id from emf to entityManagerFactory:

    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="org.wahid.cse.entity" />
        <property name="dataSource" ref="dataSource" />
    
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
        </property>
    
    </bean>
    

    To

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="org.wahid.cse.entity" />
        <property name="dataSource" ref="dataSource" />
    
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
        </property>
    
    </bean>
    
    0 讨论(0)
提交回复
热议问题