Java Spring Boot - 存储库字段需要一个无法找到的名为“entityManagerFactory”的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42182426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - repository field required a bean named 'entityManagerFactory' that could not be found
提问by Dheeraj
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 or missing any dependencies. Any help would be appreciated.
我正在开发 Spring Boot 应用程序,在启动服务器时遇到了这个错误。我不确定我是否错误地定义了任何注释或缺少任何依赖项。任何帮助,将不胜感激。
Main class:
主要类:
@SpringBootApplication
public class FantasyManagerApplication {
public static void main(String[] args) {
SpringApplication.run(FantasyManagerApplication.class, args);
}
}
LeagueService.java:
LeagueService.java:
@Service
public class LeagueService {
@Autowired
private LeagueRepository leagueRepository;
@Autowired
private PlayerRepository playerRepository;
@Autowired
private TeamRepository teamRepository;
/**
* Returns a list of all the leagues in the database
* @return List<League>
*/
public List<League> getAllLeagues(){
List<League> leagues = new ArrayList<>();
leagueRepository.findAll()
.forEach(leagues::add);
return leagues;
}
/**
* Find details for a particular League
* @param leagueId
* @return League
*/
public League getLeagueById(long leagueId){
return leagueRepository.findOne(leagueId);
}
/**
* Find the leagueSettings for a particular League
* @param leagueId
* @return LeagueSettings
*/
public LeagueSettings getLeagueSettingsById(long leagueId){
return leagueRepository.findOne(leagueId).getLeagueSettings();
}
/**
* Returns a list of all the Team's in the League
* @param leagueId
* @return List<Team>
*/
public List<Team> getTeamsInLeague(long leagueId){
List<Team> teams = new ArrayList<>();
leagueRepository.findOne(leagueId).getTeams()
.forEach(teams::add);
return teams;
}
/**
* Returns a list of all the Player's in the League
* @param leagueId
* @return List<Player>
*/
public List<Player> getPlayersInLeague(long leagueId){
List<Player> players = new ArrayList<>();
leagueRepository.findOne(leagueId).getPlayers()
.forEach(players::add);
return players;
}
/**
* Returns a list of all the User's in the League
* @param leagueId
* @return List<User>
*/
public List<User> getUsersInLeague(long leagueId){
List<User> users = new ArrayList<>();
leagueRepository.findOne(leagueId).getUsers()
.forEach(users::add);
return users;
}
/**
* Add League to database
* @param league
*/
public void addLeague(League league){
leagueRepository.save(league);
}
/**
* Assign LeagueSettings for a League
* @param userId
* @param leagueSettings
*/
public void assignLeagueSettings(long leagueId, LeagueSettings leagueSettings){
League league = leagueRepository.findOne(leagueId);
league.setLeagueSettings(leagueSettings);
leagueRepository.save(league);
}
/**
* Assign a Player to a League and vice versa
* @param leagueId
* @param playerId
*/
public void assignPlayerToLeague(long leagueId, long playerId){
//Find the league and player from the database
League league = leagueRepository.findOne(leagueId);
Player player = playerRepository.findOne(playerId);
//Get the players that the league already has
List<Player> players = new ArrayList<>();
players = league.getPlayers();
//Get the leagues that the player is part of
List<League> leagues = new ArrayList<>();
leagues = player.getLeagues();
//Assign player to this league and vice versa
leagues.add(league);
players.add(player);
league.setPlayers(players);
player.setLeagues(leagues);
//Update changes in database
playerRepository.save(player);
leagueRepository.save(league);
}
/**
* Assign a Team to a League and vice versa
* @param leagueId
* @param teamId
*/
public void assignTeamToLeague(long leagueId, long teamId){
//Find the league and player from the database
League league = leagueRepository.findOne(leagueId);
Team team = teamRepository.findOne(teamId);
//Get the teams that are already in the league
List<Team> teams = new ArrayList<>();
teams = league.getTeams();
//Assign team to this league and vice versa
teams.add(team);
league.setTeams(teams);
team.setLeague(league);
//Update changes in database
teamRepository.save(team);
leagueRepository.save(league);
}
/**
* Edit the details for a particular League
* @param league
*/
public void updateLeague(League league, long leagueId){
leagueRepository.save(league);
}
/**
* Delete the League from the database
* @param leagueId
*/
public void deleteLeague(long leagueId){
leagueRepository.delete(leagueId);
}
}
LeagueRepository.java
LeagueRepository.java
public interface LeagueRepository extends CrudRepository<League, Long> {
}
pom.xml:
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.dheeraj</groupId>
<artifactId>fantasy-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fantasy-manager</name>
<description>Fantasy Manager Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error:
错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field leagueRepository in com.dheeraj.service.LeagueService required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
回答by kyleus
spring-boot-starter-data-jpawill pull in all the hibernate dependencies you need. Using spring boot release 1.5.1, it will pull in hibernate-core:5.0.11and 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.
spring-boot-starter-data-jpa将引入您需要的所有休眠依赖项。使用 spring 启动版本 1.5.1,它将拉入hibernate-core:5.0.11和hibernate-entitymanager:5.0.11。除了不必要之外,您的休眠依赖项版本不匹配,我猜这是导致错误的原因。
Try removing these dependencies from your pom.xml.
尝试从 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>
回答by Jules S.
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>
回答by malvadao
Try deleting your dependencies from
尝试从
- ~/.m2/repository (Linux)
- C:/Users/your_user/.m2/repository (Windows)
- ~/.m2/repository (Linux)
- C:/Users/ your_user/.m2/repository (Windows)
and further updating your project dependencies either through your IDE's update dependenciesfunctionality, or by running mvn installfrom your project's folder.
并通过 IDE 的更新依赖项功能或通过从项目文件夹运行mvn install进一步更新项目依赖项。
回答by user9347049
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.
Hibernate-core 5.2.0 在处理收集时存在 Spring Data JPA 问题。当您使用 Spring Data JPA 时,Hibernate-core 5.2.1 及更高版本适用于 Spring Data JPA。
From hibernate onwards 5.2.0. hibernate-entitymanager is no longer needed.
从休眠 5.2.0 开始。不再需要 hibernate-entitymanager。
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.
这为我修好了。
回答by Dila Gurung
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>