我有一个在内存POC中运行H2数据库的应用程序,我需要自动导入src / main / resources文件夹中的schema.sql和data.sql脚本。根据我的阅读,只需将这些文件放在该文件夹中即可,当启动服务器时,我可以看到它们正在执行:
2018-09-30 14:01:45.644 INFO 9720 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/home/brandon/code/validus/music-web/target/classes/schema.sql]
2018-09-30 14:01:45.677 INFO 9720 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/home/brandon/code/validus/music-web/target/classes/schema.sql] in 33 ms.
2018-09-30 14:01:45.680 INFO 9720 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/home/brandon/code/validus/music-web/target/classes/data.sql]
2018-09-30 14:01:45.707 INFO 9720 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/home/brandon/code/validus/music-web/target/classes/data.sql] in 27 ms.
问题在于,当我随后登录到架构时,它是空的。这是我的JPA配置:
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.validus" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:testdb");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
// properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("spring.jpa.generate-ddl", "false");
properties.setProperty("spring.jpa.hibernate.ddl-auto", "create");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
// properties.setProperty("javax.persistence.schema-generation.database.action", "none");
// properties.setProperty("javax.persistence.schema-generation.scripts.action", "none");
// properties.setProperty("javax.persistence.schema-generation.create-source", "none");
return properties;
}
}
我设法关闭了基于POJO的生成,但是我希望它仍然自动导入schema.sql。我在这里想念什么?
谢谢
答案 0 :(得分:0)
创建一个sql文件夹,以便您可以将sql脚本放入其中,并尝试在数据源Bean中像下面这样尝试代码:
try {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
return dbBuilder.setType(EmbeddedDatabaseType.H2)
.addScript("Classpath:sql/security.sql").build();
}catch(Exception e) {
logger.error("Embedded Datasource bean cannot be created !!", e);
return null;
}
希望这会有所帮助