2016-12-28 78 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