2014-02-13 33 views
1

我的設置是使用hsqldb進行集成測試的hibernate/JPA。我希望能夠使用sql腳本來將一些測試數據填充到數據庫中。如何在創建時填充嵌入式數據庫(例如hsqldb)

基本上我正在尋找的Spring的

<jdbc:initialize-database data-source="dataSource"> 
     <jdbc:script location="classpath:test-data-hsql.sql"/> 
</jdbc:initialize-database> 

等同除了使用Spring這個項目是不是我的選擇。我已經閱讀了各種hsql文章,建議這可以使用文件模式並指向腳本的路徑,但我一直無法得到這個工作。

的persistence.xml

<persistence-unit name="test-pu" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

    <class>...</class> 

    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"></property> 
     <property name="hibernate.connection.username" value="sa"></property> 
     <property name="hibernate.connection.password" value=""></property> 
     <property name="hibernate.connection.url" value="jdbc:hsqldb:file:test-data-hsql"></property> 
     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"></property> 
     <property name="hibernate.hbm2ddl.auto" value="create"></property> 
     <property name="hibernate.show_sql" value="true"></property> 
    </properties> 

</persistence-unit> 

測試數據hsql.script沒有得到運行,但總有一個錯誤。

如果我在腳本中包含的DDL語句來創建我得到

Caused by: org.hsqldb.HsqlException: invalid schema name: INFORMATION_SCHEMA 
at org.hsqldb.error.Error.error(Unknown Source) 
at org.hsqldb.error.Error.error(Unknown Source) 
at org.hsqldb.SchemaManager.getUserSchemaHsqlName(Unknown Source) 
at org.hsqldb.StatementSchema.getResult(Unknown Source) 
at org.hsqldb.StatementSchema.execute(Unknown Source) 
at org.hsqldb.Session.executeCompiledStatement(Unknown Source) 

如果我排除DDL語句(希望我可以依靠hbm2ddl.auto =創建),然後我得到

Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: INFORMATION_SCHEMA.ONETESTUSER 
at org.hsqldb.error.Error.error(Unknown Source) 
at org.hsqldb.error.Error.error(Unknown Source) 
at org.hsqldb.SchemaManager.getUserTable(Unknown Source) 
at org.hsqldb.scriptio.ScriptReaderText.processStatement(Unknown Source) 
at org.hsqldb.scriptio.ScriptReaderText.readLoggedStatement(Unknown Source) 
at org.hsqldb.scriptio.ScriptReaderText.readDDL(Unknown Source) 
at org.hsqldb.scriptio.ScriptReaderBase.readAll(Unknown Source) 
at org.hsqldb.persist.Log.processScript(Unknown Source) 
at org.hsqldb.persist.Log.open(Unknown Source) 
at org.hsqldb.persist.Logger.open(Unknown Source) 
at org.hsqldb.Database.reopen(Unknown Source) 
at org.hsqldb.Database.open(Unknown Source) 
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source) 

有沒有人有一個工作腳本文件的例子或更好的方法來做到這一點?如果有替代方案可以支持此功能,我不一定與hsqldb綁定。

更新 我使用產生錯誤的最小腳本必須INFORMATION_SCHEMA

drop table AccessToken if exists 
create table AccessToken (id integer generated by default as identity (start with 1), createdAt timestamp not null, lastAccessedAt timestamp, token varchar(255) not null, expirationPolicyId varchar(255) not null, userId varchar(255) not null, primary key (id)) 
insert into ACCESSTOKEN (id, createdAt, lastAccessedAt, token, expirationPolicyId, userId) values (1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'welcome1', 'oneDayPolicy', 'jtrotter'); 
+0

通過普通的vanilla JDBC調用執行腳本的結果是什麼? –

+0

你可以發佈的SQL,即INFORMATION_SCHEMA部分 –

+1

發佈的SQL,歡呼 – Josh

回答

0

沒有提到你需要在腳本中的DDL語句,否則數據庫是空的,你得到的第二個錯誤。

問題出在腳本中,對mySQL特有的元數據模式INFORMATION_SCHEMA有參考。刪除對該架構中對象的引用,以便可以完成創建表。

+0

謝謝 - 我已更新與正在使用的SQL腳本的職位 - 沒有明確提及INFORMATION_SCHEMA,這是某種默認被使用HSQLDB? – Josh

相關問題