简体   繁体   中英

Create in-memory database with hibernate and load data into

For testing issues, I would like to create an in-memory database. One of the tables should have content by default. How do I do this? I tried with the following configuration:

<?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>

The file initial_data.sql is simple:

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);

But when I start the tests, I get the following error:

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:

What is wrong?

I think Hibernate has an issue with multi-line SQL. Try to put all parts of any single query on a single line:

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);

or use the following config:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM