DataSourceInitializer不会被调用

时间:2015-11-21 10:55:38

标签: java spring hibernate jpa-2.1

我在我的spring mvc app中为数据库设置java配置,而我无法弄清楚的是为什么DataSourceInitializer bean没有被创建,即永远不会调用dataSourceInitializer方法。

@Configuration
@EnableJpaRepositories(basePackageClasses = {CustomerRepository.class})
@EnableTransactionManagement
public class DatabaseConfig {

    @Resource
    Environment environment;

    @Bean
    public NamedParameterJdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.user"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.pwd"));
        return dataSource;
    }

    @Bean
    public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource);
        initializer.setDatabasePopulator(databasePopulator());
        return initializer;
    }

    @Bean
    public DatabasePopulator databasePopulator() {
        ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
        databasePopulator.addScript(new ClassPathResource("classpath:/db/sql/create-db.sql", DatabaseConfig.class));
        databasePopulator.addScript(new ClassPathResource("classpath:/db/sql/insert-data.sql", DatabaseConfig.class));
        return databasePopulator;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) throws Exception {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceUnitName("CustomersPU");
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.myapp.domain");
        factory.setDataSource(dataSource);

        factory.setJpaProperties(jpaProperties());

        return factory;
    }

    Properties jpaProperties() {
        Properties props = new Properties();
        props.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        props.put("hibernate.hbm2ddl.auto", "validate");
        ...
        return props;
    }
}

更新:我得到的例外是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/myapp/conf/DatabaseConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: CustomersPU] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at ....
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: CustomersPU] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at ...
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [CUSTOMERS]
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:67)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:484)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
    ... 62 more

我正在寻找的是将它配置为类似于此配置:嵌入式数据库+特殊用户名和密码+架构和插入sql文件的路径。如此处所述https://github.com/JohnathanMarkSmith/HelloSpringJavaBasedJavaConfig

更新 如果我只使用EmbeddableDatabase,那么它不允许我定义用户名,密码和数据库连接URL,这对我来说是必要的。以下示例有效,但不允许我定义用户名,密码和数据库连接URL。

@Bean
public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:/db/sql/create-db.sql")
            .addScript("classpath:/db/sql/insert-data.sql")
            .build();
}

0 个答案:

没有答案