【问题标题】:Consider defining a bean named 'entityManagerFactory' in your configuration-Spring boot考虑在您的配置中定义一个名为“entityManagerFactory”的bean-Spring boot
【发布时间】:2017-11-14 23:06:52
【问题描述】:

以下是我的代码

Spring boot 入口类

@SpringBootApplication
@EnableJpaRepositories("com.test.assertmanagementdigital4.repositories")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@ComponentScan("com.test.assertmanagementdigital4")
public class AssertManagementDigital4Application {

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

控制器

@RestController 公共类 ShopController {

    @Autowired
    private ShopService shopService;

    @RequestMapping(value="/shops",method=RequestMethod.POST)
    public void shopDetails(@RequestBody Shop shop){
        shopService.addShopDetails(shop);

    }

}

实体

@Entity
@Table(name="ShopDetails")
public class Shop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String shopName;
    private Address address;

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    @OneToMany
    @JoinColumn(name="add_Id")
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Shop [id=" + id + ", shopName=" + shopName + ", address=" + address + "]";
    }


}

spring 数据存储库

@Repository
public interface ShopRepository extends CrudRepository<Shop,Long>{

}

服务类

@Service
public class ShopService {
    @Autowired
    private ShopRepository shopRepository;

    public void addShopDetails(Shop shop) {
        shopRepository.save(shop);
    }
}

application.properties

#spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
spring.datasource.url:jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username:sa
spring.datasource.password:
spring.datasource.driver-class-name:org.h2.Driver
spring.datasource.platform:h2

  # enable H2 web console and set url for web console
  # http://localhost:8080/console
  spring.h2.console.enabled:true
  spring.h2.console.path:/console

build.gradle

 buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

我知道这个问题已经有人发布了,但这并没有解决我的问题我仍然收到以下错误,请你告诉我这是什么问题

Description:

Field shopRepository in com.test.assertmanagementdigital4.service.ShopService required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

如果我删除排除自动配置,我会收到以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at com.test.assertmanagementdigital4.AssertManagementDigital4Application.main(AssertManagementDigital4Application.java:20) [bin/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: address, for columns: [org.hibernate.mapping.Column(shop)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    ... 22 common frames omitted

【问题讨论】:

  • 好吧,您明确禁用了 JPA 的自动配置,所以显然没有自动配置 EntityManagerFactory。删除排除或手动定义EntityManagerFactory。此外,您的 spring.datasource 属性几乎没用,而且您还排除了数据源的自动配置。
  • 请检查上面的更新
  • 不要发布堆栈跟踪的 sn-p,发布完整的 stracktrace。
  • M.Deinum 我已经发布了完整的堆栈跟踪
  • 您不能在字段和设置/获取方法上组合注释。要么使用字段级别,要么使用 set/get 方法,而不是同时使用两者。此外,错误出现在您的Address 类中,未在此处发布。

标签: java spring-mvc spring-boot spring-data h2


【解决方案1】:

如果您关闭自动配置,则应自行提供。

查看示例:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws URISyntaxException {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(your_datasource);
    entityManagerFactoryBean.setPackagesToScan(package_to_scan);
    //additional config of factory

    return entityManagerFactoryBean;
}

@Bean(name = "transactionManager")
public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) throws URISyntaxException {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
}

【讨论】:

  • 嗨,我也有同样的问题。我添加了 entityManagerFactory 和 jpaTransactionManager 但之后,它给了我一个错误,因为“创建名称为 'entityManagerFactory' 的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?”。有什么建议吗?
  • @DhiralKaniya,我不确定您的具体情况,但如果您分享您的堆栈跟踪,可以尝试提供帮助。如果问题仍然存在,您可以创建新问题并与我分享。据我所知,您的异常意味着您在 bean 中定义了循环引用。我猜你的entityManagerFactory 依赖于一些具有entityManagerFactory 依赖的bean。
【解决方案2】:

如果您有这段代码,只需将其删除。

public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}

【讨论】:

  • 谢谢。花了大约 40 分钟,然后找到了这个。
【解决方案3】:

这个问题是由于 tomcat 连接池造成的。尝试在 pom.xml 中添加以下依赖项

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
    </dependency>

它解决了我的问题。

【讨论】:

  • 它以前有效,但在我当前的项目中无效。有什么建议吗??
【解决方案4】:

当我在 pom.xml 中将 Spring Boot 父版本更新为 2.1.1 时问题已解决

<!-- Inherit defaults from Spring Boot -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
</parent>

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2022-01-19
    • 2019-01-27
    • 2017-07-19
    • 2017-05-01
    • 2019-02-26
    • 1970-01-01
    • 2022-09-28
    • 2019-12-05
    相关资源
    最近更新 更多