【发布时间】:2013-04-30 14:59:46
【问题描述】:
我用 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 相同的环境?
【问题讨论】:
-
无法从类路径资源 [schema.sql] 中读取 SQL 脚本 => 在我看来,spring 无法找到您的 schema.sql。也许这可以帮助 answer about path
-
对不起,我弄错了,我不想运行任何脚本,只需要一个数据库。所以,我删除了
.addDefaultScripts()。另一个问题出现了,现在H2数据库不允许定义外键约束。(ERROR: Unknown data type: "FK4C806F66E8438A"; SQL statement:) -
你能用新的堆栈跟踪和已修改的代码编辑问题吗?
-
我已经更新了问题,删除
.addDefaultScripts()后,问题有所改变。 -
您是否设置了正确的休眠方言?否则你的持久性配置是什么?
标签: spring datasource h2