当前位置: 首页 > 知识库问答 >
问题:

Spring Boot-Repository字段需要一个名为“Entity ManagerFactory”的bean,但找不到该bean

胡意致
2023-03-14

我正在开发一个Spring Boot应用程序,在启动服务器时遇到了这个错误。我不确定我是否错误地定义了任何注释或遗漏了任何依赖项。如有任何帮助,我们将不胜感激。

主类:

@SpringBootApplication
public class FantasyManagerApplication {

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

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

public interface LeagueRepository extends CrudRepository<League, Long> {

}
<?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>
***************************
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.

共有1个答案

巫健柏
2023-03-14

spring-boot-starter-data-jpa将引入您需要的所有hibernate依赖项。使用spring boot Release1.5.1,它将引入Hibernate-Core:5.0.11和Hibernate-EntityManager:5.0.11。除了不必要之外,您的hibernate依赖项版本也不匹配,我猜这就是导致错误的原因。

尝试从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>
 类似资料:
  • 使用spring boot 2.0版本。0.M4我有这个问题:

  • > 启动ApplicationContext时出错。若要显示自动配置报告,请在启用“debug”的情况下重新运行应用程序 设置构造函数参数时无法解析对bean“Entity ManagerFactory”的引用 找不到名为“Entity ManagerFactory”的bean Field repository需要一个名为“Entity ManagerFactory”的bean,但找不到该bean

  • 编辑-更新:我根据本教程创建了一个全新的项目,我注意到,在配置pom后,问题是如果我添加 应用程序类的注释。 我是Spring Boot的新手,我已经创建了一个具有持久性的简单工作应用程序,现在我试图添加Spring security jwt,但它不起作用。 以下是我的项目结构: 在为安全性添加依赖项之前,它在持久性方面都工作得很好,现在是这样。我错过了什么?

  • 我正在尝试部署我的spring应用程序。以下是pom的副本。xml文件。 以下是申请的副本。属性文件。 我得到的错误跟踪如下。 我被夹在中间。我几乎没试过什么东西。因为提供entityManager的hibernate JPA启动器是从pom中删除hibernate核心和hibernate实体管理器的。xml也是如此。但我也犯了同样的错误。除此之外,我还创建了自定义datasoruce,如下所示。

  • 我有一个java项目,它将Spring Boot与JPA结合使用,并将Hibernate用于数据库。我正在尝试建立一个访问数据库的微服务。(我不熟悉微服务和Spring Boot)。 以下是主要课程: IGmCircularsDAO. class: GMCircularsDAOImpl。类别: ParentDAO。班 循环服务。班 当我运行这段代码时,我遇到了以下错误,我已经陷入其中一段时间了。

  • 我正在使用Spring Boot开发一个简单的Spring Batch jar。我已经使用配置类创建了dataSource bean,并用@Component进行了注释。但是当我使用命令行Runner运行应用程序时,它在读取ABPBatchInfrastructure.xml时抛出bean not found异常。 我在谷歌上对这个错误做了一些研究,找到了一个解决方案,我在ABPBatchInfr