使用hibernate创建内存数据库并将数据加载到

时间:2016-12-28 16:41:03

标签: java hibernate h2

对于测试问题,我想创建一个内存数据库。其中一个表默认具有内容。我该怎么做呢? 我尝试使用以下配置:

<?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','no_reply@mail.com',NULL);

但是当我开始测试时,我收到以下错误:

org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful:    VALUES('1','Confirm','Your registration','Some content here','no_reply@mail.com',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:

有什么问题?

1 个答案:

答案 0 :(得分:0)

我认为Hibernate在多行SQL方面存在问题。 尝试将任何单个查询的所有部分放在一行:

INSERT INTO test.email_template(template_id, description, subject, text, sender, reply_to) VALUES('1','Confirm','Your regitration','Some content here','no_reply@mail.com',NULL);

或使用以下配置:

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