gpt4 book ai didi

java - 考虑在你的配置中定义一个 'javax.persistence.EntityManagerFactory' 类型的 bean

转载 作者:行者123 更新时间:2023-12-04 00:32:35 24 4
gpt4 key购买 nike

我正在使用 Spring Boot 2.0.0.RC1(它包括 Spring Framework 5.0.3.RELEASE)、Hibernate 5.2.12.Final、JPA 2.1 API 1.0.0.Final。

我有一个类

package com.example;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManagerFactory;

@Configuration
public class BeanConfig {

@Autowired
EntityManagerFactory emf;

@Bean
public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}

}

然后报错
Error
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method sessionFactory in com.example.BeanConfig required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.


Process finished with exit code 1

如何解决这个问题?

最佳答案

如果你包括这个:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

您不必自动连接 Entity Manager或提供 Session Factory bean 。

您只需要提供 JpaRepository 接口(interface),例如:
public interface ActorDao extends JpaRepository<Actor, Integer> {
}

在哪里 ActorJPA实体类和 Integer是 ID/主键并注入(inject) ActorDaoservice实现类。

关于java - 考虑在你的配置中定义一个 'javax.persistence.EntityManagerFactory' 类型的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48888603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com