0
votes

How do I create a datasource for cloud in MySQL when running on Bluemix? If there are any Java configuration examples available, please share. How do I make Hibernate create tables and why do I get this error?

Error creating bean with name 'entityManagerFactory' defined in com.covenant.app.config.root.DatabaseConfig: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.jdbc.datasource.DriverManagerDataSource]: : No qualifying bean of type [org.springframework.jdbc.datasource.DriverManagerDataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.DriverManagerDataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

My database Config class

@Configuration
@Profile("cloud")
@EnableTransactionManagement
public class CloudDatabaseConfig extends AbstractCloudConfig {

        @Bean
            public DataSource inventoryDataSource() {
                return connectionFactory().dataSource("mysql");
            }
        @Bean(name = "namingStrategy")
        public ImprovedNamingStrategy getNamingStrategy(){

            ImprovedNamingStrategy namingStrategy = new  CDCustomNamingStrategy();
            return namingStrategy;
        }
        @Bean(name="dataSource")
        public BasicDataSource dataSource() throws PropertyVetoException {
            BasicDataSource bean = new BasicDataSource();
            bean.setDriverClassName("com.mysql.jdbc.Driver");
            bean.setUrl("jdbc:mysql://localhost:3306/bluemix?useUnicode=true&characterEncoding=UTF-8");
            bean.setUsername("root");
            bean.setPassword("root");
            return bean;
        }
        @Bean(name = "entityManagerFactory")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, ImprovedNamingStrategy ins) {

            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(dataSource);
            entityManagerFactoryBean.setPackagesToScan(new String[]{"com.covenant.app.model"});
            entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
            entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());


            Map<String, Object> jpaProperties = new HashMap<String, Object>();
            jpaProperties.put("database", "mysql");
            jpaProperties.put("hibernate.hbm2ddl.auto", "update");
            jpaProperties.put("hibernate.show_sql", "true");
            jpaProperties.put("hibernate.format_sql", "true");
            jpaProperties.put("hibernate.use_sql_comments", "true");
            jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            jpaProperties.put("hibernate.ejb.naming_strategy", ins);
            entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);

            return entityManagerFactoryBean;
        }

    }

My manifest.yml file on Bluemix:

---
applications:
  - name: lordthankyou
    path: target/ideals.war
    services:
      - mysql
env:
    SPRING_PROFILES_ACTIVE: cloud

I get the following errors:

Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.covenant.app.dao.UserRepository com.covenant.app.services.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

1

1 Answers

0
votes

Finally I got It working I just added an environment variable to activate cloud profile in manifest.yml and removed extends AbstractCloudConfig as it was also searching for mongodb .After these changes It Started working and now I can run spring mvc on blue mix.