2016-12-28 81 views
0

對於測試問題,我想創建一個內存數據庫。其中一個表格默認應該有內容。我該怎麼做呢? 我試着用以下配置:創建內存數據庫休眠並加載數據

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <!-- Database connection settings --> 
    <property name="connection.driver_class">org.h2.Driver</property> 
    <property name="connection.url">jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS `test`;</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 
    <property name="hibernate.default_schema">test</property> 
    <property name="connection.pool_size">10</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property> 
    <property name="show_sql">true</property> 
    <property name="hibernate.hbm2ddl.auto">create</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.hbm2ddl.import_files">initial_data.sql</property> 
</session-factory> 

文件initial_data.sql很簡單:

INSERT INTO test.email_template(template_id, description, subject, text, sender, reply_to) 
VALUES('1','Confirm','Your registration','Some content here','[email protected]',NULL); 

但是當我開始測試,我得到以下錯誤:

org.hibernate.tool.hbm2ddl.SchemaExport : HHH000388: Unsuccessful: VALUES('1','Confirm','Your registration','Some content here','[email protected]',NULL) 
2016-12-29 13:53:30.826 ERROR 6560 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : Method is not allowed for a query. Use execute or executeQuery instead of executeUpdate; SQL statement: 

出了什麼問題?

+0

例外情況和相關代碼。 –

+0

給出的例外是我得到的一切。 – Kahuna

回答

0

我覺得Hibernate對於多行SQL有一個問題。 嘗試把任何一個查詢的所有部分在同一行:

INSERT INTO test.email_template(template_id, description, subject, text, sender, reply_to) VALUES('1','Confirm','Your regitration','Some content here','[email protected]',NULL); 

,或者使用以下配置:當你問一個問題有關異常,總是發佈的完整的堆棧跟蹤

<property name="hibernate.hbm2ddl.import_files_sql_extractor">org.hibernate.tool.hbm2ddl.M‌​ultipleLinesSqlCommandExtractor</property> 
+0

我試過了,沒有工作。 – Kahuna