Spring启动(带jpa的mysql):没有名为&entityManagerFactory'可用

时间:2017-09-13 06:29:18

标签: java spring jpa exception spring-boot

开始构建一个(第一个)spring启动应用程序,这是我的spring boot主类(Full code on github

@EnableAutoConfiguration
@SpringBootApplication
public class Application {

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

这是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>

我一直收到以下错误。我想要添加什么?为什么我需要添加一个名为&#39; entityManagerFactory&#39;的bean bean。 ??即使在使用弹簧靴后也是必要的吗?

如果那件事我的回购看起来像这样

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

。 ____ _ __ _ _   / \ / &#39; __ _ )_ __ __ _ \ \ \ \   (()_ |&#39; _ |&#39; | |&#39; / `| \ \ \ \   \ / )| | )| | | | | || ( | |))))    &#39; | ____ | | | | | | | __,| / / / / /   ========= | _ | ============== | ___ / = / / / _ /   :: Spring Boot ::(v1.5.6.RELEASE)

     

2017-09-13 12:48:38.808 INFO 15361 --- [main] com.rightlink.RightLinkApplication:使用PID 15361(/ Users / sureshatta / Documents / workspace-)在Admins-MacBook-Pro.local上启动RightLinkApplication sts-3.9.0.RELEASE / RightLink / target / classes由sureshatta在/Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink中启动   2017-09-13 12:48:38.811 INFO 15361 --- [main] com.rightlink.RightLinkApplication:没有活动的配置文件集,回退到默认配置文件:默认   2017-09-13 12:48:38.865 INFO 15361 --- [main] ationConfigEmbeddedWebApplicationContext:刷新org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@120d6fe6:启动日期[Wed Sep 13 12:48:38 IST 2017];上下文层次结构的根   2017-09-13 12:48:40.380 INFO 15361 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:用端口初始化的Tomcat:8181(http)   2017-09-13 12:48:40.402 INFO 15361 --- [main] o.apache.catalina.core.StandardService:启动服务[Tomcat]   2017-09-13 12:48:40.403 INFO 15361 --- [main] org.apache.catalina.core.StandardEngine:启动Servlet引擎:Apache Tomcat / 8.5.16   2017-09-13 12:48:40.566 INFO 15361 --- [ost-startStop-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring嵌入式WebApplicationContext   2017-09-13 12:48:40.567 INFO 15361 --- [ost-startStop-1] o.s.web.context.ContextLoader:Root WebApplicationContext:初始化在1705 ms完成   2017-09-13 12:48:40.743 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean:映射servlet:&#39; dispatcherServlet&#39;至 [/]   2017-09-13 12:48:40.752 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; characterEncodingFilter&#39;至:[/ ]   2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:Mapping filter:&#39; hiddenHttpMethodFilter&#39;致:[/ ]   2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:Mapping filter:&#39; httpPutFormContentFilter&#39;至:[/ ]   2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; requestContextFilter&#39;致:[/ ]   2017-09-13 12:48:40.844 WARN 15361 --- [main] ationConfigEmbeddedWebApplicationContext:上下文初始化期间遇到的异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为&#39; registerController&的bean时出错#39;:通过字段“regServices”表达的不满意的依赖性;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称&#39; regService创建bean时出错:通过字段&reg;注册表示不满意的依赖关系&#39 ;;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; registrationRepo&#39;:无法创建内部bean&#39;(内部bean)#43c67247&#39;类型为[org.springframework.orm.jpa.SharedEntityManagerCreator]时设置bean属性&#39; entityManager&#39 ;;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为&#39;(内部bean)的bean时出错#43c67247&#39;:无法解析对bean的引用&#39; entityManagerFactory&#39;设置构造函数参数时;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有bean命名为&#39; entityManagerFactory&#39;可得到   2017-09-13 12:48:40.849 INFO 15361 --- [main] o.apache.catalina.core.StandardService:停止服务[Tomcat]   2017-09-13 12:48:40.903 INFO 15361 --- [主要] utoConfigurationReportLoggingInitializer:

     

启动ApplicationContext时出错。要显示自动配置报告,请使用&#39; debug&#39;重新运行您的应用程序。启用。   2017-09-13 12:48:40.975 ERROR 15361 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter:

           

申请失败

           

说明

     

com.rightlink.services.RegistrationService中的字段registrationRepo需要一个名为&#39; entityManagerFactory&#39;的bean。无法找到。

     

动作:

     

考虑定义名为&#39; entityManagerFactory&#39;的bean。在你的配置中。

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

尝试为repo添加@Repository注释,并在主类中添加@enableJpaRepositories注释。什么都没有帮助。

1 个答案:

答案 0 :(得分:4)

问题不在于直接启动版本,问题在于staled hibernate jar。由于我在项目中添加了JPA支持,因此使用(??)Hibernate作为JPA实现在内部启动并尝试使用(??)这些jar。由于我的本地仓库已损坏hibernate-entity-* jars,因此发生了这种奇怪的错误。

他们的comment in the code确认他们在内部使用了休眠。

 <!-- 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>

从我的maven local repo中删除了完整的hibernate文件夹,现在看来还不行。