1

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

我使用 MySQL 数据源的应用程序上下文配置是


    @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 相同的环境?

4

1 回答 1

-1

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

hibernate.dialect=org.hibernate.dialect.H2Dialect

此外,更改您的数据源配置以使用 h2 配置。

于 2013-09-12T16:02:57.003 回答