在spring应用程序上下文中设置H2嵌入式数据源

时间:2013-04-30 14:59:47

标签: spring datasource h2

我已经使用MySQL设置了dataSource以进行部署,但我想用H2数据库设置测试环境。

我的MySQL dataSource的应用程序上下文配置是


    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/MyDatabase");
        dataSource.setUsername("username");
        dataSource.setPassword("password");
        dataSource.setInitialSize(2);
        dataSource.setMaxActive(10);
        return dataSource;
    }

我尝试过以下操作来设置H2的数据源



@Configuration
@EnableJpaRepositories("devopsdistilled.operp.server.data")
@EnableTransactionManagement
@PropertySource("server/jdbc.properties")
@ComponentScan("server.data")
public class JpaH2TestContext {

    @Inject
    private Environment env;

    @Value("server.data.entity")
    private String packagesToScan;

    @Bean
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
                .setName("MyDatabase").build();
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.MYSQL);
        jpaVendorAdapter.setGenerateDdl(true);
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter
                .setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        return jpaVendorAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(this.dataSource());
        emf.setJpaVendorAdapter(this.jpaVendorAdapter());
        emf.setPackagesToScan(packagesToScan);
        emf.setJpaProperties(this.hibernateProperties());
        return emf;
    }

    @Bean
    public JpaDialect jpaDialect() {
        return new HibernateJpaDialect();
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory()
                .getObject());
        transactionManager.setJpaDialect(jpaDialect());
        return transactionManager;
    }

    @Bean
    public Properties hibernateProperties() {
        Properties hibernateProps = new Properties();
        hibernateProps.setProperty("hibernate.hbm2ddl.auto", "create");
        return hibernateProps;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}

仅更改了dataSource()。 通过使用H2数据源设置,我可以在控制台中输出。



// ... More like these

ERROR: Unknown data type: "FK71CEE68EBA16946C"; SQL statement:
alter table Product_Category add index FK71CEE68EBA16946C (Product_productId), add constraint FK71CEE68EBA16946C foreign key (Product_productId) references Product (productId) [50004-171]
Hibernate: alter table Stock add index FK4C806F66E8438A (item_itemId), add constraint FK4C806F66E8438A foreign key (item_itemId) references Item (itemId)
Apr 30, 2013 9:18:42 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Stock add index FK4C806F66E8438A (item_itemId), add constraint FK4C806F66E8438A foreign key (item_itemId) references Item (itemId)
Apr 30, 2013 9:18:42 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Unknown data type: "FK4C806F66E8438A"; SQL statement:
alter table Stock add index FK4C806F66E8438A (item_itemId), add constraint FK4C806F66E8438A foreign key (item_itemId) references Item (itemId) [50004-171]
Hibernate: alter table StockKeeper add index FKF22CD8887F68140 (itemWarehouseCatalog_itemWarehouseCatalogId), add constraint FKF22CD8887F68140 foreign key (itemWarehouseCatalog_itemWarehouseCatalogId) references ItemWarehouseCatalog (itemWarehouseCatalogId)
Apr 30, 2013 9:18:42 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table StockKeeper add index FKF22CD8887F68140 (itemWarehouseCatalog_itemWarehouseCatalogId), add constraint FKF22CD8887F68140 foreign key (itemWarehouseCatalog_itemWarehouseCatalogId) references ItemWarehouseCatalog (itemWarehouseCatalogId)
Apr 30, 2013 9:18:42 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Unknown data type: "FKF22CD8887F68140"; SQL statement:
alter table StockKeeper add index FKF22CD8887F68140 (itemWarehouseCatalog_itemWarehouseCatalogId), add constraint FKF22CD8887F68140 foreign key (itemWarehouseCatalog_itemWarehouseCatalogId) references ItemWarehouseCatalog (itemWarehouseCatalogId) [50004-171]
Apr 30, 2013 9:18:42 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

造成这些ERROR: Unknown data type: "FKF22CD8887F68140"; SQL statement:错误的原因是什么?

如何使用H2作为MySQL实现相同的环境?

1 个答案:

答案 0 :(得分:0)

尝试在休眠配置中设置以下属性:

hibernate.dialect = org.hibernate.dialect.H2Dialect

另外,更改数据源配置以使用h2配置。