2

I have a Hibernate JPA project that has an import.sql file on the classpath. I have tried a number of things to get it working. My persistence.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<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.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="SNAPSHOTPU"
        transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/PostgresqlDS</jta-data-source>
        <class>net.example.Actor</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.schema-generation.database.action"
                value="drop-and-create" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.hbm2ddl.import_files" value="import.sql" />
        </properties>
    </persistence-unit>
</persistence>

I have also tried it without the hibernate.hbm2ddl.* stuff because this is JPA, but neither seems to work. My import.sql is in src/main/resources and this is a Maven project.

I have one valid SQL statement in the file:

insert into actors values (1, 'Test Import.sql');

I even tried a blank line at the beginning of the file as I saw someone suggest. I have debug turned on for Hibernate and can see it running the DDL commands in the log to create the tables, but I do not see anything about import.sql or about the insert statement in the file.

Why isn't this working?

EDIT

@Baldurian got it right below, here is what works (in the persistence.xml):

<properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="javax.persistence.schema-generation.database.action"
        value="drop-and-create" />
    <property name="javax.persistence.sql-load-script-source"
        value="/import.sql" />
</properties>
1
  • very important note import.sql is in src/main/resources Commented Sep 23, 2017 at 12:48

1 Answer 1

1

Try (change javax.persistence.sql-load-script-source path accordingly):

<properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
    <property name="javax.persistence.schema-generation.create-source" value="metadata-then-script"/>
    <property name="javax.persistence.sql-load-script-source" value="/import.sql"/>
</properties>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.