Spring Boot 2.0.0.M4 & Hibernate 5.2.11.Final could not find bean of type EntityManagerFactoryBuilder

前端 未结 2 1326
粉色の甜心
粉色の甜心 2021-01-06 04:03

I have clearly what seems to be some configuration issue, but I\'ve been unable to resolve this myself. I have hope you guys could help me?

None of the examples I fi

相关标签:
2条回答
  • 2021-01-06 04:20

    In response to @Albert Bos questions...

    I think what did it was this.

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackageClasses = { MyEntityRepository.class },
            repositoryBaseClass = EntityRepositoryImpl.class,
            transactionManagerRef = "myTransactionManager",
            entityManagerFactoryRef = "myEntityManagerFactory"
    )
    public class MyDataSourceConfiguration { ...
    

    This is my datasource configuration.

    @Bean(name = DATASOURCE_NAME + "DataSource")
    public DataSource dataSource(@Qualifier(DATASOURCE_NAME + "DataSourceProperties") DataSourceProperties dataSourceProperties) 
    {
        return DataSourceBuilder.create()
                .url(dataSourceProperties.getUrl())
                .username(dataSourceProperties.getUsername())
                .password(dataSourceProperties.getPassword())
                .driverClassName(dataSourceProperties.getDriverClassName())
                .build();
    }
    
    @Bean(name = DATASOURCE_NAME + "EntityManagerFactory")
    @PersistenceContext(unitName = DATASOURCE_NAME + "PersistenceUnit")
    public EntityManagerFactory entityManagerFactory(
            @Qualifier(DATASOURCE_NAME + "JpaProperties") Properties jpaProperties,
            @Qualifier(DATASOURCE_NAME + "DataSource") DataSource dataSource,
            @Qualifier(DATASOURCE_NAME + "JpaVendorAdapter") JpaVendorAdapter jpaVendorAdapter) 
    {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setJpaProperties(jpaProperties);
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        factoryBean.setPackagesToScan(MyEntity.class.getPackage().getName());
        factoryBean.setPersistenceUnitName(DATASOURCE_NAME + "PersistenceUnit");
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }
    
    @Bean(name = DATASOURCE_NAME + "TransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier(DATASOURCE_NAME + "EntityManagerFactory") EntityManagerFactory entityManagerFactory) 
    {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }
    

    And Application Configuration

    @SpringBootApplication(exclude = {
            LiquibaseAutoConfiguration.class,
            DataSourceAutoConfiguration.class,
            ValidationAutoConfiguration.class,
            HibernateJpaAutoConfiguration.class,
            JpaRepositoriesAutoConfiguration.class,
            DataSourceTransactionManagerAutoConfiguration.class
    })
    
    0 讨论(0)
  • 2021-01-06 04:37

    By excluding HibernateJpaAutoConfiguration you are turning off creating EntityManagerFactoryBuilder bean.

    By default EntityManagerFactoryBuilder bean is produced by JpaBaseConfiguration#entityManagerFactoryBuilder(JpaVendorAdapter, ObjectProvider).

    There is only one JpaBaseConfiguration implementation - HibernateJpaConfiguration, which is activated if:

    • there is only one DataSource candidate (or one is marked as @Primary);
    • HibernateJpaAutoConfiguration conditions are true - check HibernateJpaAutoConfiguration class.

    You should either create and configure LocalContainerEntityManagerFactoryBean without builder (like you did in your own answer), or create EntityManagerFactoryBuilder in your configuration manually like this:

    @Bean
    public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
        return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
    }
    
    0 讨论(0)
提交回复
热议问题