![](/img/trans.png)
[英]Spring Boot + Hibernate JPA configuration to use EntityManager
[英]Getting an error that cannot create entityManager while trying to use Spring Data JPA with Spring Boot
我目前正在研究一個 POC,以將 Spring Boot 與 Spring Data JPA 結合使用。
我想使用 Spring Data JPA 從數據庫中獲取記錄。
我收到以下錯誤
創建名為“bookRepository”的 bean 時出錯:在設置 bean 屬性“entityManager”時無法創建類型為 [org.springframework.orm.jpa.SharedEntityManagerCreator] 的內部 bean“(inner bean)”; 嵌套異常是 org.springframework.beans.factory.BeanCreationException:創建名為 '(inner bean)#2' 的 bean 時出錯:設置構造函數參數時無法解析對 bean 'entityManagerFactory' 的引用; 嵌套異常是 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>
請幫我解決這個錯誤
您可以嘗試使用 spring-boot 1.0.2.RELEASE 嗎?
我認為您缺少數據庫驅動程序依賴性。 就像您使用 posgress 一樣,您需要提供驅動程序 jar。
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.