简体   繁体   中英

Java EE jta hibernate.hbm2ddl.import_files does not work

I try to import sql file when web application runs. I placed import.sql into src/main/resources/import.sql . Then I modified my persistence.xml by adding import_files directive:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.1"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jsp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="spPU" transaction-type="JTA">
    <description>SP Persistence Unit</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/SpDS</jta-data-source>

    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.hbm2ddl.import_files" value="import.sql"/>
    </properties>
  </persistence-unit>
</persistence>

But after redeploy or restart server nothing to uploads in database. I use WildFly 8.1.0, Hibernate 4.3.5.Final, Database h2database 1.3.161. I tried to remove blank lines from import.sql and change hibernate.hbm2ddl.auto to create-drop. Any idea why import.sql does not load in database?

I had similar situation, please check if:

1) You can run your sql script manualy ( No SQL errors are shown when error occurs during import )

2) Please make sure that all sql statements are single-lined , eg:

    create table Table1{ column1, column2,etc.};

instead of

    create table Table1{
     col1 ..,
     col2 ..,
    }

I noticed these 2 issues when using import, hope this solves your problem too.

[UPDATE]
Thx to @vsapiha there is support for multiple lines sql:

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

You don't need to specify the import files. Hibernate will look by default at WEB-INF/classes/import.sql , the place where it will be in your WAR if you place the file where you did. I always use WAR and never had to specify the location of it.

If you use JAR , the import goes in the root of the file, according to this link . (I never tried it though).

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