アノテーション付きのSpring3.1を使用して、埋め込みHSQLを使用するデータソースを作成しています。
@Bean
public DataSource dataSource() throws Exception {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
bean.setDatabaseType(EmbeddedDatabaseType.HSQL);
bean.afterPropertiesSet();
DataSource object = bean.getObject();
return object;
}
私もこのようにSessionFactoryを構成しています
@Bean
public SessionFactory sessionFactory() {
SessionFactory sessionFactory = new LocalSessionFactoryBuilder(dataSource)
.setNamingStrategy(namingStrategy())
.addProperties(hibernateProperties)
.addAnnotatedClass(Some.class)
.buildSessionFactory();
logger.info("Created session factory: " + sessionFactory + " with dataSource: " + dataSource);
return sessionFactory;
}
問題は、データベースにデータを入力する@Componentを使用して他のBeanを作成すると、データベースが作成されていないためにSQLスクリプトが失敗することです。私のhibernate.propertiesには、DDLを生成するための次の行が含まれています
properties.put("hibernate.hbm2ddl.auto", "create-drop");
つまり、これはBeanの作成に関するある種の順序の問題です。ただし、この問題はLinux(Kubuntu 12.04)でのみ発生し、Windows7では発生しません。