Spring Boot - repository field required a bean named 'entityManagerFactory' that could not be found

后端 未结 5 2147
温柔的废话
温柔的废话 2020-12-06 16:18

I am in the process of developing a Spring Boot application and came across this error when starting the server. I am not sure if I am incorrectly defining any annotations o

相关标签:
5条回答
  • 2020-12-06 16:42

    The problem might be due to version mismatch. Problem can be solve by specifying the dependency like this (since version is not mentioned, its auto managed)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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-web</artifactId>
        </dependency>
    </dependencies>
    
    0 讨论(0)
  • 2020-12-06 16:55

    spring-boot-starter-data-jpa will pull in all the hibernate dependencies you need. Using spring boot release 1.5.1, it will pull in hibernate-core:5.0.11 and hibernate-entitymanager:5.0.11. In addition to being unnecessary, your hibernate dependency versions are mismatched which is what I'm guessing is causing the error.

    Try removing these dependencies 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>
    
    0 讨论(0)
  • 2020-12-06 16:59

    Hibernate-core 5.2.0 have problem with Spring Data JPA while process collection. Hibernate-core 5.2.1 and above is ok with Spring Data JPA when you use Spring Data JPA.

    From hibernate onwards 5.2.0. hibernate-entitymanager is no longer needed.

    So try to change:

    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>4.1.4.Final</version>
    </dependency>
    

    To this version:

    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version> 5.2.1.Final</version>
    </dependency>
    

    This fixed it for me.

    0 讨论(0)
  • 2020-12-06 16:59

    Try deleting your dependencies from

    • ~/.m2/repository (Linux)
    • C:/Users/your_user/.m2/repository (Windows)

    and further updating your project dependencies either through your IDE's update dependencies functionality, or by running mvn install from your project's folder.

    0 讨论(0)
  • 2020-12-06 17:05

    Try those dependencies

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

    instead of:

    <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>
    
    0 讨论(0)
提交回复
热议问题