java – Spring启动(带jpa的mysql):没有名为’entityManagerFactory’的bean可用

开始构建(第一个) spring boot应用程序,这是我的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>

我一直得到以下错误.我想要添加什么?为什么我需要添加一个名为’entityManagerFactory’的bean bean?甚至在使用弹簧靴之后是否有必要?

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

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

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

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

他们的comment in the code确认他们在内部使用了hibernate.

<!-- 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本地回购中删除了完整的hibernate文件夹,现在看来还可以.

相关文章
相关标签/搜索