在尝试将Spring Data JPA与Spring Boot一起使用时,获取无法创建entityManager的错误

时间:2014-04-29 12:10:48

标签: java spring spring-data-jpa spring-boot

我目前正在开发一个POC,将Spring Boot与Spring Data JPA结合使用。

我想使用Spring Data JPA从数据库中获取记录。

我收到以下错误

  

创建名为'bookRepository'的bean时出错:在设置bean属性'entityManager'时,无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#2'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'entityManagerFactory'的bean

以下是我的移民课程:

package com.boot.configration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableAutoConfiguration
@ComponentScan
@EnableJpaRepositories
public class ApplicationStarter {
    public static void main (String[] args) {
        SpringApplication.run(ApplicationStarter.class, args);
    }

}

以下是我的寄托

package com.boot.configration;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, String> {
    public Iterable<Book> findBooksByAuthor(@Param("author") String author);
}

这是我的控制器

@RestController
public class BookController {
    @Autowired
    protected BookRepository bookRepository;

    @RequestMapping(value = "/isbn")
    @ResponseBody
    public String book() {
        Book book = bookRepository.findOne("2222222");
        return "Book Name is = " + book.getTitle()+ " "  + "Author is = " + book.getAuthor();
    }

}

在POM.xml中,我有以下依赖项:

<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>BOOT</groupId>
    <artifactId>SpringBootProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.0.RC1</version>
    </parent>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
    <name>SpringBootProject</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>
</project>

请帮我解决此错误

2 个答案:

答案 0 :(得分:0)

您可以尝试使用spring-boot 1.0.2.RELEASE?

答案 1 :(得分:0)

我认为您缺少数据库驱动程序依赖性。 就像您使用 posgress 一样,您需要提供驱动程序 jar。

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>